scp和ssh公匙认证 双机信任

 

 

 

 

=============================== 1 Linux下不出现提示使用远程拷贝

 

实现:从75scp至76

 

 

ssh到75上:

[root@localhost /]# ssh-keygen -t rsa

 

  Enter file in which to save the key (/home/ludy/.ssh/id_rsa):   //直接输入回车
    Enter passphrase (empty for no passphrase):                         //直接输入回车
    Enter same passphrase again:                                                //直接输入回车

 

 说明:
该命令将在用户的主目录/.ssh目录下面产生一对密钥
一般采用的ssh的rsa密钥:
id_rsa     私钥
id_rsa.pub 公钥
下述命令产生不同类型的密钥
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1


[root@localhost /]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.76

 

等同于 scp /root/.ssh/id_rsa.pub root@172.16.142.5:/root/.ssh/authorized_keys

说明:
将公钥拷贝到远端主机,并写入授权列表文件
你也可以把公钥文件拷贝过去后,在远端主机下直接执行
touch /root/.ssh/authorized_keys
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

 


//需要手动ssh连接一次,才能认证生效(无需输入密码)
[root@localhost /]#ssh 192.168.1.76

 

//无需手动输入密码
[root@localhost /]# scp -rp /tmp/t.txt root@192.168.1.76:/temp

 

 

***以上方法可以实现A B C三台服务器之间多机公匙认证***

 

 

 

 ======================================2  ssh全自动实现双机信任脚本

 

 其实ssh的双机无密码登陆无非就是实现密钥验证而已

其实密钥登陆也就是用自己的私钥去验证在目的主机上是否有相对应的公钥
1.首先主机会获取自己家目录下的.ssh目录下的id_rsa私钥文件
2.然后主机会去查找目的主机在其加目录下是否有authorized_keys
3.如果有的话会用自己的私钥来解密看是否是自己的公钥 如果是则登陆成功
4.否则启动密码验证。

实验前设置好sshd的配置文件将会很大提高ssh的链接速度
首先要确保ssh开启了密钥验证(默认开启)我么也可以通过
#RSAAuthentication yes
##PubkeyAuthentication yes
#AuthorizedKeysFile     .ssh/authorized_keys
主要是通过这三个选项

是否开启密码验证
PasswordAuthentication yes

关闭dns解析会加快ssh连接的速度
UseDNS no

实验环境:centos5.4 x86

脚本使用的ssh用户都为root 大家都自行修改

脚本如下:

 

    #!/usr/bin/expect
    #2013-01-18
    #author zhangyifei
    #blog http://zyfforlinux.blog.51cto.com
    set local_passwd "server"
    set des_passwd "server"
    set timeout 10
    set localip "192.168.0.254"
    set desip "192.168.0.251"
    spawn ssh-keygen -t rsa
    expect "Enter file*:" {send "\r"}
    expect "Enter passphrase*" {send "\r"}
    expect "Enter same*" {send "\r";exp_continue}
    spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$desip
    expect {
    "yes/no" { send "yes\r";exp_continue}
    "password:" {send "$des_passwd\r";exp_continue}
    }
    
    spawn ssh $desip "ssh-keygen -t rsa"
    expect "Enter file*:" {send "\r"}
    expect "Enter passphrase*" {send "\r"}
    expect "Enter same*" {send "\r";exp_continue}
    
    spawn scp $desip:/root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
    expect {
    "yes/no" { send "yes\r";exp_continue}
    "password:" {send "$local_passwd\r";exp_continue}
    }

此脚本我已经测试成功大家可以放心使用,脚本原理很简单,无法就是借助了expect来实现自动化而已。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值