1.场景:3台Linux,IP地址分别是:192.168.111.129/192.168.111.144/192.168.111.145;
2.要求:192.168.111.129不输入密码登录到192.168.111.144和192.168.111.145;
3.操作步骤如下:
3.1登录到192.168.111.129执行ssh-keygen命令
[root@localhost ~]# ssh-keygen
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:
SHA256:5KjcV3+tY6JdYJCM2Rr3BqfkHZPfj3h+cHmihwxWC+Q root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| |
| =.. . |
| =oO = |
| + *EB.+ . |
| . S +o*.. o|
| . o .o+.oo++|
| o . .. ooo=++|
| . .==+..|
| ...+oo |
+----[SHA256]-----+
[root@localhost ~]#
配置选项保持默认即可,直接回车,直到最后结束;
3.2向主机分发私钥
[root@localhost ~]# ssh-copy-id root@192.168.111.144
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.111.144's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.111.144'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]#
[root@localhost ~]# ssh-copy-id root@192.168.111.145
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.111.145's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.111.145'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]#
3.3测试免密
[root@localhost ~]# ssh root@192.168.111.144
Last login: Wed May 8 08:58:02 2024
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:24:01:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.111.144/24 brd 192.168.111.255 scope global noprefixroute dynamic ens33
valid_lft 1699sec preferred_lft 1699sec
inet6 fe80::a825:8c14:ec67:906/64 scope link noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::581e:b295:ea37:15c6/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
[root@localhost ~]#
实现免密登录认证通过。
PS:注意,如果出现了一下报错:
-bash: ssh-copy-id: command not found
那么就证明我们需要安装一个包:
yum -y install openssh-clientsansible
把包安装上即可。