Loading... ### 1. 在服务器上制作密钥对。 首先使用root或者其他账户登录服务器,然后执行以下命令: ```shell ## 创建密钥对 [root@host ~]$ ssh-keygen Generating public/private rsa key pair. ## 指定存放目录,默认回车即可 Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. ## 设置密钥密码,不设密码则留空 Enter passphrase (empty for no passphrase): ## 再次输入 Enter same passphrase again: ## 创建成功 Your identification has been saved in /root/.ssh/id_rsa Your public key has been saved in /root/.ssh/id_rsa.pub ``` ### 2. 安装公钥 键入以下命令,在服务器上安装公钥: ```shell [root@host ~]$ cd .ssh [root@host .ssh]$ cat id_rsa.pub >> authorized_keys ``` 如此便完成了公钥的安装。为了确保连接成功,请保证以下文件权限正确: ```shell [root@host .ssh]$ chmod 600 authorized_keys [root@host .ssh]$ chmod 700 ~/.ssh ``` ### 3. 设置 SSH,打开密钥登录功能 编辑 /etc/ssh/sshd_config 文件,进行如下设置: ```shell RSAAuthentication yes PubkeyAuthentication yes ``` 另外,请留意 root 用户能否通过 SSH 登录: ```shell PermitRootLogin yes ``` 最后,重启 SSH 服务: ```shell [root@host .ssh]$ service sshd restart ``` 当你以密钥方式登录成功后,可以禁用密码登录(可选的): ```shell PasswordAuthentication no ``` ### 客户端导入 下载私钥文件`/root/.ssh/id_rsa`至本地,导入密钥, 如果使用Xshell则无需转换密钥,使用Putty&WinSCP则需要把密钥文件转换为PPK 最后修改:2021 年 06 月 07 日 © 允许规范转载 打赏 赞赏作者 支付宝 赞 0 如果觉得我的文章对你有用,请随意赞赏