[关闭]
@myles 2018-03-28T10:16:59.000000Z 字数 2527 阅读 1874

SSH 免密码登录(详解)

Linux学习


一、SSH免密登录概述

1.1 SSH 登录认证方式

SSH 登录认证一般分为以下两种认证方式,即:

1、口令(密码)认证方式
2、密钥认证方式(即免密码登录);

其中口令(密码)认证方式是我们最常用的一种,这里主要介绍使用密钥认证方式登录到linux/unix的方法。

1.2 SSH免密登录实施

具体有关SSH 免密登录分为以下2步操作:

1、生成密钥(公钥与私钥);
2、上传公钥(Public Key)到服务器~/.ssh/authorized_key文件中;

二、SSH 免密登录实操

有关客户端是Linux的SSH 免密登录只需要两步即可,具体操作合说明梳理如下。

序号 步骤 相关命令 相关文件 备注说明
1 生成秘钥对 ssh-keygen -t ras id_rsa(私钥),id_ras.pub(公钥) 在Clinet端生成公/私秘钥对
2 上传公钥 ssh-copy-id root@172.31.0.112 authorized_keys(公钥存放文件) 上传client端公钥(id_rsa.pub)到Server端口(/root/.ssh/authorized_keys)

2.1 客户端生成秘钥对

  1. root@kali:~/.ssh# ls
  2. known_hosts #当前默认/root/.ssh目录下仅这一个文件。
  3. root@kali:~/.ssh# ssh-keygen -t rsa # 注:一路回车即可。
  4. Generating public/private rsa key pair.
  5. Enter file in which to save the key (/root/.ssh/id_rsa):
  6. Enter passphrase (empty for no passphrase):
  7. Enter same passphrase again:
  8. Your identification has been saved in /root/.ssh/id_rsa.
  9. Your public key has been saved in /root/.ssh/id_rsa.pub.
  10. The key fingerprint is:
  11. SHA256:6me7GBEdxiJ2fzvEPeDETApFNM4B6FkTP1ExsjUEi7A root@kali
  12. The key's randomart image is:
  13. +---[RSA 2048]----+
  14. | ..+B%BO. |
  15. | +o=O.@=o |
  16. | oE=o+X+ o |
  17. | o ...= o |
  18. | . So . . |
  19. | o o |
  20. | o . |
  21. | . oo |
  22. | oooo |
  23. +----[SHA256]-----+
  24. root@kali:~/.ssh# ls
  25. id_rsa id_rsa.pub known_hosts # 生成一对秘钥文件
  26. root@kali:~/.ssh#

2.2 上传公钥(id_ras.pub)到服务端

我们可以通过ssh-coyp-id 命令结合用户登录验证进行公钥(id_ras.pub)上传。(当然我们也可以直接手工复制公钥(id_ras.pub)到目标服务上的'/home/$user/.ssh/auhtorized-keys'文件中)
  1. root@kali:~/.ssh# ssh-
  2. ssh-add ssh-agent ssh-argv0 ssh-copy-id ssh-keygen ssh-keyscan
  3. root@kali:~/.ssh# ssh-copy-id root@172.31.0.112
  4. The authenticity of host '172.31.0.112 (172.31.0.112)' can't be established.
  5. RSA key fingerprint is SHA256:L8sTFYRm3EWyovdkIRP8UBiGobOPmhnuk+ZY2jyRwFU.
  6. Are you sure you want to continue connecting (yes/no)? yes
  7. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  8. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  9. root@172.31.0.112's password:
  10. Number of key(s) added: 1
  11. Now try logging into the machine, with: "ssh 'root@172.31.0.112'"
  12. and check to make sure that only the key(s) you wanted were added.

2.3 免密登录测试

  1. root@kali:~/.ssh# ssh root@172.31.0.112

三、Xshell 免密登录

3.1 Xshell免密登录基本流程

(1) 使用Xshell 中的“Tools -> New User key Wizard”生成秘钥对
(2) 复制公钥到Linux服务器'~/.ssh/authorized_key'

3.2 Xshell 免密配置实操

3.2.1 生成ras秘钥对(public&private)

3.2.2 复制粘贴秘钥到服务器('~/.ssh/authorized_keys')

3.2.3 xshell客户端配置

Xshell登录配置.png

四、报错记录

4.1 ssh 公钥上传后,连接测试报错。

  1. root@kali:~/.ssh# ssh root@172.31.0.112
  2. sign_and_send_pubkey: signing failed: agent refused operation
  3. root@172.31.0.112's password:
  1. root@kali:~/.ssh# eval "$(ssh-agent -s)"
  2. Agent pid 9690
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注