目录
2、在server1上做,将生成的密钥放在authorized_keys文件中
3、分别登录server2、server3、server4做如下操作,以server2为例。将id_rsa.pub复制到server1上
4、在server1上操作,将server2、server3、server4上的id_rsa.pub内容复制到authorized_keys文件中
5、将server1上authorized_keys的文件复制到server2、server3、server4上
一、前提
1、本集群近四台机器,设置其机器名称为server1、server2、server3、server4。
2、四台机器均需要创建用户alanchan,并且加入root用户组
二、具体操作
1、四台机器均需要做的操作,以server1为示例
[alanchan@server1 ~]$ssh-keygen -t rsa
2、在server1上做,将生成的密钥放在authorized_keys文件中
- [alanchan@server1 .ssh]$ touch ~/.ssh/authorized_keys
- [alanchan@server1 .ssh]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
- [alanchan@server1 .ssh]$ cd ~/.ssh/
- [alanchan@server1 .ssh]$ cat authorized_keys
- ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtM+WPAtjLuqsaDL3hVcSfwhUCNFCUenLgWlkRvUgJkGOn8Cfg+Ael6zHaL3EhqJU873pmNnn1yR4FQXYQxJ3omsisyugYvbufhmXjrQXrmtL2BJKWmylzgqpvpVBmJ4xjCbpTQvSWPi5oCIEY2crIMVocyGco/NA18qezN08h4hKQd/EScZGBpMmst5os0C1YUoqnWP83rcWxo1f/qXXBGCBCuXLDbLI94XxT6FMhfUXCwgRQxDXTMzqJ7rZRa5W/6lVueTEXivLbXUkis/Xo0CEAVSWt0hlNq4i9A2i2nISZLbB9Fp0GkwQhVZMNuKj8ccDzeNwPh8YxZSOFpCUKw== alanchan@server1
3、分别登录server2、server3、server4做如下操作,以server2为例。将id_rsa.pub复制到server1上
- [alanchan@server2 .ssh]$ scp ~/.ssh/id_rsa.pub alanchan@server1:~/.ssh/server2.id_rsa.pub
- alanchan@server1's password:
- id_rsa.pub
- #登录server1查看发送的结果
- [alanchan@server1 .ssh]$ ll
- 总用量 28
- -rw-r--r-- 1 alanchan root 398 9月 8 14:03 authorized_keys
- -rw------- 1 alanchan root 1671 9月 8 13:40 id_rsa
- -rw-r--r-- 1 alanchan root 398 9月 8 13:40 id_rsa.pub
- -rw-r--r-- 1 alanchan root 2805 9月 7 09:07 known_hosts
- -rw-r--r-- 1 alanchan root 398 9月 8 14:03 server2.id_rsa.pub
- -rw-r--r-- 1 alanchan root 398 9月 8 14:04 server3.id_rsa.pub
- -rw-r--r-- 1 alanchan root 398 9月 8 14:05 server4.id_rsa.pub
4、在server1上操作,将server2、server3、server4上的id_rsa.pub内容复制到authorized_keys文件中
- [alanchan@server1 .ssh]$ cat ~/.ssh/server2.id_rsa.pub >> ~/.ssh/authorized_keys
- [alanchan@server1 .ssh]$ cat ~/.ssh/server3.id_rsa.pub >> ~/.ssh/authorized_keys
- [alanchan@server1 .ssh]$ cat ~/.ssh/server4.id_rsa.pub >> ~/.ssh/authorized_keys
- [alanchan@server1 .ssh]$ ll
- 总用量 28
- -rw-r--r-- 1 alanchan root 1592 9月 8 14:06 authorized_keys
- -rw------- 1 alanchan root 1671 9月 8 13:40 id_rsa
- -rw-r--r-- 1 alanchan root 398 9月 8 13:40 id_rsa.pub
- -rw-r--r-- 1 alanchan root 2805 9月 7 09:07 known_hosts
- -rw-r--r-- 1 alanchan root 398 9月 8 14:03 server2.id_rsa.pub
- -rw-r--r-- 1 alanchan root 398 9月 8 14:04 server3.id_rsa.pub
- -rw-r--r-- 1 alanchan root 398 9月 8 14:05 server4.id_rsa.pub
- [alanchan@server1 .ssh]$ cat authorized_keys
- ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtM+WPAtjLuqsaDL3hVcSfwhUCNFCUenLgWlkRvUgJkGOn8Cfg+Ael6zHaL3EhqJU873pmNnn1yR4FQXYQxJ3omsisyugYvbufhmXjrQXrmtL2BJKWmylzgqpvpVBmJ4xjCbpTQvSWPi5oCIEY2crIMVocyGco/NA18qezN08h4hKQd/EScZGBpMmst5os0C1YUoqnWP83rcWxo1f/qXXBGCBCuXLDbLI94XxT6FMhfUXCwgRQxDXTMzqJ7rZRa5W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FMHWMw5V3vDA69SJvLfoBoZfvT8C48g1XpEI0me1X+6KpDQk+1CZu4nA1NSmytxbl9OmcoxDo2ywLtclycjfhKfKUYL7uBXNT8oiUbwUc6RTme5WC0sU3oTvQLcHbQjR218+LZi74xszWssRuJmrUlc0zZy/6GjsK2pIieO7Iq5lq9uBQ== alanchan@server4
5、将server1上authorized_keys的文件复制到server2、server3、server4上
如此可以做到server1到server4的双向免密登录。如果是单向免密登录,这步可以不做。
- [alanchan@server1 .ssh]$ scp ~/.ssh/authorized_keys alanchan@server2:~/.ssh/
- alanchan@server2's password:
- authorized_keys 100% 1592 1.6KB/s 00:00
- [alanchan@server1 .ssh]$ scp ~/.ssh/authorized_keys alanchan@server3:~/.ssh/
- alanchan@server3's password:
- authorized_keys 100% 1592 1.6KB/s 00:00
- [alanchan@server1 .ssh]$ scp ~/.ssh/authorized_keys alanchan@server4:~/.ssh/
- alanchan@server4's password:
- authorized_keys 100% 1592 1.6KB/s 00:00
- [alanchan@server1 .ssh]$ ll
- 总用量 28
- -rw-r--r-- 1 alanchan root 1592 9月 8 14:06 authorized_keys
- -rw------- 1 alanchan root 1671 9月 8 13:40 id_rsa
- -rw-r--r-- 1 alanchan root 398 9月 8 13:40 id_rsa.pub
- -rw-r--r-- 1 alanchan root 2805 9月 7 09:07 known_hosts
- -rw-r--r-- 1 alanchan root 398 9月 8 14:03 server2.id_rsa.pub
- -rw-r--r-- 1 alanchan root 398 9月 8 14:04 server3.id_rsa.pub
- -rw-r--r-- 1 alanchan root 398 9月 8 14:05 server4.id_rsa.pub
6、在四台机器上都操作,进行文件夹授权
- [alanchan@server1 ~]$ chmod 700 ~/.ssh
- [alanchan@server1 ~]$ chmod 600 ~/.ssh/authorized_keys
7、验证
在server1上测试连接server2、server3、server4的免密登录情况
- [alanchan@server1 ~]$ ssh server2
- Last login: Thu Sep 8 13:54:04 2022 from 192.168.3.105
- [alanchan@server2 ~]$ exit
- logout
- Connection to server2 closed.
- [alanchan@server1 ~]$ ssh server3
- Last login: Thu Sep 8 13:54:27 2022 from 192.168.3.105
- [alanchan@server3 ~]$ exit
- logout
- Connection to server3 closed.
- [alanchan@server1 ~]$ ssh server4
- Last login: Thu Sep 8 05:54:48 2022 from 192.168.3.105
- [alanchan@server4 ~]$ exit
- 注销
- Connection to server4 closed.
以上完成后,免密登录完成。
评论记录:
回复评论: