debian更改密码访问为ssh密钥访问
生成密钥
ssh-keygen
或ssh-keygen -t rsa一路回车在 root 用户目录中生成了一个 .ssh 目录,内含两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥,公钥下载到本地。
在服务器上安装公钥
[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys保证以下文件权限正确
chmod 600 authorized_keys
chmod 700 ~/.ssh
设置 SSH,打开密钥登录功能
编辑 /etc/ssh/sshd_config 文件,确保如下内容取消注释
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
PubkeyAuthentication yes
PermitRootLogin yes #允许root 用户通过 SSH 登录确保允许root 用户通过 SSH 登录:
重启ssh服务:
service sshd restart切换为root用户和密码登录后,下载 .ssh 目录下的私钥文件id_rsa,导入ssh客户端。
确保root用户能够通过密钥登录。
完成全部设置,并以密钥方式登录成功后,再禁用密码登录,不建议禁用,可以设置一个复杂的密码
passwd root #修改密码PasswordAuthentication no最后,重启 SSH 服务:
[root@host .ssh]$ service sshd restart参考