电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 1984 人浏览分享

CentOS设置ssh密钥登录

[复制链接]
1984 0

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

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

(1)生成密钥对

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

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

  26. //文件生成成功,keys_root为私钥,keys_root.pub为公钥
  27. [root@iZwz9catu2mrq92b07d1d0Z ~]# ls
  28. keys_root  keys_root.pub   
复制代码


(2)配置公钥

  1. //将生成的公钥写入到用户的authorized_keys
  2. [root@iZwz9catu2mrq92b07d1d0Z ~]# echo -e '#this is keys_root' >> ~/.ssh/authorized
  3. _keys ; cat ~/keys_root.pub >> ~/.ssh/authorized_keys   

  4. [root@iZwz9catu2mrq92b07d1d0Z ~]# cat ~/.ssh/authorized_keys
  5. #this is keys_root
  6. ssh-rsa 我是猪!B3NzaC1yc2E我是猪!BIwAAAQEAyhp9SBxas8Nmwdi4dQfOuUULpMGRnGEFopU2DXhSF+
  7. PE/s80xrVS31Ycd5o4gU3iehKx2vo4OEB2lYZ2JCfptTc59HAj+Qwqh7i5S4YQuX/+31GkY+s8XKFR4QgH1
  8. ubQt9feU2cagfG1f+wWRsa0YtefE67Kjv6OZuKuA2bOdrAH4mzV1m71iLMUZYgaEnfJExXj2lbPAXRqCV+tdIj9h
  9. 0jxhB5pQXsZ3NE38D22WYNKO4Sy8odfE7Oby1I0Emm8Uhiwqgx91HP22iY/WqzZOxeKZPF17CPWr9cChaPh
  10. 9/DXM1Wd8KDCg33MO6hbpqAwh7iEughndXly0FY0oZNKnQ== root@iZwz9catu2mrq92b07d1d0Z
复制代码


(3)配置私钥

下载私钥到本地机器

  1. [root@iZwz9catu2mrq92b07d1d0Z ~]# sz keys_root
复制代码


启动Xshell
工具(Tools)
用户密钥管理者(User Key Manager)
导入已下载的 keys_root 文件(Import)
配置完成
方式2:使用Xshell生成并配置
启动Xshell
工具
新建用户密钥生成向导(New User Key Wizard)
按步骤选择下一步
输入密钥名称和私钥密码后选择下一步
密钥对生成成功,此时私钥已自动导入到Xshell中,需要我们手动保存公钥到本地机器
上传已保存的公钥文件到服务器并配置在用户的authorized_keys文件中

二、通过密钥登录服务器

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

附录

  1. [root@iZwz9catu2mrq92b07d1d0Z ~]# man ssh
复制代码


1.ssh-keygen命令介绍

  1. The user creates his/her key pair by running ssh-keygen(1). This stores the private key in ~/.ssh/id
  2. entity (protocol 1), ~/.ssh/id_dsa (protocol 2 DSA), ~/.ssh/id_ecdsa (protocol 2
  3. ECDSA), or ~/.ssh/id_rsa (protocol 2 RSA) and stores the public key in ~/.ssh/identity.pub
  4. (protocol 1), ~/.ssh/id_dsa.pub (protocol 2 DSA), ~/.ssh/id_ecdsa.pub (protocol 2 ECDSA), or
  5. ~/.ssh/id_rsa.pub (protocol 2 RSA) in the user’s home directory. The user should then  copy t
  6. he public key to ~/.ssh/authorized_keys in his/her home directory on the remote machine.
  7. The authorized_keys file corresponds to the conventional ~/.rhosts file, and has  one key per line,
  8. though the lines can be very long. After this, the user can log in without giving
  9. the password.
复制代码


注:每个用户都拥有自己的 authorized_keys

2.authorized_keys文件介绍


  1. ~/.ssh/authorized_keys
  2. Lists the public keys (RSA/ECDSA/DSA) that can be used for logging in as this user. The format of this file is
  3. described in the sshd(8) manual page. This file is not highly sensitive, but the recommended permissions are
  4. read/write for the user, and not accessible by others.
复制代码


注:建议文件权限对拥有者为读写权限,其他用户无权限





您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

关注

0

粉丝

9021

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.