<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] ## 概述 - SSH 除了密碼登錄和秘鑰登錄,還可以通過證書登錄 - 它是更合理、更安全的登錄方法 ### 非證書登錄的缺點 1.密碼登錄需要輸入服務器密碼,這非常麻煩,也不安全,存在被暴力破解的風險。 2. 密鑰登錄需要服務器保存用戶的公鑰,也需要用戶保存服務器公鑰的指紋。這對于多用戶、多服務器的大型機構很不方便,如果有員工離職,需要將他的公鑰從每臺服務器刪除 ### 證書登錄是什么 它引入了一個證書頒發機構(Certificate1 authority,簡稱 CA) 1. 用戶和服務器不用交換公鑰,這更容易管理,也具有更好的可擴展性。 2. 證書可以設置到期時間,而公鑰沒有到期時間。針對不同的情況,可以設置有效期很短的證書,進一步提高安全性 ### 證書登錄的流程 SSH 證書登錄之前,如果還沒有證書,需要生成證書。 具體方法是: 1. 用戶和服務器都將自己的公鑰,發給 CA; 2. CA 使用服務器公鑰,生成服務器證書,發給服務器; 3. CA 使用用戶的公鑰,生成用戶證書,發給用戶 用證書后,用戶登錄服務器流程(用戶是無感知的) 1. 用戶登錄服務器時,SSH 自動將用戶證書發給服務器 2. 服務器檢查用戶證書是否有效,以及是否由可信的 CA 頒發。證實以后,就可以信任用戶 3. SSH 自動將服務器證書發給用戶 4. 用戶檢查服務器證書是否有效,以及是否由信任的 CA 頒發。證實以后,就可以信任服務器 5. 第五步,雙方建立連接,服務器允許用戶登錄 ## 操作流程 ### 生成 CA 的密鑰 - 而 CA 本質上就是一對密鑰,跟其他密鑰沒有不同,CA 就用這對密鑰去簽發證書 - 雖然 CA 可以用同一對密鑰簽發用戶證書和服務器證書,但是出于安全性和靈活性,最好用不同的密鑰分別簽發 - 所以,CA 至少需要兩對密鑰,一對是簽發用戶證書的密鑰,假設叫做`user_ca`,另一對是簽發服務器證書的密鑰,假設叫做`host_ca` 生成 user_ca: ``` ssh-keygen -t rsa -b 4096 -f ~/.ssh/user_ca -C user_ca -f 指定生成密鑰的位置和文件名 -C 指定密鑰的識別字符串,相當于注釋,可以隨意設置 ``` >會在`~/.ssh`目錄生成一對密鑰:`user_ca`(私鑰)和`user_ca.pub`(公鑰) 生成 host_ca: ``` ssh-keygen -t rsa -b 4096 -f ~/.ssh/host_ca -C host_ca ``` > 在`~/.ssh`目錄生成一對密鑰:`host_ca`(私鑰)和`host_ca.pub`(公鑰) ### CA 簽發服務器證書 SSH 服務器(通常是`sshd`)安裝時,已經生成密鑰`/etc/ssh/ssh_host_rsa_key` 如果沒有的話,可以用下面的命令生成: ``` ssh-keygen -f /etc/ssh/ssh_host_rsa_key -b 4096 -t rsa ``` > 會在`/etc/ssh`目錄,生成`ssh_host_rsa_key`(私鑰)和`ssh_host_rsa_key.pub` 需要把服務器公鑰ssh_host_rsa_key.pub,復制或上傳到 CA 所在的服務器 CA 為服務公鑰簽發證書 ``` ssh-keygen -s host_ca -I host.example.com -h -n host.example.com -V +52w ssh_host_rsa_key.pub -s 指定 CA 簽發證書的密鑰。 -I 身份字符串,可以隨便設置,相當于注釋,方便區分證書, 將來可以使用這個字符串撤銷證書。 -h 指定該證書是服務器證書,而不是用戶證書。 -n 指定服務器的域名,表示證書僅對該域名有效。 如果有多個域名,則使用逗號分隔。 用戶登錄該域名服務器時,SSH 通過證書的這個值, 分辨應該使用哪張證書發給用戶,用來證明服務器的可信性。 -V +52w 指定證書的有效期,這里為52周(一年)。 默認情況下,證書是永遠有效的。建議使用該參數指定有效期 ssh_host_rsa_key.pub 服務器公鑰 ``` 查看證書詳情 ``` ssh-keygen.exe -L -f ssh_host_rsa_key-cert.pub ssh_host_rsa_key-cert.pub: Type: ssh-rsa-cert-v01@openssh.com host certificate Public key: RSA-CERT SHA256:kvLXmSTWcmj1x8G/+9CKx72I1T7KY4JpPJ3gqOWQrwA Signing CA: RSA SHA256:NNGLy0yE89owhFg4VjYscR7+AlO6DnaPU+M+ulGubgw (using rsa-sha2-512) Key ID: "host.example.com" Serial: 0 Valid: from 2020-12-15T10:08:00 to 2021-12-14T10:09:50 Principals: host.example.com Critical Options: (none) Extensions: (none) ``` 設置權限 ``` chmod 600 ssh_host_rsa_key-cert.pub ``` ### CA 簽發用戶證書 需要用戶的公鑰,如果沒有的話,客戶端可以用下面的命令生成一對密鑰 ``` ssh-keygen -f ~/.ssh/user_key -b 4096 -t rsa ``` > `~/.ssh`目錄,生成`user_key`(私鑰)和`user_key.pub`(公鑰) 將用戶公鑰`user_key.pub`,上傳或復制到 CA 服務器 ``` ssh-keygen -s user_ca -I user@example.com -n user -V +1d user_key.pub ``` 設置權限 ``` chmod 600 user_key-cert.pub ``` ### 服務器安裝證書 CA 生成服務器證書`ssh_host_rsa_key-cert.pub`以后,需要將該證書發回服務器 可以使用下面的`scp`命令,將證書拷貝過去: ``` scp ~/.ssh/ssh_host_rsa_key-cert.pub root@host.example.com:/etc/ssh/ ``` 并修改配置文件 `/etc/ssh/sshd_config` ``` HostCertificate /etc/ssh/ssh_host_rsa_key-cert.pub ``` ### 服務器安裝CA簽發的用戶 的公鑰 將用戶公鑰發送到服務器 ``` scp ~/.ssh/user_ca.pub root@host.example.com:/etc/ssh/ ``` 修改配置,添加用戶CA公鑰 `/etc/ssh/sshd_config`,此配置是讓所有服務器所有用戶都信任此證書 ``` TrustedUserCAKeys /etc/ssh/user_ca.pub ``` 讓指定賬號信任證書,`user_ca.pub`加到服務器某個賬戶的`~/.ssh/authorized_keys` 具體方法是打開`~/.ssh/authorized_keys`,追加一行,開頭是`@cert-authority principals="..."`,然后后面加上user_ca.pub的內容 如下 ``` @cert-authority principals="user" ssh-rsa AAAAB3Nz...XNRM1EX2gQ== ``` `principals="user"`指定用戶登錄的服務器賬戶名,一般就是`authorized_keys`文件所在的賬戶 重啟 ssh服務 ``` $ sudo systemctl restart sshd.service # 或者 $ sudo service sshd restart ``` ### 客戶端安裝證書 從 CA 將用戶證書`user_key-cert.pub`復制到客戶端,與用戶的密鑰`user_key`保存在同一個目錄即可 ### 客戶端安裝 CA 公鑰 為了讓客戶端信任服務器證書,必須將 CA 簽發服務器證書的公鑰`host_ca.pub`,加到客戶端的`/etc/ssh/ssh_known_hosts`文件(全局級別)或者`~/.ssh/known_hosts`文件(用戶級別) 具體做法是打開`ssh_known_hosts`或`known_hosts`文件,追加一行,開頭為`@cert-authority *.example.com`,然后將`host_ca.pub`文件的內容(即公鑰)粘貼在后面 ``` @cert-authority *.example.com ssh-rsa AAAAB3Nz...XNRM1EX2gQ== ``` - `*.example.com`是域名的模式匹配,表示只要服務器符合該模式的域名,且簽發服務器證書的 CA 匹配后面給出的公鑰,就都可以信任 - 如果沒有域名限制,這里可以寫成`*` - 如果有多個域名模式,可以使用逗號分隔; - 如果服務器沒有域名,可以用主機名或ip名 現在就可以用證書登錄了 ``` ssh -i ~/.ssh/user_key user@host.example.com ``` > 如果證書與密鑰在同一個目錄,則連接服務器時將自動使用該證書 ### 廢除證書 服務器證書的廢除 - 用戶需要在`known_hosts`文件里面,修改或刪除對應的`@cert-authority`命令的那一行 用戶證書的廢除 - 需要在服務器新建一個`/etc/ssh/revoked_keys`文件,然后在配置文件`sshd_config`添加一行 ``` RevokedKeys /etc/ssh/revoked_keys ``` - revoked_keys文件保存不再信任的用戶公鑰,由下面的命令生成 ``` ssh-keygen -kf /etc/ssh/revoked_keys -z 1 ~/.ssh/user1_key.pub -z 參數用來指定用戶公鑰保存在revoked_keys文件的哪一行 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看