在Ubuntu上设置SSH密钥认证可以提供更安全的登录方式,避免每次登录时都需要输入密码。以下是详细的步骤:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-t rsa
:指定使用RSA算法。-b 4096
:设置密钥长度为4096位,增加安全性。-C "your_email@example.com"
:添加一个注释,通常是你的电子邮件地址,便于标识密钥用途。~/.ssh/id_rsa
(私钥)和~/.ssh/id_rsa.pub
(公钥)。你可以按回车键接受默认位置。~/.ssh/authorized_keys
文件中:ssh-copy-id user@remote_host
user
:远程服务器上的用户名。remote_host
:远程服务器的IP地址或域名。authorized_keys
文件中。/etc/ssh/sshd_config
文件:sudo nano /etc/ssh/sshd_config
#
开头):PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
sudo systemctl restart sshd
ssh user@remote_host
为了进一步提高安全性,你可以禁用密码认证。编辑/etc/ssh/sshd_config
文件:
sudo nano /etc/ssh/sshd_config
找到并确保以下行被注释掉或设置为no
:
PasswordAuthentication no
保存并退出编辑器,然后重启SSH服务:
sudo systemctl restart sshd
现在,只有拥有正确密钥的用户才能登录远程服务器。
通过以上步骤,你就可以在Ubuntu上成功设置SSH密钥认证了。