用公共key实现无密码ssh

一、在主服务器master下生成密钥css

$ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsanode

二、将密钥放在 ~/.ssh/authorized_keysvim

$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keysbash

三、对生成的文件authorized_keys受权服务器

  chmod 600 ~/.ssh/authorized_keysssh

四、编辑sshd_config文件,将下面三列#去掉测试

 vim /etc/ssh/sshd_configspa

 RSAAuthentication yes.net

PubkeyAuthentication yesrest

AuthorizedKeysFile     .ssh/authorized_keys

五、重启sshd 服务(可省略)

   service  sshd  restart

六、验证无验证登录

ssh localhost

7  配置master无密钥登录slave(以此能够配置master无密钥登录slaveX

注释:root为root用户,master为主机名

 

[root@slaver ~]# scp -r root@master :/root/.ssh/id_dsa.pub /root/.ssh/slaver.pub

 [root@slaver ~]# cat ~/.ssh/slaver.pub >> ~/.ssh/authorized_keys

chmod 600~/.ssh/authorized_keys

vim /etc/ssh/sshd_config

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile     .ssh/authorized_keys

service sshd restart

 

 

 

 

 

 

更好的自动ssh登陆,用公共key实现无密码ssh

解决~/.ssh/known_hosts 过时问题。
bash + expect

bash:ssh.sh

  1. #!/bin/bash
  2. help(){
  3.     echo "usage: [password]"
  4. }
  5. -z "$1" ] &{ help; exit 1|{
  6.     case $1 in
  7.         -*) help; exit ;;
  8.         *) ip=$1;;
  9.     esac
  10. }
  11. shift
  12. ssh.exp "$ip" "$@"
  13. [ "$?" = "1" ] && ssh-keygen -R $ip && ssh_ivie.exp "$ip" "$@"


expec
ssh.exp

  1. #!/usr/bin/expect -f
  2. proc help {{
  3. puts {usage<ivie_ip[password]}
  4. }
  5. if {$argc<1{ help ; exit}
  6. set ip [ lindex $argv 0 ]
  7. set password rootroot
  8. if {$argc==2{ set password [lindex $argv 1] }
  9. # close the output
  10. log_user 0
  11. set timeout 30
  12. spawn ssh -XY root@$ip
  13. expect {
  14.     -re ".*:~ # " {}
  15.     "Password: " { send "$password\r" }
  16.     "(yes/no)?" {send "yes\r"; exp_continue}
  17.     "Host key verification failed" { send_user "run: ssh-keygen -R $ip" ; exit 1 }
  18.     timeout {puts "check your ip: $ip"; exit 2}
  19.     eof { puts "check your ip: $ip" ;exit 3 }
  20. }
  21. interact


我测试过在一个expect中完成,可是没有成功。还望有谁可以完成,给予指教。

 

 

用公共key实现无密码ssh
在HPC上,从server登录到各个node都须要输密码,很麻烦,能不能不输密码呢。

须要用到的技术就是ssh中public key的authorized。 具体作法分以下几步:

用ssh-keygen生成一个publc key; ssh-keygen -t rsa 中间会提醒输入id_rsa文件的位置和pass-phrase,能够一路回车。id_rsa在~/.ssh/中产生两个文件id_rsa和id_rsa.pub,一个是私有密钥,一个是公共密钥。 因为HPC中共享硬盘,直接将其中的public key(通常默认为~/.ssh/id_rsa.pub)在复制一份并命名为authorized_keys 若是运气好,作完第二步就能够实现不输入密码登录了。不过实际实施过程当中,还有不少种发生意外的可能(经亲身验证)。尤为须要注意的问题是各个文件/文件夹权限的问题。 合理的权限列表以下: id_rsa 600 id_rsa.pub 644 ~/.ssh/ 755 同时要注意的就是,$HOME,也就是~不能是777的权限,在这个问题上卡了三四个小时啊。
相关文章
相关标签/搜索