系统环境
[root@saltStack01 .ssh]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
要免密登录其他主机的服务器(server)IP
10.123.208.34
其他主机(clients)IP
10.123.20.2
10.123.20.3
工作原理
安装ssh软件
yum install -y openssh-server + openssh-clients + openssh
[root@saltStack01 .ssh]# yum list installed | grep ssh
libssh2.x86_64 1.4.3-10.el7_2.1 @anaconda
openssh.x86_64 7.4p1-16.el7 @centos7-x86_64-base
openssh-clients.x86_64 7.4p1-16.el7 @centos7-x86_64-base
openssh-server.x86_64 7.4p1-16.el7 @centos7-x86_64-base
检查是否启动ssh进程
[root@saltStack01 .ssh]# ps -Af | grep sshd
root 819 1 0 2018 ? 00:00:17 /usr/sbin/sshd
root 22118 819 0 14:45 ? 00:00:00 sshd: root@pts/0
root 22492 819 0 15:01 ? 00:00:00 sshd: root@pts/1
root 23403 22494 0 15:33 pts/1 00:00:00 grep --color=auto sshd
在server端生成公钥
生成的公钥存放在~.ssh/id_rsa.pub文件中,公钥拷贝到clients端的~/.ssh/authorized_keys文件中
[root@saltStack01 .ssh]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
[root@saltStack01 .ssh]#cat id_rsa.pub
复制公钥
cd .ssh/
touch authorized_keys
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCswLJiyCtMxsDzCrLT1R3jbnL3+StOyx4/XRJPTyexcj5bcNPSrQyVVhe8hEFMFqW5ubNlGG09zoBSm0uGacKxYbNqBLaZb/JQPvVszBBx8Tqgbop8Chddz18SLj8+hqkel6Yw7DRikeSswz28tM+MmRe4lI3/O5nAt+s5cW8sAJlbbCXJOh4Z+4ioFSEipY5h3gZuvuhFvnGZaBMVuahnfOp2NmhbaXsuET1/+ZlYGMQW4Vwd06XyG5cJGBX01pU4JSeulUThqlMnLd21VK8ojs8Z92JilExtWoaH19y826wty+XqhrjIFfWj6FplLRjyaNQ5ZDvlFlxST2P2j/Kr root@saltStack01.wotv.com" >> ~/.ssh/authorized_keys
修改文件权限
chmod 644 authorized_keys
测试
在server端上测试
[root@saltStack01.ssh]#ssh 10.123.208.2
Authorized only.All activity will be monitored and reported
Last login:Mon May 27 15:29:47 2019 from 10.123.209.34
Authorized users only.All activity may be monitored and reported
centos 6还需要修改配置文件
vim /etc/ssh/sshd_config
PubkeyAuthentication yes #去掉此行注释
/sbin/service sshd restart #重启ssh