现有三台机器,仅使用密钥对方式登录,不使用口令
server :服务器 50
client1:客户机1 51
1.生成密钥对,将公钥传给server,可以直接免密登录server。
2.将私钥传给client2,使client2可以免密登录server。
(实际工作就是使用这个方式把私钥生成,随身携带,方便使用其他计算机远程服务器)
client2:客户机2 52
一 生成密钥对,将公钥传给server,可以直接免密登录server。
[root@client1 ~]# ssh-keygen (回车即可) //生成密钥对
[root@client1 ~]# ls /root/.ssh/ //产生以下文件
id_rsa id_rsa.pub known_hosts
[root@client1 ~]# ssh-copy-id root@192.168.4.50 //将公钥传给server
[root@server ~]# ls /root/.ssh/ //在server上查看
authorized_keys
[root@server ~]# vim /etc/ssh/sshd_config
AuthorizedKeysFile .ssh/authorized_keys //开启密钥对认证
PasswordAuthentication no //关闭口令认证
[root@server ~]# systemctl restart sshd //修改配置文件后,重启服务,使其生效
[root@client1 ~]# ssh -X root@192.168.4.50 //登录server
Last login: Wed Mar 6 16:36:45 2019 from 192.168.4.51
[root@server ~]#
二 将私钥传给client2,使client2可以免密登录server。
[root@client1 ~]# scp /root/.ssh/id_rsa root@192.168.4.52:/root/.ssh/
root@192.168.4.52's password:
id_rsa 100% 1679 1.7MB/s 00:00
[root@client2 ~]# ls /root/.ssh/
id_rsa
[root@client2 ~]# ssh -X root@192.168.4.50
Last login: Wed Mar 6 16:36:45 2019 from 192.168.4.52
[root@server ~]#
出现问题1:
[root@client1 ~]# ssh -X root@192.168.4.50
sign_and_send_pubkey: signing failed: agent refused operation
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
解决:
[root@client1 ~]# eval ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-9eMPmnamPBIP/agent.4246; export
SSH_AUTH_SOCK;SSH_AGENT_PID=4247; export
SSH_AGENT_PID;echo Agent pid 4247;
[root@client1 ~]# ssh-add
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
出现问题2:
[root@client2 ~]# ssh -X root@192.168.4.50
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
解决:
[root@client2 ~]# rm -rf /root/.ssh/*
[root@client1 ~]# scp /root/.ssh/id_rsa root@192.168.4.52:/root/.ssh/ //重新传私钥
root@192.168.4.51's password:
id_rsa 100% 1679 140.9KB/s 00:00