SSH密钥登录 | OpenSSH <--> SSH2
一、SSH是什么?
Secure Shell(缩写为SSH),由IETF的网络工作小组(Network Working Group)所制定;SSH为一项创建在应用层和传输层基础上的安全协议,为计算机上的Shell(壳层)提供安全的传输和使用环境。
因为之前公司用的是SuSE(SSH2),现在用的是CentOS(OpenSSH),所以要通过密钥相互登录就有点麻烦了。
二、SSH密钥使用
由于之前所说的原因,会出现一种蛋疼的情况,有些公司还喜欢使用SSH2版本的SSH服务,SSH2和OpenSSH的加密算法是完全不一样的,他们所使用的的密钥对也不兼容,所以会出现下面4种组合
1、OpenSSH客户端对OpenSSH服务器端
2、SSH2客户端对SSH2服务器端
3、OpenSSH客户端对SSH2服务器端
4、SSH2客户端对OpenSSH服务器端
OpenSSH:10.1.1.1
SSH2:10.2.2.2
1、OpenSSH客户端对OpenSSH服务器端
在客户端(OpenSSH)生成私钥(默认保存在~/.ssh/id_rsa)
# ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): ...
将公钥传给服务器端(OpenSSH)(默认保存在~/.ssh/authorized_keys)
# ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 36000 root@10.1.1.1"Now try logging into the machine, with "ssh '-p 36000 root@10.1.1.1'", and check in: .ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.
验证是否成功
# ssh -p36000 root@10.1.1.1Last login: Mon Dec 23 15:19:55 2013 from 10.2.68.36Welcome to tlinux 1.2 64bitVersion 1.2 20130715
2、SSH2客户端对SSH2服务器端
在客户端(SSH2)生成私钥(默认保存在~/.ssh2/id_rsa)
# ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh2/id_rsa): ...
在客户端(SSH2)将公钥传给服务器端(SSH2)
# scp -P 36000 ~/.ssh/id_rsa_2048_a.pub root@10.2.2.2:~/.ssh2/
在服务器端(SSH2)添加公钥
# echo "Key id_rsa_2048_a.pub" >> ~/.ssh2/authorization
在客户端(SSH2)添加私钥
# echo "IdKey id_rsa_2048_a" >> ~/.ssh2/identification
在客户端(SSH2)添加私钥
3、OpenSSH客户端对SSH2服务器端
在客户端(OpenSSH)生成私钥(默认保存在~/.ssh/id_rsa)
# ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): ...
在客户端(OpenSSH)将公钥转换SSH2格式并传给服务器端
# ssh-keygen -e -f ~/.ssh/id_rsa.pub > ~/.ssh/id_rsa_ssh2.pub# scp -P 36000 ~/.ssh/id_rsa_ssh2.pub root@10.2.2.2:~/.ssh2/
在服务器端(SSH2)添加公钥内容
# echo "Key id_rsa_ssh2.pub" >> ~/.ssh2/authorization
验证是否成功
# ssh -p36000 root@10.2.2.2Last login: Mon Dec 23 2013 14:49:15 +0800 from 10.2.2.2Welcome to TENCENT SuSE Linux 10 SP2 64Bit May 29 2012 by DISVersion v3.0.20120529
4、SSH2客户端对OpenSSH服务器端
在客户端(SSH2)生成私钥(默认保存在~/.ssh2/id_rsa)
# ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh2/id_rsa): ...
在客户端(SSH2)将公钥传给服务器端(OpenSSH),并在服务器端将公钥转换OpenSSH格式
# scp -P 36000 ~/.ssh/id_rsa_2048_a.pub root@10.1.1.1:~/.ssh/# ssh-keygen -i -f ~/.ssh/id_rsa_2048_a.pub > ~/.ssh/authorized_keys
在客户端添加公钥内容
# echo "IdKey id_rsa_2048_a" >> ~/.ssh2/identification
验证是否成功
# ssh -p36000 root@10.1.1.1Authentication successful.Last login: Thu Dec 26 16:47:07 2013 from 10.2.2.2