前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CentOS设置ssh密钥登录 原

CentOS设置ssh密钥登录 原

作者头像
拓荒者
发布2019-03-11 10:37:51
4.6K0
发布2019-03-11 10:37:51
举报
文章被收录于专栏:运维经验分享

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

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

(1)生成密钥对

代码语言:javascript
复制
[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)配置公钥

代码语言:javascript
复制
//将生成的公钥写入到用户的authorized_keys
[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. 连接会话

附录

代码语言:javascript
复制
[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. 注:建议文件权限对拥有者为读写权限,其他用户无权限

本文由 CentOS中文站 - 专注Linux技术 作者:centos 发表,其版权均为 CentOS中文站 - 专注Linux技术 所有,文章内容系作者个人观点,不代表 CentOS中文站 - 专注Linux技术 对观点赞同或支持。如需转载,请注明文章来源。

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/11/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、生成密钥对(两种方式)并配置
  • 二、通过密钥登录服务器
  • 附录
相关产品与服务
全站加速网络
全站加速网络(Enterprise Content Delivery Network,以下简称 ECDN)为您提供稳定高效的网络加速服务,适用于动静混合、纯动态、跨国、上传等多种加速场景。ECDN 网络资源丰富,同时融合静态缓存、智能路由、协议优化、多路传输、抗抖动等自研技术,加速效果更加显著;接入便捷,功能配置灵活多样,可满足您个性化的业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档