一、環境說明:操作系統:CentOS-7-x86_64-Minimal-1611虛擬機:VMware® Workstation 12 Pro;12.5.5 build-5234757伺服器:node1(User1),node2(User2)二、實現內容:node1 伺服器用戶 User1 可通過 S... ...
一、環境說明:
操作系統:CentOS-7-x86_64-Minimal-1611
虛擬機:VMware® Workstation 12 Pro;12.5.5 build-5234757
伺服器:node1(User1),node2(User2)
二、實現內容:
node1 伺服器用戶 User1 可通過 SSH ,免密鑰登錄伺服器 node2 的 User2 賬戶;
三、配置流程:
預設情況下,node1 上的用戶 User1 想連接 node2,需要輸入密碼,如下:
1 [User1@localhost ~]$ ssh User2@node2 2 The authenticity of host 'node2 (192.168.126.141)' can't be established. 3 ECDSA key fingerprint is 0c:f2:1d:5b:0a:ea:38:43:e7:d2:07:28:d8:05:8a:d6. 4 Are you sure you want to continue connecting (yes/no)? yes 5 Warning: Permanently added 'node2,192.168.126.141' (ECDSA) to the list of known hosts. 6 User2@node2's password: 7 Last login: Fri Feb 2 17:03:45 2018
1、現在通過用戶 User1 在 node1 的 /home/User1 目錄下生成一對公鑰和私鑰:
1 [User1@localhost ~]$ ssh-keygen -t rsa 2 Generating public/private rsa key pair. 3 Enter file in which to save the key (/home/User1/.ssh/id_rsa): 4 Enter passphrase (empty for no passphrase): 5 Enter same passphrase again: 6 Your identification has been saved in /home/User1/.ssh/id_rsa. 7 Your public key has been saved in /home/User1/.ssh/id_rsa.pub. 8 The key fingerprint is: 9 ff:cc:0e:22:aa:79:54:d3:15:90:bd:98:2b:26:e1:35 [email protected] 10 The key's randomart image is: 11 +--[ RSA 2048]----+ 12 | .+.. | 13 | . o | 14 | . + . | 15 | . E + . | 16 | . + oS. | 17 | + o .. | 18 | . o... o | 19 | ... . . = | 20 | oo. .= | 21 +-----------------+
2、然後將公鑰上傳給 node2 的 User2 用戶:
1 [User1@localhost ~]$ ssh-copy-id User2@node2 2 /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed 3 /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys 4 User2@node2's password: 5 6 Number of key(s) added: 1 7 8 Now try logging into the machine, with: "ssh 'User2@node2'" 9 and check to make sure that only the key(s) you wanted were added.
3、登錄驗證成功:
說明:
1、此時 node1 上用戶 User1 的私鑰文件內容(id_rsa.pub)會追加到 node2 上用戶 User2 的 ~/.ssh/authorized_keys 文件中:
2、還可以通過 scp 命令,將 node1 伺服器上的公鑰拷貝到 node2 伺服器上,然後追加到 ~/.ssh/authorized_keys,
1 scp ~/.ssh/id_rsa.pub User2@node2:~/
3、還可以通過 cat 命令直接追加:
1 cat ~/.ssh/id_rsa.pub | ssh -P 22 User2@node2 'cat >> ~/.ssh/authorized_keys'