当前位置: 移动技术网 > 科技>操作系统>Linux > Linux - 配置SSH免密登录 - “ssh-keygen”的基本用法

Linux - 配置SSH免密登录 - “ssh-keygen”的基本用法

2019年06月15日  | 移动技术网科技  | 我要评论

1 什么是ssh

引用百度百科的说明:

ssh 为 secure shell的缩写,由 ietf 的网络小组(network working group)所制定;它是建立在应用层基础上的安全协议。
ssh 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 ssh 协议可以有效防止远程管理过程中的信息泄露问题。
ssh最初是unix系统上的一个程序,后来又迅速扩展到其他操作平台。

为了在不同平台/网络主机之间的通信安全, 很多时候我们都要通过ssh进行认证. ssh认证方式主要有2种:

① 基于口令的安全认证: 每次登录的时候都要输入用户名和密码, 由于要在网络上传输密码, 可能存在中间人攻击的风险;
② 基于密钥的安全认证: 配置完成后就可以实现免密登录, 这种方式更加安全 —— 不需要在网络上传递口令, 只需要传输一次公钥. 常见的git的ssh方式就是通过公钥进行认证的.

2 配置ssh免密登录

说明: 这里演示所用的服务器操作系统是cent os 7. 我们的目标是:

a服务器(172.16.22.131) 能免密登录 b服务器 (172.16.22.132).

注意: ssh连接是单向的, a能免密登录b, 并不能同时实现b能免密登录a.

2.1 安装必需的软件

在操作之前, 先确保所需要的软件已经正常安装.

这里我们需要安装ssh-keygenssh-copy-id, 安装方式如下:

# 安装ssh-keygen, 需要确保服务器可以联网. 博主这里已经安装完成, 所以没有做任何事.
[root@localhost ~]# yum install -y ssh-keygen
loaded plugins: fastestmirror, langpacks
base                                                         | 3.6 kb  00:00:00     
epel                                                         | 3.6 kb  00:00:00     
extras                                                       | 2.9 kb  00:00:00     
updates                                                      | 2.9 kb  00:00:00     
loading mirror speeds from cached hostfile
no package ssh-keygen available.
error: nothing to do

# 安装ssh-copy-id
[root@localhost ~]# yum install -y ssh-copy-id 
loaded plugins: fastestmirror, langpacks
loading mirror speeds from cached hostfile
no package ssh-copy-id available.
error: nothing to do

2.2 ssh-keygen创建公钥-私钥对

(1) 在指定目录下生成rsa密钥, 并指定注释为“shoufeng”, 实现示例:

[root@localhost ~]# ssh-keygen  -t rsa    -f ~/.ssh/id_rsa   -c "shoufeng"
#                               ~密钥类型  ~密钥文件路径及名称  ~ 备注信息
generating public/private rsa key pair.
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:
9a:e3:94:b9:69:c8:e9:68:4b:dc:fa:43:25:7f:53:f1 shoufeng
the key's randomart image is:
+--[ rsa 2048]----+
|                 |
|          .      |
|           o     |
|    . .   . e    |
|     +  s.       |
| . .. .=o        |
|  oo.ob. .       |
| ..o=o.+         |
| .++oo+          |
+-----------------+

注意: 密钥的文件名称必须是id_xxx, 这里的xxx就是-t参数指定的密钥类型. 比如密钥类型是rsa, 那么密钥文件名就必须是id_rsa.

(2) ssh-keygen常用参数说明:

-t: 密钥类型, 可以选择 dsa | ecdsa | ed25519 | rsa;

-f: 密钥目录位置, 默认为当前用户home路径下的.ssh隐藏目录, 也就是~/.ssh/, 同时默认密钥文件名以id_rsa开头. 如果是root用户, 则在/root/.ssh/id_rsa, 若为其他用户, 则在/home/username/.ssh/id_rsa;

-c: 指定此密钥的备注信息, 需要配置多个免密登录时, 建议携带;

-n: 指定此密钥对的密码, 如果指定此参数, 则命令执行过程中就不会出现交互确认密码的信息了.

举例说明: 同时指定目录位置、密码、注释信息, 就不需要输入回车键即可完成创建:

ssh-keygen -t rsa -f ~/.ssh/id_rsa -n shoufeng -c shoufeng

(3) 前往~/.ssh/目录下查看生成的文件:

# 生成的文件以test_rsa开头, test_rsa是私钥, test_rsa.pub是公钥:
[root@localhost .ssh]# ls
test_rsa  test_rsa.pub

# 通过cat命令查看公钥文件: 
[root@localhost .ssh]# cat id_rsa.pub 
ssh-rsa aaaab3nzac1yc2eaaaadaqabaaabaqc2jplmqgeg9jb9zztocw0wms8hdvpfxthqg1voqtoji/cp0+8ruzl3p6ntzqfhbs0itcy0ypijggx4exyipflvilv2bsxrincvv73vnydvyl5glhsrgox+372wovlanq7mxq06qaonjurd0c64xqdjfkb1ovs/nykaor9d8yq/fxfwkqk7tzjm0cvbag7+yr8lc9tjtcypmnxnngislipzjbcnft+5vtcfsenfujd60dmzdzrqtxgfss2j34cucztqssitmyf3dyhqmrxl+cj2vjzwvzru6iy7bpqjfwwfyy9m8kal0pz+jjuau7esvbxf6hjcqhypp2btuyff+vdv shoufeng
# 可以看到最后有一个注释内容shoufeng

2.3 ssh-copy-id把a的公钥发送给b

默认用法是: ssh-copy-id root@172.16.22.132, ssh-copy-id命令连接远程服务器时的默认端口是22, 当然可以指定文件、远程主机的ip、用户和端口:

# 指定要拷贝的本地文件、远程主机的ip+用户名+端口号:
[root@localhost .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@172.16.22.132
/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.16.22.132's password:  # 输入密码后, 将拷贝公钥

number of key(s) added: 1

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

2.4 在a服务器上免密登录b服务器

[root@localhost .ssh]# ssh root@172.16.22.132
last login: fri jun 14 08:46:04 2019 from 192.168.34.16    # 登录成功

                    

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网