2024年1月20日发(作者:)

SSH公钥密钥的使用(整理版)

一、在Linux客户端使用SSH管理远程Linux主机

1.在客户机上生成密钥

[root@rain ~]# ssh-keygen -t rsa

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_.

The key fingerprint is:

4d:dd:48:af:76:c2:ba:a8:bc:20:f3:28:1d:6a:28:53 root@

2.将公钥文件复制到要登录的SSH服务器的用户主目录的.ssh目录中

3、将/root/.ssh/id_改名为/root/.ssh/authorized_keys

mv /root/.ssh/id_ /root/.ssh/authorized_keys

4、将私钥id_rsa拷贝到客户机

A:linux下可以拷贝到/root/.ssh/就可以直接使用了。

二、SecureCRT使用密钥登录SSH服务器

1.使用SecureCRT创建私钥和公钥.

SecureCRT: Quick Connect -> Authentiation -> Public Key -> Properties ->

Create Identity File -> DSA/RSA -> Set Passphrase ->

Done

这个时候在指定目录会生成两个文件,例如,私钥my_rsa和公钥my_

服务器上建立.ssh目录,一般情况下,已经有这个目录

mkdir /root/.ssh

chmod 700 /root/.ssh

3.将公钥 my_ 传到linux服务器,将SSH2兼容格式的公钥转换成为Openssh兼容格式

#ssh-keygen -X -f >> authorized_keys2

此方法同样可用来转换ssh-cleint 客户端生成的Key文件

4.在SecureCRT里面设置登录模式为PublicKey,并选择刚刚创建的my_rsa文件作为私钥

5.重启Linux服务器上SSH服务器

#service sshd restart 或者 /etc/rc.d/init.d/sshd restart

6.由于已经设置了密钥登录,原来的密码登录就完全可以去掉

# vi /etc/ssh/sshd_config

Protocol 2 /仅允许使用SSH2

PubkeyAuthentication yes /*启用PublicKey认证

AuthorizedKeysFile .ssh/authorized_keys2 /*PublicKey文件路径

PasswordAuthentication no /*禁止密码验证登录

PS:以上步骤是使用SecureCRT生成的密钥对来进行登录验证的,其实也可以在服务器上使用ssh-keygen命令生成的密钥,同样在生成密钥对之

后,将格式转换成SecureCRT 使用的SSH2格式

三、SSH Secure Shell下使用密钥方式登录ssh

1.先用自已的用户登录到服务器,比如我用 uplinux 登录到服务器

2.运行 SSH Secure Shell 工具中的“Secure Shell Client ”,选择菜单中“Edit”-> “Setting”,在打开的窗口左侧依次选择“Global

Settings”->“User Authentication”->“Keys”

3.点击“Generate New”,在打开的窗口中点击“Next”,选择 “RSA”和

“2048”,然后点击“Next”,稍等一会,会计算一个密钥。

4.上传公钥

5.$ ssh-keygen -i -f uplinux_ >> authorized_keys

四、有关SSH的密匙生成问题

key方式各个客户端有点不兼容,但是服务器端是一样的。一般应当使用客户端生成用户key,然后把public key弄到服务器上。如果是服务器

上是openssh,目录是 .ssh,如果是ssh2,目录是.ssh2。

windows里面推荐使用ssh client,比较兼容。利用ssh client生成user key,然后用upload的选项上载到服务器上(前提是已经登录到服务器

上),注意把.ssh2的目标目录改为.ssh(针对openssh的服务器,如果服务器是ssh2,什么都不用改)。

上载以后在.ssh的目录里面执行

ssh-keygen -X -f >> authorized_keys2

是你的公钥的文件名,就是刚刚被ssh client上载上去的那个。

然后就可以用key pair的方式登录了。如果直接用root,记得看看

sshd_config

里面的 PermetRootLogin 选项改成 no

就是说禁止root用密码的方式远程登录,只能使用key pair。