ssh免密登录

如果A机器到B机器需要免密登录,以下流程

1.A机器执行ssh-keygen -t rsa生成公钥文件id_rsa.pub

 authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥(初始不存在该文件) *
* id_rsa : 生成的私钥文件 *
* id_rsa.pub : 生成的公钥文件*
* know_hosts : 已知的主机公钥清单*

初次连接对端服务器,系统会提示你让你确认,如果是yes,那系统就会再know_hosts里追加这条访问记录,下次再次连接的时候就不会在提示用户。当然如果对端机器密钥发送变更,系统还是会提示你让你确认。/etc/ssh/ssh_host_rsa_key.pub服务器的公钥会登录到client的know_hosts文件里


密码登录认证原理:

client客户机向server发送登录请求,server服务器将自己的公钥发送给client(客户机)。

client使用这个公钥,将公钥进行加密,然后再发送给server。

server用私钥解密登录密码,验证合法性。

server返回验证结果为client。

这个流程中存有的一个问题,怎么保证收到的公钥就是目标server的公钥?

密码登录时需要注意:中间人攻击,所以如果一个攻击者中途拦截了client的登录请求,发送自己的公钥给client,client端就会用攻击者的公钥进行数据加密,攻击者接收到信息之后,用自己的私钥就可以进行解密了,这就窃取了client的登录信息了。

为了解决这个问题,client端第一次登录的时候,会进行一个登录公钥确认。

如果服务器重启了,就会删掉know_hosts:rm -rf know_hosts

查看公钥指纹:

ssh-keygen-if  /etc/ssh/ssh_host_rsa_key.pub
server主机的pubkey保存在/etc/ssh目录下,默认使用ssh_host_ecdsa_key.pub

[root@mysql-rpm ssh]# cd /etc/ssh
[root@mysql-rpm ssh]# ls
moduli      sshd_config         ssh_host_ecdsa_key.pub  ssh_host_ed25519_key.pub  ssh_host_rsa_key.pub
ssh_config  ssh_host_ecdsa_key  ssh_host_ed25519_key    ssh_host_rsa_key

2 将A机器的公钥文件拷贝到B机器,用ssh-copy-id命令,会将A机器的公钥拷贝到B机器的authorized_keys文件里

ssh-copy-id -i ~/.ssh/id_rsa.pub [romte_ip]

3 测试A机器到B机器免密登录是否成功

ssh user@romte_ip

如果私钥的名字不叫默认的id_rsa,你需要指定私钥文件用-i

ssh -i 私钥文件 user@romte_ip

    -i identity_file
             Selects a file from which the identity (private key) for public
             key authentication is read.  The default is ~/.ssh/id_dsa,
             ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519 and ~/.ssh/id_rsa.  Identity
             files may also be specified on a per-host basis in the configura‐
             tion file.  It is possible to have multiple -i options (and mul‐
             tiple identities specified in configuration files).  If no cer‐
             tificates have been explicitly specified by the CertificateFile
             directive, ssh will also try to load certificate information from
             the filename obtained by appending -cert.pub to identity file‐
             names.

查看某个用户的密码状态
#chage -l  root
Last password change : Oct 23, 2013       【最近一次修改密码的时间】
Password expires : Jan 23, 2014              【密码过期时间】
Password inactive : never                       【密码失效时间】
Account expires : never                         【账户过期时间】
Minimum number of days between password change : 0    【两次改变密码之间相距最小天数】
Maximum number of days between password change : 92   【两次密码改变密码相距最大天数】
Number of days of warning before password expires : 7 【密码过期前开始警告的天数】

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值