實(shí)現(xiàn)功能
主機(jī)A 免密訪問 主機(jī)B垮庐。
創(chuàng)建秘鑰
登錄主機(jī)A,創(chuàng)建主機(jī)秘鑰和公鑰坞琴。
user@A:~$ ssh-keygen -t rsa
直接回車哨查,使用默認(rèn)值即可,秘鑰文件默認(rèn)生成到$HOME/.ssd/目錄:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/seewin/.ssh/id_rsa):
Created directory '/home/seewin/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/seewin/.ssh/id_rsa.
Your public key has been saved in /home/seewin/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ut6DNj0a3jjrEAJRi4CsxXDfvnM83BV/BPODil3iuzY seewin@seewin
The key's randomart image is:
+---[RSA 2048]----+
|*+o o |
|o=oo . .+ |
|+.. . . ..o .o|
|.. . + +o ..|
| . . . S. +. . .|
| . . = . .. . |
| . =o= .. |
| o=O+. E. |
| +O=oo... |
+----[SHA256]-----+
復(fù)制公鑰內(nèi)容
user@A:~$ ssh-copy-id user@B
接下來剧辐,就可以在主機(jī)A上直接使用SSH或者SCP訪問主機(jī)B了:
user@A:~$ ssh user@B
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-70-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Sun Nov 24 00:51:15 UTC 2019
System load: 0.08 Processes: 106
Usage of /home: 0.0% of 858.31GB Users logged in: 1
Memory usage: 5% IP address for enp2s0: 192.168.1.2
Swap usage: 0%
0 packages can be updated.
0 updates are security updates.
Failed to connect to https://changelogs.ubuntu.com/meta-release-lts. Check your Internet connection or proxy settings
Last login: Sun Nov 24 00:46:36 2019 from 192.168.1.3
user@B:~$
或者寒亥,使用手工的方式將A的公鑰拷貝到B主機(jī):
user@A:~$ cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD4LnrbrQrUR58hvDZChgLnOQh1OobKmYWAr8QBtuV8FYS2NWBkf+UqKt9e8/xA6F32SUBOFGtSQcPCyr0eb6QyS6N0JSwWgvGVEe13HqgpDBucqdoRrsnap6zvvqANMN+dVg0wJ/WP07JyTgYXCoQmAFc5Xj1YtlaYLflWCXuqs5Dzr4jjqBQUNggAP3DASxF8RcDvhExUwGBxaymwh61TrucdcwE/NUlm2d52oXKnj89F1NduXpQzDoLyyAD7FVADcPTxcKuJwdyorBKrYsK1SMUyon3p6xD4SM7VGLNF9FQ+qYkxWLnUS3p0px6uP4LShHDGXFoJba0ab3NI6kCF user@A
創(chuàng)建默認(rèn)認(rèn)證
登錄主機(jī)B,進(jìn)入HOME/.ssh/目錄荧关。若沒有.ssh目錄溉奕,則創(chuàng)建該目錄:
user@B:~$ mkdir ~/.ssh
檢查是否存在authorized_keys文件,若不存在忍啤,則創(chuàng)建加勤。
user@B:~$ touch authorized_keys
將主機(jī)A的公鑰內(nèi)容添加到authorized_keys文件末尾:
user@B:~$ vi authorized_keys
# 這里是原有內(nèi)容,其它主機(jī)的公鑰數(shù)據(jù)同波。
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD4LnrbrQrUR58hvDZChgLnOQh1OobKmYWAr8QBtuV8FYS2NWBkf+UqKt9e8/xA6F32SUBOFGtSQcPCyr0eb6QyS6N0JSwWgvGVEe13HqgpDBucqdoRrsnap6zvvqANMN+dVg0wJ/WP07JyTgYXCoQmAFc5Xj1YtlaYLflWCXuqs5Dzr4jjqBQUNggAP3DASxF8RcDvhExUwGBxaymwh61TrucdcwE/NUlm2d52oXKnj89F1NduXpQzDoLyyAD7FVADcPTxcKuJwdyorBKrYsK1SMUyon3p6xD4SM7VGLNF9FQ+qYkxWLnUS3p0px6uP4LShHDGXFoJba0ab3NI6kCF user@A
驗(yàn)證免密登錄
登錄主機(jī)A胸竞,執(zhí)行ssh 或 scp:
user@A:~$ ssh user@B
user@A:~$ scp abc.txt user@B:~/data/