2011年6月11日 星期六

ssh without password


使用ssh協定,要登入遠方主機或拷貝遠方檔案時,有免去輸入帳密方法,如下:
原理: 將自己的公開金鑰(localhost:~/.ssh/*.pub)加入
遠方主機的授權金鑰檔內(remotehost:~/.ssh/authorized_keys)

工具: 可利用以下reg_ssh_key.sh批次檔作登入及加入動作.
以後,利用終端機ssh remotehost或scp file remotehost:時,就可以免問帳密.

方法: localhost> vi reg_ssh_key.sh # 建立註冊批次檔,user為遠端主機帳號
cat ~/.ssh/*.pub | ssh user@$* "cat >> .ssh/authorized_keys"

localhost> sh reg_ssh_key.sh remotehost # 利用註冊批次檔,向遠方主機remotehost註冊
金鑰
Password:

localhost> ssh remotehost # 測試ssh自動登入遠方主機,不問密碼

remotehost>

註:
1.遠方主機的檔案目錄必需設定權限如下:
remotehost:~/.ssh 目錄權限為711,即己/群/他權限分別為rwx/x/x,
remotehost:~/.ssh/authorized_keys 檔案權限為644,即己/群/他權限分別為rw/r/r,

2.個人金鑰生成法:
若有ssh-user-config指令,以問答方式產生個人金鑰,
若無該指令,可用ssh-keygen指令產生

3.本地/遠方所需ssh組態檔如下,
客戶端: /etc/ssh_config 或 /etc/ssh/ssh_config
伺服端: /etc/sshd_config 或 /etc/ssh/sshd_config

------------------ reg_ssh_key.sh ----------------------
#
# http://josephjiang.com/article/understand-ssh-key/
#
# run to get /etc/ssh_config, /etc/sshd_config
# ssh-host-config
# run to get ~/.ssh/identify.pub, id_rsa.pub, id_dsa.pub
# ssh-user-config
# #ssh-keygen -t rsa1
# #ssh-keygen -t rsa
# #ssh-keygen -t dsa
# chmod 711 remote:~/.ssh
# chmod 644 remote:~/.ssh/authorized_keys
#
cat ~/.ssh/*.pub | ssh user@$* "cat >> .ssh/authorized_keys"

沒有留言: