linux之SSH密钥创建及配置

1,196 阅读5分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第14天,点击查看活动详情

SSH(Secure Shell)是一种能安全登陆和访问远程linux主机的协议。当然,前提是远程linux主机必须部署和配置sshd服务(linux默认安装此服务)。

一、SSH服务配置

SSH服务的守护进程为sshd,它的配置文件为/etc/ssh/sshd_config,下面是对redhat8默认的SSH服务配置文件中的部分配置信息说明:

#Port 22                                      #配置远程登录端口,默认为22端口
#ListenAddress 0.0.0.0	                      #设定sshd服务器监听的IP地址
PermitRootLogin yes                           #是否允许root用户远程登陆
#PubkeyAuthentication yes                     #是否允许密钥方式登录,默认允许
AuthorizedKeysFile	.ssh/authorized_keys  #指定公钥的路径及名称(注意这里的.ssh处于/root目录下)
PermitEmptyPasswords no	                      #是否允许空密码登录,默认不允许
PasswordAuthentication yes                    #授权口令方式登陆,默认允许

二、SSH远程登陆

1.SSH远程登陆工具大体有两类:

  • 一类是图形化的SFTP登陆工具,如winscp,Xftp,FlashFXP等,它允许用户对远程主机进行文件的复制、粘贴及编辑工作。
  • 一类是基于命令行终端的登陆工具,如XShell、SecureCRT等,它通过命令操作来实现对远程主机的更为广泛的控制,如软件安装、进程管理等(不仅仅局限于文件复制粘贴及编辑等功能)。

2.远程linux主机的sshd服务可提供两种安全验证的方法,允许我们进行远程登陆和访问。

  • 1)基于口令的验证-用户账户和密码来验证登陆。
  • 2)基于密钥的验证-需要在生成密钥对,其中客户机中需存储密钥对中的私钥;而远程主机中必须存储密钥对中的公钥,其名称可在配置文件中设置,默认为authorized_keys。

三、SSH密钥创建及登陆配置

基于口令的验证配置很好理解,只要完成远程访问的相关配置,并知道用户名和密码,即可登陆。这里重点学习基于密钥的验证方式来登陆远程主机。

1.修改配置文件(默认可不修改,当然如果需要个性化访问则另当别论)

这里假设远程主机ip为:192.168.100.100

[root@hollowman ~]# vim /etc/ssh/sshd_config 
#Port 22                                      #配置远程登录端口,默认为22端口 ,可不修改
#PubkeyAuthentication yes                     #授权密钥方式登录,默认允许 ,可不修改
AuthorizedKeysFile .ssh/authorized_keys       #指定公钥的路径及名称(注意这里的.ssh处于/root目录下),下一步公钥文件要使用此文件名
[root@hollowman ~]# systemctl restart sshd    #重启sshd服务,使配置生效

2.密钥对创建

基于密钥的验证需要用到一对密钥,其中公钥放在远程主机的指定路径下,并用指定名称(如/root/.ssh/authorized_keys),私钥则放在客户机上。

可在linux客户机或者远程主机(当客户机为windows的情况下)打开终端,并通过ssh-keygen命令来生成密钥对。

[root@hollowman ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  #手动设置存储路径或者按回车键接受默认存储路径
Created directory '/root/.ssh'.
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:sUpc2U+VnnLpkAGWodvT7XifCJegFkGXRtPyYXtRNNA root@hollowman.cn
The key's randomart image is:
+---[RSA 2048]----+
|         .=Bo.+++|
|        .=o+o=.E.|
|        =.o =+oo.|
|     . . =.+++=. |
|      o S.o.o=o  |
|     . .  o..oo  |
|      .  o ..oo  |
|        .   o....|
|             . ..|
+----[SHA256]-----+

此时,密钥对创建成功,默认的密钥对将保存在/root/.ssh目录下,其中私钥为id_rsa,公钥为id_rsa.pub

[root@hollowman ~]# ls /root/.ssh/
id_rsa  id_rsa.pub

3.密钥对分配

要想客户机访问远程主机,必须将公钥放置在被访问的远程主机种的/root/.ssh目录,并命名为authorized_keys

1)当客户机为linux系统时。

此时密钥对可在客户机种生成,并可通过ssh-copy-id命令将公钥上传至远程主机/root/.ssh目录,并自动命名为authorized_keys

[root@hollowman ~]# ssh-copy-id  192.168.100.100

以下为优麒麟安装PuTTY SSH(软件安装器中直接安装)并以root身份生成密钥后,上传公钥的命令:

hollowman@hollowman:~/桌面$ sudo su - root
root@hollowman:~# ssh-copy-id 192.168.100.100
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.100.100 (192.168.100.100)' can't be established.
ECDSA key fingerprint is SHA256:rTjTNa/pCh+eiZ5DRHXJce0wtfiRtywYY3rM3D9Lp5A.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 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@192.168.100.100's password:  #输入优麒麟的root密码
Permission denied, please try again.
root@192.168.100.100's password:  #输入远程主机(192.168.100.100)的root密码

Number of key(s) added: 1

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

由于公钥已经上传至远程主机,用ssh连接后,直接可以以基于密钥的验证方式进入系统(不需要用户密码)

root@hollowman:~# ssh 192.168.100.100
Activate the web console with: systemctl enable --now cockpit.socket

Last failed login: Wed Jan  6 07:34:49 CST 2021 from 192.168.100.101 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Jan  6 07:26:46 2021 from 192.168.100.101
[root@hollowman ~]# 

2)当客户机为windows系统时

此时一般通过在远程主机生成密钥后,将密钥复制到客户机中(如winscp,Xftp)来处理,同时将公钥改名为配置文件定义的文件名。

a.先用远程SSH协议工具(winscp,Xftp)以基于口令的验证方式登陆来进行连接(前提是基于口令的验证方式登陆方式没有被禁用)。

b.将私钥复制到客户机中(winscp为例),供客户机中的SSH协议工具导入使用。

SSH远程登陆Linux主机 winscp.png

c.客户机SSH协议工具导入私钥(id_rsa),并通过基于密钥的验证方式访问远程主机 因为winscp只支持PuTTY格式的密钥,不支持远程主机生成的SSH-2格式的密钥,这里可以用XFtp或XShell工具来测试(如下图)。

SSH远程登陆Linux主机 winscp2.png

SSH远程登陆Linux主机 winscp3.png