在Linux系統營運中,我們可能會遇到這樣的問題:當我們用用戶端PUTTY利用key驗證分別登入到主機A,主機B,主機C的時候,都不用輸入密碼,直接由key驗證可以成功登入,那麼我想從主機A登入到主機B,再從主機B登入到主機C的時候,要怎麼做才能實現直接用key驗證,而不用被要求輸入密碼呢?
這個時候我們需要在主機A和PUTTY上開啟key轉寄功能,見:
650) this.width=650;" src="http://img1.51cto.com/attachment/201310/213922785.png" title="1.png" alt="213922785.png" />
鑰匙產生和分發
1.在主機A上產生金鑰組
[root@ws128 ~]# ssh-keygen預設演算法為RSA,2048位)
Generating public/privatersa key pair.
Enter file in which tosave the key (/root/.ssh/id_rsa):
Enter passphrase (emptyfor no passphrase):輸入私密金鑰密碼)
Enter same passphraseagain:
Your identification hasbeen saved in /root/.ssh/id_rsa.
Your public key has beensaved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
c7:67:a8:4e:1b:e6:a2:43:ef:59:a0:10:48:67:f8:f2root@ws128.example.com
2.將產生的公開金鑰分別安裝到主機B、主機C上
[root@ws128 ~]# ssh-copy-id -i .ssh/id_rsa.pubroot@192.168.80.129
[root@ws128 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.80.130
3.將私密金鑰檔案拷貝到終端,由於Linux的ssh-keygen產生的私密金鑰檔案PUTTY不識別,所以要用pttygen軟體做一次轉換,開啟puttygen軟體匯入私密金鑰,然後另存新檔PPK格式。
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131227/2034091W1-1.png" title="2.png" alt="214321542.png" />
私密金鑰轉寄配置
1.PUTTY配置,開啟私密金鑰轉寄,指定私密金鑰檔案,
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131227/2034092360-2.png" title="3.png" alt="214544564.png" />
2.在主機A上開啟SSH代理
[root@ws128 .ssh]# eval`ssh-agent`
Agent pid 3850
[root@ws128 .ssh]# ssh-add
Enter passphrase for/root/.ssh/id_rsa:
Identity added: /root/.ssh/id_rsa(/root/.ssh/id_rsa)
指令碼實現
Vim /root/sshagent.sh
#!/usr/bin/expect
spawn ssh-add
expect "passphrase"
send "redhat\r"
expect eof
exit
Vim /root/.bashrc
Eval `ssh-agent`
/root/sshagent.sh
3.在主機A上設定SSH代理轉寄
[root@ws128 .ssh]# vim /etc/ssh/ssh_config
ForwardAgent yes
ForwardX11 yes
驗證
1.在主機A上用SSH登入到主機B,然後在主機B上看能不能不用密碼直接登入到主機C,如果可以,則配置成功。
本文出自 “清風明月” 部落格,請務必保留此出處http://coolsky.blog.51cto.com/177347/1304181