經常在不同linux機器之間互相scp拷文件,每次總是要輸入密碼才可行。
通過ssh-keygen生成公鑰,在兩台機器之間互相建立信任通道即可。
假設本地機器client,遠程機器為server。
1. 生成rsa keygen
[winter@client winter] $ ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/winter/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/winter/.ssh/id_rsa.
Your public key has been saved in /home/winter/.ssh/id_rsa.pub.
The key fingerprint is:
33:d4:7b:9c:87:04:cf:14:40:22:6d:c7:15:78:97:6a winter@client
直接上面公鑰和私鑰存放地址可以直接回車,私鑰密碼可以直接回車,也可以輸入。
2. 查看.ssh目錄下了多私鑰和公鑰文件
[winter@client winter] $ ls .ssh/
id_rsa id_rsa.pub known_hosts
3. 拷貝公鑰到目標機器上,並改名成authorized_keys
[winter@client winter] $ scp .ssh/id_rsa.pub [email protected]:/home/winter/.ssh/authorized_keys
The authenticity of host '192.168.0.110(192.168.0.110)' can't be established.
RSA key fingerprint is 5b:78:e8:e9:dc:d8:db:bd:f7:66:44:e6:3b:ce:f2:79.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.110' (RSA) to the list of known hosts.
[email protected]'s password:
id_rsa.pub 100% 221 1.2MB/s 00:00
首次scp命令時候還是會提示輸入密碼,還有是否繼續鏈接的提示,以後
4. 測試ssh進入
[winter@client winter] $ ssh 192.168.0.110
5. ok,搞定!
[winter@server winter] # it's ok!