0%

SSH 密钥创建及密钥登录

SSH 密钥创建及密钥登录

本文基本引自这里:https://blog.csdn.net/nahancy/article/details/79059135

在我们平时使用Linux系统时候,通常使用的Linux SSH登录方式是用户名加密码的登录方式,今天来探讨另外的一种相对安全的登录方式——密钥登录

我们知道SSH登录是用的RSA非对称加密的,所以我们在SSH登录的时候就可以使用RSA密钥登录,SSH有专门创建SSH密钥的工具ssh-keygen,下面就来一睹风采。

首先进入Linux系统的用户目录下的.ssh目录下,root用户是/root/.ssh,普通用户是/home/您的用户名/.ssh,我们以root用户为例

1
2
3
[root@b04f945297ac .ssh]# cd /root/.ssh/
[root@b04f945297ac .ssh]# ls
known_hosts

上面是没有创建过ssh秘钥的样子

执行ssh-keygen命令创建密钥对:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@b04f945297ac .ssh]# ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/zhimma_id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/zhimma_id_rsa.
Your public key has been saved in /root/.ssh/zhimma_id_rsa.pub.
The key fingerprint is:
SHA256:WrIN4U/dk+1KdTxv0t5zFeXCeWWpvaNtFFMxbkDY2Cw root@b04f945297ac
The key's randomart image is:
+---[RSA 4096]----+
| Bo oo|
| E +o.*|
| . ..o*+|
| . . . . +=*o|
| + S . + o+B|
| X + *+|
| o o . * *|
| . o Bo|
| . . =|
+----[SHA256]-----+

-b 参数,指定了长度,也可以不加-b参数,直接使用ssh-keygen -t rsa

这里我重新命名了下秘钥文件名 zhimma_id_rsa

密钥生成后会在当前目录下多出两个文件,zhimma_id_rsa和zhimma_id_rsa.pub,其中zhimma_id_rsa是私钥(敲黑板:这个很重要,不能外泄),zhimma_id_rsa.pub这个是公钥

1
2
3
[root@b04f945297ac .ssh]# ls
known_hosts zhimma_id_rsa zhimma_id_rsa.pub
[root@b04f945297ac .ssh]#

放置公钥

ssh-copy-id

把公钥拷贝到需要登录的远程服务器或Linux系统上,这里可以使用ssh-copy-id

ssh-copy-id默认端口是22,如果要重新指定端口,则使用-p 端口号命令

1
2
3
4
5
6
7
8
9
10
[root@b04f945297ac .ssh]# ssh-copy-id -i /root/.ssh/zhimma_id_rsa.pub -p 203 root@192.168.2.107  
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/zhimma_id_rsa.pub"
/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@192.168.2.107's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh -p '203' 'root@192.168.2.107'"
and check to make sure that only the key(s) you wanted were added.

这样就把公钥加的指定的服务器上了

尝试登陆下

1
2
3
[root@b04f945297ac .ssh]# ssh -i /root/.ssh/zhimma_id_rsa -p 203 root@192.168.2.107
Last login: Wed Jun 6 04:16:09 2018 from 172.17.0.1
[root@e91b4a662023 ~]#

登陆成功!

手动放置

进入远程服务器.ssh目录,创建authorized_keys 文件,赋权限600

1
2
3
4
5
6
7
8
9
[root@01bb4850cc8c .ssh]# touch authorized_keys
[root@01bb4850cc8c .ssh]# chmod -R 600 authorized_keys
[root@01bb4850cc8c .ssh]# ls -al
total 16
drwx------ 1 root root 4096 Jun 6 07:02 .
dr-xr-x--- 1 root root 4096 Apr 27 08:15 ..
-rw------- 1 root root 0 Jun 6 07:02 authorized_keys
drwxr-xr-x 2 root root 4096 Jun 6 07:02 dd
-rw-r--r-- 1 root root 824 Mar 16 08:59 known_hosts

复制zhimma_id_rsa.pub的内容进入这个文件

尝试登陆

1
2
3
[root@b04f945297ac .ssh]# ssh -i /root/.ssh/zhimma_id_rsa -p 204 root@192.168.2.107
Last login: Wed Jun 6 04:18:46 2018 from 172.17.0.1
[root@01bb4850cc8c ~]#

登陆成功!

ssh-keygen可用的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
ssh-keygen可用的参数选项有:
-a trials 在使用 -T 对 DH-GEX 候选素数进行安全筛选时需要执行的基本测试数量。
-B 显示指定的公钥/私钥文件的 bubblebabble 摘要。
-b bits 指定密钥长度。对于RSA密钥,最小要求768位,默认是2048位。DSA密钥必须恰好是1024位(FIPS 186-2 标准的要求)。
-C comment 提供一个新注释
-c 要求修改私钥和公钥文件中的注释。本选项只支持 RSA1 密钥。
程序将提示输入私钥文件名、密语(如果存在)、新注释。
-D reader 下载存储在智能卡 reader 里的 RSA 公钥。

-e
读取OpenSSH的私钥或公钥文件,并以 RFC 4716 SSH 公钥文件格式在 stdout 上显示出来。
该选项能够为多种商业版本的 SSH 输出密钥。
-F hostname
在 known_hosts 文件中搜索指定的 hostname ,并列出所有的匹配项。
这个选项主要用于查找散列过的主机名/ip地址,还可以和 -H 选项联用打印找到的公钥的散列值。
-f filename
指定密钥文件名。
-G output_file
为 DH-GEX 产生候选素数。这些素数必须在使用之前使用 -T 选项进行安全筛选。
-g 在使用 -r 打印指纹资源记录的时候使用通用的 DNS 格式。
-H 对 known_hosts 文件进行散列计算。这将把文件中的所有主机名/ip地址替换为相应的散列值。
原来文件的内容将会添加一个".old"后缀后保存。这些散列值只能被 ssh 和 sshd 使用。
这个选项不会修改已经经过散列的主机名/ip地址,因此可以在部分公钥已经散列过的文件上安全使用。
-i 读取未加密的SSH-2兼容的私钥/公钥文件,然后在 stdout 显示OpenSSH兼容的私钥/公钥。
该选项主要用于从多种商业版本的SSH中导入密钥。
-l 显示公钥文件的指纹数据。它也支持 RSA1 的私钥。
对于RSA和DSA密钥,将会寻找对应的公钥文件,然后显示其指纹数据。
-M memory
指定在生成 DH-GEXS 候选素数的时候最大内存用量(MB)。

-N new_passphrase
提供一个新的密语。

-P passphrase
提供(旧)密语。
-p 要求改变某私钥文件的密语而不重建私钥。程序将提示输入私钥文件名、原来的密语、以及两次输入新密语。
-q 安静模式。用于在 /etc/rc 中创建新密钥的时候。
-R hostname
从 known_hosts 文件中删除所有属于 hostname 的密钥。
这个选项主要用于删除经过散列的主机(参见 -H 选项)的密钥。
-r hostname
打印名为 hostname 的公钥文件的 SSHFP 指纹资源记录。
-S start
指定在生成 DH-GEX 候选模数时的起始点(16进制)。
-T output_file
测试 Diffie-Hellman group exchange 候选素数(由 -G 选项生成)的安全性。
-t type
指定要创建的密钥类型。可以使用:"rsa1"(SSH-1) "rsa"(SSH-2) "dsa"(SSH-2)
-U reader
把现存的RSA私钥上传到智能卡 reader
-v 详细模式。ssh-keygen 将会输出处理过程的详细调试信息。常用于调试模数的产生过程。
重复使用多个 -v 选项将会增加信息的详细程度(最大3次)。
-W generator
指定在为 DH-GEX 测试候选模数时想要使用的 generator
-y 读取OpenSSH专有格式的公钥文件,并将OpenSSH公钥显示在 stdout 上。