centos/redhat 7 配置ssh启用公钥认证实现免密登陆

1、确保本地local_host和远程remote_host都安装了openssh

2、local_host创建ssh密钥对:ssh-keygen -t rsa, 然后一路enter

3、remote_host创建.ssh目录和authorized_keys文件,然后修改权限

mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys


chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

4、将local_host创建的公钥rsa_rsa.pub传到remote_host的authorized_keys里面。可通过scp:

scp ~/.ssh/id_rsa.pub username@remote_host:~/.ssh/authorized_keys

也可通过ssh-copy-id命令(建议):

ssh-copy-id username@remote_host

5、ssh-copy-id执行成功后即可在本地主机上使用ssh免密登陆远程主机。之后可以禁用远程主机的密码登陆:

vim /etc/ssh/sshd_config

修改这一行为 no

PasswordAuthentication yes

PasswordAuthentication no