2011/05/15

SSH免密碼登入

使用ssh登入遠端server時會要求輸入密碼,有些時候想要略過此步驟(如執行auto script時),建立免密碼登入就可以派上用場。

1. 在local端建立public key與private key
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test/.ssh/id_rsa):     (按enter)
Enter passphrase (empty for no passphrase):                       (按enter)
Enter same passphrase again:                                      (按enter)
Your identification has been saved in /home/test/.ssh/id_rsa.     (按enter)
Your public key has been saved in /home/test/.ssh/id_rsa.pub.     (按enter)
The key fingerprint is:
fe:3f:63:3d:7d:34:06:53:a3:53:22:73:c3:c4:a3:f3 test@test-desktop.local
andomart image is:
+--[ RSA 2048]----+
|           o+  . |
|          .  +.E+|
|         .   ..=.|
|   . o  ..= . oo.|
|  . + = So.=. o. |
|   . . o  ...+   |
|           .     |
|                 |
|                 |
+-----------------+
在~/.ssh目錄下產生了id_rsa與id_rsa.pub

2. 將public key放到server端
$ cd ~/.ssh
$ scp id_rsa.pub username@server_host_name:~/.ssh

3. 進到server端~/.ssh目錄,將public key加到authorized_keys的結尾(authorized_keys可以放置多組),並修改權限為600
$ cd ~/.ssh
$ cat id_rsa.pub >> authorized_keys
$ chmod 600 authorized_keys

下次登入server時就不需要輸入密碼了。


沒有留言:

張貼留言