@myles
2018-03-28T02:16:59.000000Z
字数 2527
阅读 2007
Linux学习
SSH 登录认证一般分为以下两种认证方式,即:
1、口令(密码)认证方式
2、密钥认证方式(即免密码登录);
其中口令(密码)认证方式是我们最常用的一种,这里主要介绍使用密钥认证方式登录到linux/unix的方法。
具体有关SSH 免密登录分为以下2步操作:
1、生成密钥(公钥与私钥);
2、上传公钥(Public Key)到服务器~/.ssh/authorized_key文件中;
有关客户端是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) |
root@kali:~/.ssh# ls
known_hosts #当前默认/root/.ssh目录下仅这一个文件。
root@kali:~/.ssh# 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_rsa.pub.
The key fingerprint is:
SHA256:6me7GBEdxiJ2fzvEPeDETApFNM4B6FkTP1ExsjUEi7A root@kali
The key's randomart image is:
+---[RSA 2048]----+
| ..+B%BO. |
| +o=O.@=o |
| oE=o+X+ o |
| o ...= o |
| . So . . |
| o o |
| o . |
| . oo |
| oooo |
+----[SHA256]-----+
root@kali:~/.ssh# ls
id_rsa id_rsa.pub known_hosts # 生成一对秘钥文件
root@kali:~/.ssh#
我们可以通过ssh-coyp-id 命令结合用户登录验证进行公钥(id_ras.pub)上传。(当然我们也可以直接手工复制公钥(id_ras.pub)到目标服务上的'/home/$user/.ssh/auhtorized-keys'文件中)
root@kali:~/.ssh# ssh-
ssh-add ssh-agent ssh-argv0 ssh-copy-id ssh-keygen ssh-keyscan
root@kali:~/.ssh# ssh-copy-id root@172.31.0.112
The authenticity of host '172.31.0.112 (172.31.0.112)' can't be established.
RSA key fingerprint is SHA256:L8sTFYRm3EWyovdkIRP8UBiGobOPmhnuk+ZY2jyRwFU.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
root@172.31.0.112's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.31.0.112'"
and check to make sure that only the key(s) you wanted were added.
2.3 免密登录测试
root@kali:~/.ssh# ssh root@172.31.0.112
(1) 使用Xshell 中的“Tools -> New User key Wizard”生成秘钥对
(2) 复制公钥到Linux服务器'~/.ssh/authorized_key'
(1) 选择“Tools -> New User Key Wizard”
(2)配置key类型与长度(默认即可)
(3)生成秘钥对(public&private key pair)
(4)秘钥命名保存
(1)复制公钥
(2)粘贴公钥到服务
root@kali:~/.ssh# ssh root@172.31.0.112
sign_and_send_pubkey: signing failed: agent refused operation
root@172.31.0.112's password:
root@kali:~/.ssh# eval "$(ssh-agent -s)"
Agent pid 9690