ssh密钥登录

一、生成密钥对(两种方式)并配置

方式1:使用ssh-keygen(1)生成并配置

(1)生成密钥对

[root@iZwz9catu2mrq92b07d1d0Z ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.

//输入文件名称
Enter file in which to save the key (/root/.ssh/id_rsa): keys_root   
//输入私钥加密密码
Enter passphrase (empty for no passphrase):    
//再次输入密码                       
Enter same passphrase again:                            
              
Your identification has been saved in keys_root.
Your public key has been saved in keys_root.pub.
The key fingerprint is:
f4:f9:70:51:cf:09:f6:da:30:6a:b1:67:4f:dc:14:44 root@iZwz9catu2mrq92b07d1d0Z
The key's randomart image is:
+--[ RSA 2048]----+
|             o+E |
|            ..ooo|
|        .  ..o o+|
|       . . .+.*..|
|        S ++.+ +.|
|          .+o o  |
|            .  . |
|                 |
|                 |
+-----------------+

//文件生成成功,keys_root为私钥,keys_root.pub为公钥
[root@iZwz9catu2mrq92b07d1d0Z ~]# ls
keys_root  keys_root.pub    

(2)配置公钥

[root@iZwz9catu2mrq92b07d1d0Z ~]# echo -e '#this is keys_root' >> ~/.ssh/authorized_keys ; cat ~/keys_root.pub >> ~/.ssh/authorized_keys   
[root@iZwz9catu2mrq92b07d1d0Z ~]# cat ~/.ssh/authorized_keys
#this is keys_root
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyhp9SBxas8Nmwdi4dQfOuUULpMGRnGEFopU2DXhSF+PE/s80xrVS31Ycd5o4gU3iehKx2vo4OEB2lYZ2JCfptTc59HAj+Qwqh7i5S4YQuX/+31GkY+s8XKFR4QgH1ubQt9feU2cagfG1f+wWRsa0YtefE67Kjv6OZuKuA2bOdrAH4mzV1m71iLMUZYgaEnfJExXj2lbPAXRqCV+tdIj9h0jxhB5pQXsZ3NE38D22WYNKO4Sy8odfE7Oby1I0Emm8Uhiwqgx91HP22iY/WqzZOxeKZPF17CPWr9cChaPh9/DXM1Wd8KDCg33MO6hbpqAwh7iEughndXly0FY0oZNKnQ== root@iZwz9catu2mrq92b07d1d0Z

(3)配置私钥

  1. 下载私钥到本地机器
    [root@iZwz9catu2mrq92b07d1d0Z ~]# sz keys_root   
    
  2. 启动Xshell
  3. 工具(Tools)
  4. 用户密钥管理者(User Key Manager)
  5. 导入已下载的 keys_root 文件(Import)
  6. 配置完成

方式2:使用Xshell生成并配置

  1. 启动Xshell
  2. 工具
  3. 新建用户密钥生成向导(New User Key Wizard)
  4. 按步骤选择下一步
  5. 输入密钥名称和私钥密码后选择下一步
  6. 密钥对生成成功,此时私钥已自动导入到Xshell中,需要我们手动保存公钥到本地机器
  7. 上传已保存的公钥文件到服务器并配置在用户的authorized_keys文件中
     

二、通过密钥登录服务器

  1. 新建会话,填写名称、主机等信息
  2. 选择左侧栏目用户身份验证,右侧方法选择Public Key,填写用户名,选择对应的用户密钥并填写密钥的密码,点击确定
  3. 连接会话

 

附录

[root@iZwz9catu2mrq92b07d1d0Z ~]# man ssh

1.ssh-keygen命令介绍

The user creates his/her key pair by running ssh-keygen(1). This stores the private key in ~/.ssh/identity (protocol 1), ~/.ssh/id_dsa (protocol 2 DSA), ~/.ssh/id_ecdsa (protocol 2
ECDSA), or ~/.ssh/id_rsa (protocol 2 RSA) and stores the public key in ~/.ssh/identity.pub (protocol 1), ~/.ssh/id_dsa.pub (protocol 2 DSA), ~/.ssh/id_ecdsa.pub (protocol 2 ECDSA), or
~/.ssh/id_rsa.pub (protocol 2 RSA) in the user’s home directory. The user should then  copy the public key to ~/.ssh/authorized_keys in his/her home directory on the remote machine.
The authorized_keys file corresponds to the conventional ~/.rhosts file, and has  one key per line, though the lines can be very long. After this, the user can log in without giving
the password.
注:每个用户都拥有自己的 authorized_keys

2.authorized_keys文件介绍

~/.ssh/authorized_keys
Lists the public keys (RSA/ECDSA/DSA) that can be used for logging in as this user. The format of this file is
described in the sshd(8) manual page. This file is not highly sensitive, but the recommended permissions are
read/write for the user, and not accessible by others.
注:建议文件权限对拥有者为读写权限,其他用户无权限

转载于:https://my.oschina.net/u/3241588/blog/1576027

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows环境下安装OpenSSH并通过生成SSH密钥登录Linux服务器的步骤如下: 1. 首先,在Windows系统上下载OpenSSH软件包。最常用的是从OpenSSH官方网站下载最新版本的安装程序。 2. 下载完成后,运行安装程序。选择所需的安装选项,如安装路径和组件。 3. 在安装过程中,选择将OpenSSH添加到系统环境变量中。这将使得SSH命令在任意位置都可以被识别。 4. 安装完成后,打开PowerShell或命令提示符,输入“ssh”命令来验证OpenSSH是否成功安装。 5. 安装完成后,需要生成SSH密钥对。在PowerShell或命令提示符中输入“ssh-keygen”命令。 6. 接下来,按照提示输入密钥生成过程中需要的信息,如保存密钥的文件名和密码(可选项)。 7. 生成密钥过程完成后,会在默认的.ssh目录中生成公钥(id_rsa.pub)和私钥(id_rsa)。 8. 将公钥(id_rsa.pub)复制到要登录的Linux服务器上的用户主目录下的.ssh目录中。 9. 在Linux服务器上,使用root权限登录,并使用“mkdir .ssh”命令创建.ssh目录(如果目录不存在)。 10. 然后使用“chmod 700 .ssh”命令将.ssh目录权限设置为仅限所有者读写执行。 11. 使用“cd .ssh”命令进入.ssh目录,并使用“touch authorized_keys”命令创建authorized_keys文件。 12. 运行“chmod 600 authorized_keys”命令将authorized_keys文件权限设置为仅限所有者读写。 13. 通过“cat id_rsa.pub >> authorized_keys”命令将公钥添加到authorized_keys文件末尾。 14. 退出Linux服务器,并回到Windows系统。 15. 在Windows系统上打开PowerShell或命令提示符,输入“ssh username@ip_address”命令来通过SSH密钥登录Linux服务器。 通过以上步骤,您就可以成功安装OpenSSH,并通过生成SSH密钥登录Linux服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值