2009年8月16日 星期日

使用 RSA 登入 SSH

在想要登入的遠端主機上執行下列動作

1.建立 RSA Key

#ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
// 輸入 key 的密碼,可以直接按 Enter 輸入空白,但建議還是使用密碼保護的好
Enter passphrase (empty for no passphrase):
// 再重覆輸入一次
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
e5:2b:5d:97:89:40:4a:a5:e5:5b:90:d1:47:e1:ee:74 root@linux
進入使用者家目錄內的 .ssh 目錄內
#cd /root/.ssh
變更 id_rsa.pub (公鑰) 名稱為 authorized_keys
#mv id_rsa.pub authorized_keys
變更檔案屬性為 600
#chmod 600 authorized_keys

2.修改設定檔

sshd 的設定檔為 /etc/ssh/sshd_config,用 vi 編輯,修改下列字串
#Protocol 2,1 變更為 Protocol 2
#PasswordAuthentication yes 變更為 PasswordAuthentication no
存檔後離開,重新啟動 sshd
#service sshd restart
ps.至於要不要把 PermitRootLogin yes 改為 PermitRootLogin no,則視你需要,反正有 Key 保護,有需要使用 root 作業的人(Ex.WinSCP),就不用改,不需要用 root 作業的人,還是把它改掉的好。

在 Windows Client 上做以下動作

1.轉換 id_rsa

用你覺得安全的方法將 id_rsa (私鑰) 取回,由於 Server 端產生的私鑰格式和 pietty(putty)不同,必需經過轉換處理,先下載 puttygen,直接執行。

點擊 Conversions>Import key ,選擇剛剛取回的 id_rsa ,由於在建立公私鑰時有設定密碼 ( 非 Linux 密碼 ),所以出現如下圖所示,要求輸入密碼。


接下來只要點擊 Save private key 將私鑰存檔即可,就這麼簡單

2.將私鑰匯入 Pietty (putty) 內

打開 Pietty,點擊 選項>詳細設定,出現如下圖,Browse 剛剛用 puttygen 轉換完成的私鑰檔案,點擊 Apply 即可。


連線到伺服器上,可以發現和之前的帳號密碼登入已經不同了,現在必須擁有 Key 並且輸入 Key 的密碼才能登入伺服器了。

沒有留言:

張貼留言