用指令ssh-keygen -t rsa在 serverA中生成密鑰對,-t rsa表示采用RSA算法來生成密鑰對.生成的密鑰對預設存放在~/.ssh/目錄中,id_rsa.pub中存放公鑰,id_rsa存放私鑰.
(注:在生成密鑰對的時候系統會提示輸入密鑰對的密碼,這裡請直接回車,不要輸入密碼,否則後面打通通道之後仍然需要密碼,隻不過是用這裡設定的密碼而已)
将id_rsa.pub中的公鑰拷貝出來,追加到 serverB中的~/.ssh/authorized_keys檔案中.
本機ssh打通自己:
[email protected]:~/repos$ cd ~
[email protected]:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/zj/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/zj/.ssh/id_rsa.
Your public key has been saved in /home/zj/.ssh/id_rsa.pub.
The key fingerprint is:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [email protected]
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
| xxxxxxxxxx |
| xxxxxxxxxx |
| xxxxxxxxxxx |
| xxxxxxxxxxxx |
| xxxxxxxxxxxx |
| xxxxxxxxxxx |
|xxxxxxxxxxxx |
|xxxxxx |
|xxxx |
+-----------------+
[email protected]:~$ cd ~/.ssh
[email protected]:~/.ssh$ ls
id_rsa id_rsa.pub known_hosts
[email protected]:~/.ssh$ touch authorized_keys
[email protected]:~/.ssh$ cat id_rsa.pub > authorized_keys
test:
[email protected]:~/.ssh$ ssh 127.0.0.1
Linux debian-zj 2.6.26-1-686 #1 SMP Fri Mar 13 18:08:45 UTC 2009 i686
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
You have new mail.
[email protected]:~$ exit
logout
Connection to 127.0.0.1 closed.
[email protected]:~/.ssh$
說明:
authorized_keys檔案的權限很重要,如果設定為777,那麼登入的時候,還是需要提供密碼的。
chmod 400 authorized_keys
轉載于:https://blog.51cto.com/zhangjunhd/156307