配置ssh信任实现服务器间可信登录

ssh,即Secure Shell,它是把所有传输的数据都进行了加密。它有两个版本,即1.x和2.x,其中只有Open SSh是免费的。从客户端来看,ssh有两种级别的安全验证,一是口令验证(只要你知道账户和密码,就能登录,并且所传输的数据都是加密的),二是密匙验证(自己为自己创建一对密匙,然后把公用密匙放到服务器上;如果你连接服务器的时候,客户端首先发送一个请求,里面包括公用密匙,请求服务器进行验证;服务器接收到请求之后,就会在自己的家目录下寻找你的密匙,与你发送过来的密匙进行比较,如果相同,服务器就会把自己的“质询”信息进行加密,发送给客户端;客户端接收到“质询”信息之后,就会用自己的私人密匙进行解密,然后把它发送给服务器)。

对于需要经常ssh连接的服务器之间,每次连接都需要输入密码。而有些备份的计划任务需要ssh存储到备份服务器,因此我们需要配置服务器之间使用秘钥验证的方式ssh登录。

当客户端连接远程服务器时,会在客户端当前用户的家目录下生成.ssh/known_hosts文件,把服务器的身份信息保存下来。下一次客户端再连接时,就不再询问是否继续(yes/no)?

使用密钥对方式连接时,客户端生成的密钥是用户家目录下.ssh/id_rsa和.ssh/id_rsa.pub,上传时是将id_rsa.pub写入到服务器相关用户家目录下.ssh/authorized_keys文件中

服务端IP:192.168.1.113   客户端IP:192.168.1.112

修改服务器和客户端ssh配置文件,修改连接端口号,禁用远程root连接,设置最大连接失败次数。

[root@server ~]# vim /etc/ssh/sshd_config 
Port 1022 //修改端口号,修改后客户端登陆ssh  -p 1022 IP地址
PermitRootLogin no  不允许root用户以ssh方式登陆
MaxAuthTries  6 //每次连接允许认证登录的最多次数,若超过此               //次数仍未登录成功,服务器将主动断开连接
重启服务
[root@server ~]# /etc/init.d/sshd restart
停止 sshd :                                   [确定]
正在启动 sshd:                                [确定]

客户端版本为centos7.0

[root@client ~]# vim /etc/ssh/sshd_config 
Port 1022
PermitRootLogin no
MaxAuthTries  6 
[root@client ~]# /bin/systemctl restart sshd.service

服务器和客户端添加用户

[root@server ~]# useradd lucy
[root@server ~]# passwd lucy
更改用户 lucy 的密码 。
新的 密码:
无效的密码: 过于简单化/系统化
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[root@client ~]# useradd lucy
[root@client ~]# passwd lucy 
更改用户 lucy 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
重新生成ssh文件

 

重新生成ssh文件

[root@server ~]# rm  -f  /etc/ssh/ssh_host_*
[root@server ~]# service sshd restart
停止 sshd:                                       [失败]
生成 SSH1 RSA 主机键:                           [确定]
生成 SSH2 RSA 主机键:                           [确定]
正在生成 SSH2 DSA 主机键:                       [确定]
正在启动 sshd:                                   [确定]

客户端连接服务器

[root@client ~]# ssh -p 1022 lucy@192.168.1.113
The authenticity of host '[192.168.1.113]:1022 
([192.168.1.113]:1022)' can't be established.
RSA key fingerprint is 
50:f0:09:3c:84:8e:8d:04:fd:c1:90:d7:7d:89:46:47.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.1.113]:1022' (RSA) to the list of known hosts.
lucy@192.168.1.113's password: 
[lucy@server ~]$ exit
logout
Connection to 192.168.1.113 closed.

 

客户端生成秘钥对

[root@client ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
                        //输入密码,该密码是保护私钥的密码
Enter same passphrase again: 
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:
e6:4c:f1:dc:59:e6:0d:fb:19:a6:f0:e6:50:50:dd:73 root@client
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
|             .. .|
|            .  oE|
|        .  .  + o|
|         + ..= + |
|        S o.o.oo.|
|       =    + o.o|
|        o  . + ..|
|            +    |
|             .   |
+-----------------+

 

加密算法,此处使用的为非对称算法RSA

对称加密:加解密使用相同的密钥。算法:DES / 3DES / AES。优点,加密效率高。缺点,密钥的传输和保管不方便。

非对称加密:公钥加密、私钥解密。算法:RSA。优点,密钥传输保管方便。缺点,加密效率低。

单向加密:加密只能向一个方向进行,不能回推。哈希算法。MD5、SHA

上传公钥至服务器

7.0以前的版本ssh-copy-id不支持-p选项,7.0版本更改。7.0以前版本可使用如下命令。也可以直接将~/.ssh/id_rsa.pub文件拷贝至服务器user用户家目录下authorized_keys文件,本次试验中即

/home/lucy/.ssh/authorized_keys

[root@client ~]#ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 1022 lucy@192.168.1.113" 
7.0版本可直接使用-p选项。
[root@client ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub -p 1022 lucy@192.168.1.113
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
lucy@192.168.1.113's password: 
 
Number of key(s) added: 1
 
Now try logging into the machine, with:   "ssh -p '1022' 'lucy@192.168.1.113'"
and check to make sure that only the key(s) you wanted were added.

再次ssh服务器时,提示输入密码,此处密码为保护私钥的密码。

[root@client ~]# ssh -p 1022 lucy@192.168.1.113
Agent admitted failure to sign using the key.
lucy@192.168.1.113's password: 
Last login: Wed Jul 16 10:06:48 2014 from 192.168.1.112
[lucy@server ~]$ exit
logout
Connection to 192.168.1.113 closed.

把私钥密码托管给agent,再次连接时就不需要输入密码。

[root@client ~]# ssh-add 
Enter passphrase for /root/.ssh/id_rsa: 
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
[root@client ~]# ssh -p 1022 lucy@192.168.1.113
Last login: Wed Jul 16 10:07:24 2014 from 192.168.1.112
[lucy@server ~]$ exit
logout
Connection to 192.168.1.113 closed.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值