备注:ssh免密码互通,应该为多台服务器间通信,我这里只用一台服务器来做处理。
1,修改主机名。
vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=centos1
vi /etc/hosts
#末尾添加 ip 主机名
192.168.9.129 centos1
hostname centos1
#修改主机名临时生效
2, 无密码登录实现
2.1、用ssh-keygen创建公钥(每台服务器都需要执行)
[root@centos1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
a0:85:f4:58:d4:c7:2f:d9:ae:b1:04:91:b1:a0:9e:93 root@centos1
The key's randomart image is:
+--[ RSA 2048]----+
| ..+..+ |
| . * .+.o |
| + + .o + |
| . = .. o o |
| E S. o |
| . o . |
| . + |
| o |
| |
+-----------------+
免密码互通,直接回车完成。
ssh-keygen:生成秘钥
其中:
-t指定算法
-f 指定生成秘钥路径
-N 指定密码
2.2、查看生成的钥匙
[root@centos1 ~]# cd .ssh/
[root@centos1 .ssh]# ls
id_rsa id_rsa.pub known_hosts
2.3 将公钥复制到被管理机器下的.ssh目录下(先确保存在这个目录)
我这里是单机,就不需要执行这步操作。
接着执行
[root@centos1 .ssh]# cat id_rsa.pub >> ~/.ssh/authorized_keys
[root@centos1 .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
4、设置文件和目录权限:
设置authorized_keys权限
chmod 600 authorized_keys
设置.ssh目录权限
chmod 700 -R .ssh
验证是否成功
[root@centos1 .ssh]# ssh centos1
Last login: Thu Dec 17 10:15:52 2015 from 192.168.9.1