在 CentOS 上配置 SSH 免密登录(公钥登录),一般步骤如下:
ssh-keygen -t rsa -b 4096
一路回车即可(默认生成在 ~/.ssh/id_rsa 和 ~/.ssh/id_rsa.pub)
ssh-copy-id user@server_ip
示例:
ssh-copy-id root@192.168.1.100
输入一次密码后,以后即可免密登录。
在客户端执行:
cat ~/.ssh/id_rsa.pub
复制内容,到服务器上:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
vi ~/.ssh/authorized_keys
粘贴公钥,保存后:
chmod 600 ~/.ssh/authorized_keys
ssh user@server_ip
如果不需要输入密码,说明成功 ✅
服务器上必须是:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
restorecon -Rv ~/.ssh
检查 /etc/ssh/sshd_config:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
修改后重启:
systemctl restart sshd
确认免密可用后:
PasswordAuthentication no
systemctl restart sshd
如果你是想 CentOS → CentOS 或 Windows → CentOS,也可以告诉我,我可以给你对应版本。