iT邦幫忙

DAY 17
10

關於IT資訊界的筆記和學習紀錄系列 第 23

SSH 免用密碼登入(key)

前言 :
登入Linux的方式,大部份都是使用SSH登入或PuTTY(Windows登入),一般的使用者大部份都是輸入帳號密碼來做登入,但是在輸入帳號密碼的同時也增加了被竊取帳密的可能性。為了降低被竊取的可能性,可以使用公開金鑰(Public Key)和私密金鑰(Private Key)對應的方式,近而做到免密碼的登入,來降低風險。

一、 Key介紹 :
金鑰的種類可分為三種 : (RSA1、RSA2、DSA)。
SSH可分為 : (Protocol1和Protocol2)
1.RSA1 : 此種金鑰屬於舊系統在做使用的,如(FreeBSD 4及以前的版本,都是為SSH Protocol1,所以都使用RSA1這種金鑰。)
2.RSA2及DSA : 屬於較新的系統在做使用的,(如FreeBSD 5及之後的版本或Linux其他版本。都是使用RSA2或DSA這兩種金鑰。)

Passphrase :
Passphrase可設也可不設,若有設定可以增加主機的資安保護,才不至於一旦被別人拿走私鑰時就可直接登入主機。但也增加了每次登入主機都要輸入Passphrase。此部份在SSH時是可以使用ssh-agent機制來解決,而在PuTTY(Windows登入)時可用Pageant
,來解決每次都要輸入Passphrase的問題。

如何產生金鑰 :
如何產生金鑰,金鑰的產生方式有兩種,一種是下面要說到的【本機端產生】,另一種就是使用【PuTTYgen(Windows使用)】來產生。不管使用那種方式,會建議一次就把三種金鑰都產生好。可省下不同系統,不同金鑰的問題。
二、 key的設定方式

  1. 產生金鑰(Key)
    1.1 產生RSA1金鑰
    產生identity、identity.pub這兩把RSA1的金鑰

    [root@localhost ~]# ssh-keygen -t rsa1
    Generating public/private rsa1 key pair.
    Enter file in which to save the key (/root/.ssh/identity): #詢問金鑰要放在那邊,按Enrer就可
    Created directory '/root/.ssh'.
    Enter passphrase (empty for no passphrase): #輸入Passphrase (可設也可不設,最少9碼)
    Enter same passphrase again: #再輸入一次Passphrase
    Your identification has been saved in /root/.ssh/identity.
    Your public key has been saved in /root/.ssh/identity.pub.
    The key fingerprint is:
    52:e2:d5:58:fb:fa:3e:43:88:5c:4b:6c:bb:a5:d7:f7 root@localhost.localdomain

1.2 產生RSA2金鑰
產生id_rsa、id_rsa.pub這兩把RSA2的金鑰

[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  #詢問金鑰要放在那邊,按Enrer就可
Enter passphrase (empty for no passphrase):    #輸入Passphrase (可設也可不設,最少9碼)
Enter same passphrase again:#再輸入一次Passphrase
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:
92:56:e9:13:b1:0f:f5:41:cd:29:25:74:ef:66:4b:4c root@localhost.localdomain

1.3 產生DSA金鑰
產生id_dsa、id_dsa.pub這兩把DSA的金鑰

[root@localhost ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):  #詢問金鑰要放在那邊,按Enrer就可
Enter passphrase (empty for no passphrase):   #輸入Passphrase (可設也可不設,最少9碼)
Enter same passphrase again:   #再輸入一次Passphrase
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
fa:39:46:95:4d:a2:4f:35:01:60:28:0f:58:3b:4d:ce root@localhost.localdomain

經過上方程序後,會有三組金鑰: RSA1 ( identity.pub / identity )、RSA2 ( id_rsa.pub / id_rsa )、及 ( id_dsa.pub / id_rsa )
如果沒有特殊需求,你可以只用一份、用任一系統產生、並把這一份給保存好即可

2.把公開金鑰放到主機
2.1 接下來把剛剛產生好的這三對公開金鑰(*.pub)放到要登入的主機上

[root@localhost ~]#  scp ~/.ssh/*.pub 你欲登入的主機:~/.ssh/

2.2 登入該系統,因為還沒有設定好所以目前還是需要輸入帳號密碼

[root@localhost ~]#  ssh 欲登入的主機

2.3 預設放置公鑰的檔案是【 authorized_keys】,因此要把剛剛產生的三個公鑰的資料都加到這個檔案中。

[root@localhost ~]#  cat ~/.ssh/identity.pub >> ~/.ssh/authorized_keys
[root@localhost ~]#  cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
[root@localhost ~]#  cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
[root@localhost ~]#  chmod 711 ~/.ssh
[root@localhost ~]#  chmod 644 ~/.ssh/authorized_keys

都設定完成後,可以再重新登入到該主機上,此時應該會出現Passphrase要你輸入密碼
PS : 如果在產生金鑰的時後你沒有輸入Passphrase,而直接按Enter跳過的話,到此步驟就已經設定完成。若有設定Passphrase還請繼續參考下面的設定

  1. 設定ssh-agent
    如果每次登入都要輸入Passphrase,真的很麻煩。因此可使用ssh-agent來改善此問題。
    3.1 首先啟動ssh-agent

    [root@localhost ~]# eval ssh-agent -s

3.2 將私鑰給ssh-agent來保管
使用ssh-add來指定金鑰。
再來輸入產生金鑰時所設定的Passphrase。三個金鑰都一樣的話只要輸入一次就可以了

[root@localhost ~]#  ssh-add
Enter passphrase for /root/.ssh/id_rsa:  #輸入Passphrase密碼
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
Identity added: /root/.ssh/id_dsa (/root/.ssh/id_dsa)
Identity added: /root/.ssh/identity (root@localhost.localdomain)
You have new mail in /var/spool/mail/root

再來就可試試看登入主機,應該就可以不用密碼就可登入了。
但是每次進入Shell時都要重做設定ssh-agent,很麻煩又容易出問題。因此可參考下方的script來自動設定

3.3 此script是( Joseph M. Reagle Jr )
<http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html >
把此script放到/root/.bash_profile裡面就可以了,在每次登入Shell時就會主動詢問一次Passphrase的密碼,之後就可以直接登入。

[root@localhost .ssh]#  vi /root/.bash_profile
SSH_ENV=$HOME/.ssh/environment
function start_agent {
     echo "Initialising new SSH agent..."
     /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
     echo succeeded
     chmod 600 ${SSH_ENV}
     . ${SSH_ENV} > /dev/null
     /usr/bin/ssh-add;
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
     . ${SSH_ENV} > /dev/null
     #ps ${SSH_AGENT_PID} doesn't work under cywgin
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
         start_agent;
     }
else
     start_agent;
fi

全文同步於FAQ-BOOK
IT鐵人文章分享


上一篇
Cygwin安裝及SSH設定教學(下)
下一篇
Windows Server 2003 SSL憑證安裝教學
系列文
關於IT資訊界的筆記和學習紀錄41
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言