基于秘钥方式连接过程(原理)
1. 客户端(管理端) 执行命令创建秘钥对
2. 客户端(管理端) 建立远程连接(口令),发送公钥信息
3. 客户端(管理端) 再次建立远程连接
4. 服务端(被管理端) 发送公钥质询信息(你要是能打开我的锁头吗)
5. 客户端(管理端) 处理公钥质询信息(钥匙将锁头打开),将质询结果返回给服务端
6. 服务端(被管理端) 接收到质询结果,建立好远程连接
public key 公钥 (帮助理解公钥就是锁,私钥就是钥匙)
private ket 私钥
在客户端(管理端)建立密钥对,自己保留私钥,把公钥传给要控制的服务器
生成密钥对 客户端操作
ssh-keygen -t dsa (-t后面是加密算法 默认rsa)
/root/.ssh/id_dsa #私钥保存路径
/root/.ssh/id_dsa.pub #公钥保存路径
cd /root/.ssh/
ssh-copy-id -i id_dsa.pub root@10.125.192.6 #将公钥传到别的机器上(这个命令只能拷公钥)
/root/.ssh/authorized_key #公钥在上传后保存路径和名称
如果ssh默认端口不是22端口
ssh-copy-id -i id_dsa.pub "-p 21987 root@10.125.192.6"
ssh-copy-id -i id_dsa.pub -p 21987 "root@10.125.192.6"
sshkey不需要交互模式生成密钥对
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1