在 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
authorized_keys: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
编辑:
vi /etc/ssh/sshd_config
确认:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
修改后重启:
systemctl restart sshd
确认免密可用后:
PasswordAuthentication no
然后:
systemctl restart sshd
如果你是要 CentOS → CentOS 或 Windows → CentOS,我也可以给你对应版本。