debian更改密码访问为ssh密钥访问

69 881

生成密钥

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

参考

https://www.runoob.com/w3cnote/set-ssh-login-key.html

https://pa.ci/153.html

debian设置ssh密钥登录 – 168itw


Prev Post nginx如何优雅的一键切换停机维护界面
Next Post debian12桌面环境无法切换登录root用户的解决方法