多个服务器之间免密登录
免密登录流程
操作流程
生成公钥和私钥
使用
ssh-keygen -t rsa
生成秘钥
输出结果
使用命令会让你设置保存地址和私钥密码这里需要直接回车使用默认和空密码
[root@localhost102 data]# 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:
SHA256:sq************************************tDm root@localhost102
The key's randomart image is:
***************************************************
拷贝密钥
直接使用
ssh-copy-id
拷贝秘钥
输出结果
[root@localhost102 data]# ssh-copy-id localhost103
/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@localhost103's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'localhost103'"
and check to make sure that only the key(s) you wanted were added.
使用ssh登录其他服务器
这样就可以使用ssh免密登录其他服务器了
[root@test102 ~]# ssh test103
Last login: Fri Sep 16 08:16:23 2022 from 192.168.65.60
[root@localhost103 ~]#
.ssh文件夹内容
shh包含以下文件
[root@test102 ~]# ll .ssh/
总用量 16
-rw-------. 1 root root 798 9月 15 15:53 authorized_keys
-rw-------. 1 root root 1679 9月 15 15:25 id_rsa
-rw-r--r--. 1 root root 399 9月 15 15:25 id_rsa.pub
-rw-r--r--. 1 root root 736 9月 15 15:51 known_hosts
文件名 | 说明 |
---|---|
id_rsa | 生成的私钥 |
id_rsa.pub | 生成的公钥 |
authorized_keys | 存放授权过的无密登录服务器公钥 |
known_hosts | 记录ssh访问过计算机的公钥(public key) |