13.Linux OpenSSH 服务管理
文章目录
- Linux OpenSSH 服务管理
- 环境准备
- OpenSSH 服务介绍
- SSH 介绍
- SSH 建立连接的过程
- 加密类型
- 双向加密过程
- 使用 ssh 访问远端CLI
- ssh 工具演示
- ssh工具配置文件
- 配置 ssh 密钥认证
- ssh 故障模拟
- 故障模拟
- 排故故障
- 自定义 SSH 服务
- 配置文件
- 禁止 root 登录
- 禁止密码登录
- 只允许特定用户登录
Linux OpenSSH 服务管理
环境准备
准备两台虚拟机:
- server 虚拟机,并设置ip地址为10.1.8.10/24。
- client 虚拟机,并设置ip地址为10.1.8.11/24。
设置主机名称和名称解析,以client为例:
永久设置主机名
[root@client ~ 18:49:50]# hostnamectl set-hostname client.hxl.cloud
临时设置
[root@client ~ 18:50:03]# hostname client.hxl.cloud
验证
[root@client ~ 18:49:50]# bash
[root@client ~ 18:51:50]#
准备名称解析
[root@client ~ 18:51:50]# echo "10.1.8.10 server.hxl.cloud server
10.1.8.11 client.hxl.cloud client" >> /etc/hosts[root@client ~ 18:53:06]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain610.1.8.10 server.hxl.cloud server
10.1.8.11 client.hxl.cloud client
IP地址通过图形化配置,这里配置Client的IP地址,过程如下
在VM中打开虚拟机,中终端上输入nmtui
回车
会打开一个界面
选择第一个选项回车,进入下一个界面,会出现你虚拟机的网卡名称,选择这个网卡名称进行配置
在IPv4下面有个Addresses中填入你需要的配置的IP地址,我这里是10.1.8.11/24的地址
配置完成后用方向键上下选中OK
,回到上一个界面选QUIT
或者按ESC
退出回到初始界面选择第二个选项
在这个界面中,我们需要回车两次,让网卡重新读取我们刚才配置的IP地址,这样系统才能够重新识别
最后配置就结束了,全部退出即可
验证配置是否成功
[root@client ~ 19:02:20]# ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
ens33 UP 10.1.8.11/24 fe80::20c:29ff:fe55:a3e4/64
可以看见我们的IP地址已经配置完成了
OpenSSH 服务介绍
SSH 介绍
SSH 全称是 Secure Shell,SSH协议是基于应用层的协议,为远程登录会话和其他网络服务提供安全性的协议。
实现此功能的传统方式,如 telnet (终端仿真协议)、 rcp、ftp、 rlogin、rsh都是极为不安全的,并且会使用明文传送密码。OpenSSH 提供了服务端后台程序和客户端工具,用来加密远程控件和文件传输过程中的数据,并由此来代替原来的类似服务。
SSH 建立连接的过程
主要分为下面几个阶段:
- SSH协议版本协商阶段,SSH目前包括SSH1和SSH2两个大版本。
- 密钥和算法协商阶段,SSH支持多种加密算法,双方根据自己和对端支持的算法进行协商,最终决定要使用的算法。
- 认证阶段,服务器和客户端互相进行身份验证。
- 会话请求阶段,客户端会向服务器端发送会话请求。会话请求分为这样几类:申请对数据传送进行压缩、申请伪终端、启动 X11、TCP/IP 端口转发、启动认证代理等。
- 交互会话阶段,会话请求通过后,服务器端和客户端进行信息的交互。例如运行 shell、执行命令、传递文件。
加密类型
- 对称加密,加密和解密都使用一个钥匙。确保数据的完整性。速度快。
- 非对称加密,一对钥匙。公钥用来加密数据。私钥用来解密数据。确保数据的安全性。
双向加密过程
SSH协议是基于非对称加密方法的,服务器和客户端都会生成自己的公钥和私钥。
- 公钥用来加密数据。
- 私钥用来解密数据。
双向加密过程:
-
服务器创建密钥对。远程服务器会在/etc/ssh目录下生成一个名为多个密钥对,例如ecdsa类型的密钥对:ssh_host_ecdsa_key.pub 公钥和 ssh_host_ecdsa_key 私钥。之后每回启动sshd服务的时候,系统会自动在此路径下查找公钥。
客户端请求连接。服务器接到请求后,把公钥传给客户端使用。
-
客户端记录服务器公钥并计算自己的公私钥。客户端将服务器传来的公钥记录在**~/.ssh/known_hosts** 中,若是已经记录有该服务器公钥,则比对是否一致,一致后就计算客户端自己的公私钥。
-
客户端使用服务器的公钥加密自己的公钥并发送给服务器。服务器端拥有客户端公钥+自己私钥,客户端拥有服务器公钥+自己私钥,组成了非对称加密系统。
-
双向加解密。服务器发送数据:用客户端公钥加密,客户端收到数据后用自己私钥解密。客户端发送数据:用服务器公钥加密,服务器收到数据后用自己私钥解密。
使用 ssh 访问远端CLI
ssh 工具演示
方式一:只指定IP或主机名
# 通过IP地址
[hxl@client ~ 19:11:26]$ ssh 10.1.8.11
The authenticity of host '10.1.8.11 (10.1.8.11)' can't be established.
ECDSA key fingerprint is SHA256:EkLOT5mPnO2nbsKsf+rIY5J1bXO8pm0taNJxf2MhTU8.
ECDSA key fingerprint is MD5:4e:84:fc:63:a0:ea:b0:a9:35:0e:da:33:d5:bb:2a:8d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.1.8.11' (ECDSA) to the list of known hosts.
hxl@10.1.8.11's password:
Last login: Sat Sep 13 19:11:26 2025# 通过主机名称
[hxl@client ~ 19:12:53]$ ssh server
The authenticity of host 'server (10.1.8.10)' can't be established.
ECDSA key fingerprint is SHA256:EkLOT5mPnO2nbsKsf+rIY5J1bXO8pm0taNJxf2MhTU8.
ECDSA key fingerprint is MD5:4e:84:fc:63:a0:ea:b0:a9:35:0e:da:33:d5:bb:2a:8d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server,10.1.8.10' (ECDSA) to the list of known hosts.
hxl@server's password:
Last login: Sat Sep 13 19:12:02 2025
方式二:额外指定用户名
# 指定root用户登录
[hxl@client ~ 19:13:23]$ ssh root@server
root@server's password:
Last login: Sat Sep 13 19:10:29 2025 from client.hxl.cloud
# 或者
[hxl@client ~ 19:13:47]$ ssh -l root server
root@server's password:
Last login: Sat Sep 13 19:13:32 2025 from client.hxl.cloud
方式三:额外指定命令
# 同时指定用户和命令
[hxl@client ~ 19:14:12]$ ssh hxl@server hostname
hxl@server's password:
server.hxl.cloud# shell提示符,仍然是本机。
[hxl@client ~ 19:14:44]$
ssh工具配置文件
参考 SSH_CONFIG(5)
。
[root@client ~ 19:15:16]# man ssh_config
-
~/.ssh/config,用户自己的配置,优先级高于全局配置。文件权限不得高于640。
-
/etc/ssh/ssh_config,全局配置,应用于所有用户。
-
示例:
[hxl@client ~ 11:49:54]$ vim .ssh/config Host *User rootStrictHostKeyChecking no# 清空其他主机秘钥 [root@client ~ 10:32:42]# > .ssh/known_hosts# 再次登录不会提示主机key是否校验,验证用户也是root [root@client ~ 11:50:13]# ssh server Warning: Permanently added 'server,10.1.8.10' (ECDSA) to the list of known hosts. root@server's password:
参数说明:
- **Host ***,匹配所有目标服务器。
- StrictHostKeyChecking no,连接目标服务器不校验主机key,直接接受。
- User root,连接目标服务器默认使用laoma账户。
- PreferredAuthentications password,连接目标服务器使用密码认证。
- IdentityFile,指定私钥位置。
配置文件权限:建议设置为只能用户自己读写。
[hxl@client ~ 12:50:13]$ ssh server Bad owner or permissions on /home/laoma/.ssh/config[hxl@client ~ 12:51:25]$ chmod 600 .ssh/config [hxl@client ~ 12:51:50]$ ssh server root@server's password:
配置 ssh 密钥认证
client配置秘钥登录服务器
[hxl@client ~ 19:15:35]$ vim .ssh/config
Host *StrictHostKeyChecking noUser root# 查看并修改文件权限[hxl@client ~ 19:32:31]$ ll -d .ssh/config
-rw-rw-r-- 1 hxl hxl 46 9月 13 19:32 .ssh/config
[hxl@client ~ 19:32:37]$ chmod 640 .ssh/config
[hxl@client ~ 19:32:55]$ ll -d .ssh/config
-rw-r----- 1 hxl hxl 46 9月 13 19:32 .ssh/config
[hxl@client ~ 19:32:56]$ ssh-keygen
Generating public/private rsa key pair.# 私钥保存位置
Enter file in which to save the key (/home/hxl/.ssh/id_rsa): `回车`Enter passphrase (empty for no passphrase): `回车`
Enter same passphrase again: `回车`Your identification has been saved in /home/hxl/.ssh/id_rsa.
Your public key has been saved in /home/hxl/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+kqbNN01EK5SKgVWDdDL0hFEQAF+viCmaLOGz71PvpE hxl@client.hxl.cloud
The key's randomart image is:
+---[RSA 2048]----+
| .oBO*+ . |
| . . .o .. . |
| . .o.o. o |
| o..+o . . |
|... o.o S o |
|+. . o = . . . |
|+o . E . . |
|ooo. = * |
|.oo ooBo. |
+----[SHA256]-----+# 查看生成的文件
[hxl@client ~ 19:35:44]$ ls .ssh/
config id_rsa id_rsa.pub known_hosts# 将公钥推送给目标服务器上的目标用户
[hxl@client ~ 19:38:25]$ ssh-copy-id hxl@server
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/hxl/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
hxl@server's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'hxl@server'"
and check to make sure that only the key(s) you wanted were added.
注意:推给哪个用户就是哪个用户,没有说将密码推给root用户,其他普通用户也可以免输入进行登陆
验证
[hxl@client ~ 19:38:58]$ ssh hxl@server hostname
server.hxl.cloud
# 验证成功
推送公钥相当于:将公钥内容保存到目标服务器上目标用户家目录下.ssh/authorized_keys中
[hxl@client ~ 12:59:23]$ cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCt6I79/d3E9jw/bv0bAJV6FCytohv8ycJ2HBwVXm547dPIVUbDEDYfnzFUGTNtwMF0yUaIZ5YdUwuc114vgQ1KS+pdEpfRaQltunmtWDh58lxyTFKWfQ1WzjzZwYZbECGZ58zsDcS+egvX7mZ9d8bulvMBa4Ye+L8mSLE90xkPfYKdkTpuBHWn4dtVJGSfan78b+MSRIyykmUNkJrvaZgTc5zut9FFxGaAht/DncR6mflYKzdyQCoI5tuqNEMGRFAUSzrvQovXxlPtUx5NOXzlRQvpExUFK/pjYIPXZ3174Lggg1utnaCbBQGBS3wLzdHcFNBpZK9B6bJExKFH3amr hxl@client.hxl.cloud# 在server端查看
[hxl@server ~ 13:00:08]$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCt6I79/d3E9jw/bv0bAJV6FCytohv8ycJ2HBwVXm547dPIVUbDEDYfnzFUGTNtwMF0yUaIZ5YdUwuc114vgQ1KS+pdEpfRaQltunmtWDh58lxyTFKWfQ1WzjzZwYZbECGZ58zsDcS+egvX7mZ9d8bulvMBa4Ye+L8mSLE90xkPfYKdkTpuBHWn4dtVJGSfan78b+MSRIyykmUNkJrvaZgTc5zut9FFxGaAht/DncR6mflYKzdyQCoI5tuqNEMGRFAUSzrvQovXxlPtUx5NOXzlRQvpExUFK/pjYIPXZ3174Lggg1utnaCbBQGBS3wLzdHcFNBpZK9B6bJExKFH3amr hxl@client.hxl.cloud# 推给目标主机root用户
[root@server ~ 12:59:50]# mkdir -m 700 .ssh
[root@server ~ 13:02:05]# cp ~laoma/.ssh/authorized_keys .ssh# 客户端验证
[laoma@client ~ 13:02:59]$ ssh root@server hostname
server.laoma.cloud
以非交互方式生成密钥对
[hxl@client ~ 13:05:05]$ ssh-keygen -t rsa -N '' -f id_rsa_new[hxl@client ~ 13:05:31]$ ls *new*
id_rsa_new id_rsa_new.pub
其他选项
# -p选项指定目标服务器 sshd 服务端口号,默认22
[hxl@client ~ 13:05:45]$ ssh -l root -p 1022 server hostname
root@server's password:
server.laoma.cloud# -i 指定私钥位置
[hxl@client ~ 13:07:18]$ mv .ssh/id_rsa /tmp
[hxl@client ~ 13:10:25]$ ssh -i /tmp/id_rsa root@server hostname
server.laoma.cloud# 如果找不到密钥,则使用密码登录
[hxl@client ~ 13:10:40]$ ssh root@server hostname
root@server's password:
ssh 故障模拟
你是某金融科技公司的 Linux 运维工程师,负责维护核心生产服务器集群。
发现某台服务器无法通过ssh远程登录。
请排查故障,并修复问题。
故障模拟
server设置
可以新添加一个用户用来模拟故障,这里我接着使用我自己的用户来模拟
[root@server ~ 08:25:18]# useradd -s /sbin/nologin zhangsan
[root@server ~ 08:26:56]# echo redhat | passwd --stdin zhangsan[root@server ~ 08:27:05]# systemctl stop sshd
client 设置
如果选择上面的新添加用户,下面的User 后面写 zhangsan
[hxl@client ~ 09:24:49]$ chmod 666 .ssh/config
[hxl@client ~ 09:25:40]$ cat > .ssh/config <<EOF
Host *User hxlStrictHostKeyChecking yes
EOF
[hxl@client ~ 09:31:56]$ > .ssh/known_hosts
排故故障
- 错误现象:
[hxl@client ~ 09:32:01]$ ssh server
Bad owner or permissions on /home/hxl/.ssh/config
处理方法:
[hxl@client ~ 09:33:02]$ ll -d .ssh/config
-rw-rw-rw- 1 hxl hxl 48 9月 15 09:30 .ssh/config
[hxl@client ~ 09:33:34]$ chmod 600 .ssh/config
[hxl@client ~ 09:34:09]$ ll -d .ssh/config
-rw------- 1 hxl hxl 48 9月 15 09:30 .ssh/config
- 错误现象:
[hxl@client ~ 09:34:34]$ ssh server
ssh: connect to host server port 22: Connection refused
处理方法:
[hxl@client ~ 09:36:35]$ ping -c 1 server
PING server.hxl.cloud (10.1.8.10) 56(84) bytes of data.
64 bytes from server.hxl.cloud (10.1.8.10): icmp_seq=1 ttl=64 time=0.279 ms--- server.hxl.cloud ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.279/0.279/0.279/0.000 ms[root@server ~ 09:31:06]# systemctl is-active sshd
inactive
[root@server ~ 09:37:09]# systemctl restart sshd
[root@server ~ 09:37:21]# systemctl is-active sshd
active
- 错误现象:
[hxl@client ~ 09:37:49]$ ssh server
No ECDSA host key is known for server and you have requested strict checking.
Host key verification failed.
处理方法:
[hxl@client ~ 09:38:03]$ vim .ssh/config
Host *
User hxl
#StrictHostKeyChecking yes
StrictHostKeyChecking no
- 错误现象:
[hxl@client ~ 09:40:59]$ ssh server
Last login: Mon Sep 15 09:04:57 2025 from 10.1.8.1
This account is currently not available.
Connection to server closed.
处理方法:
[root@server ~ 09:37:24]# cat /etc/passwd | grep hxl
hxl:x:1000:1000:hxl:/home/hxl:/sbin/nologin
[root@server ~ 09:42:43]# usermod -s /bin/bash hxl
[root@server ~ 09:42:59]# cat /etc/passwd | grep hxl
hxl:x:1000:1000:hxl:/home/hxl:/bin/bash
- 问题解决:
[hxl@client ~ 09:43:49]$ ssh server
Last login: Mon Sep 15 09:43:46 2025 from client.hxl.cloud
[hxl@server ~ 09:44:23]$
自定义 SSH 服务
配置文件
sshd服务配置文件:/etc/ssh/sshd_config
。帮助 sshd_config(5)
常见配置:
- PermitRootLogin no,禁止 root 用户登录。
- root用户权限不受限制。
- root用户存在每个linux系统,只需要猜密码就可以。
- 从审计角度来看,很难跟踪哪个授权用户以root身份登录并进行了更改。 如果用户必须以普通用户身份登录并切换到root帐户,则会生成一个日志事件,可用于帮助提供问责制。
- PermitRootLogin prohibit-password,禁止root用户通过密码登录。
- PasswordAuthentication no,禁止用户使用密码登录。
- AllowUsers exampleuser,允许特定用户登录,该用户可以提权为root。
- UseDNS no,客户端连接服务器的时候,服务器不需要反向解析服务端IP地址,提高连接速度。
禁止 root 登录
进入sshd服务的配置文件中进行配置
[root@server ~ 16:54:35]# vim /etc/ssh/sshd_config
[root@server ~ 16:55:57]# systemctl restart sshd
# 即使配置了免密登录,也无法远程登录
[hxl@client ~ 16:53:10]$ ssh root@server
root@server's password:
Permission denied, please try again.
root@server's password:
Permission denied, please try again.
root@server's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).# 使用普通用户登录,然后提权为root用户
[hxl@client ~ 16:56:54]$ ssh hxl@server
Last login: Mon Sep 15 13:00:09 2025 from 10.1.8.1
[hxl@server ~ 16:57:01]$ su -
密码:
上一次登录:一 9月 15 16:52:57 CST 2025从 client.hxl.cloudpts/4 上
最后一次失败的登录:一 9月 15 16:56:31 CST 2025从 client.hxl.cloudssh:notty 上
最有一次成功登录后有 3 次失败的登录尝试。
[root@server ~ 16:57:08]#
禁止密码登录
[root@server ~ 16:57:41]# vim /etc/ssh/sshd_config
[root@server ~ 16:58:43]# systemctl restart sshd
# laowang账户未配置密钥登录,直接拒绝
[hxl@client ~ 17:05:50]$ ssh laowang@server
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
只允许特定用户登录
例如hxl用户。
[root@server ~ 17:12:05]# vim /etc/ssh/sshd_config
[root@server ~ 17:13:16]# systemctl restart sshd
# laowang输入正确的密码也无法登录
[hxl@server ~ 17:14:01]$ ssh laowang@server
laowang@server's password:
Permission denied, please try again.
laowang@server's password:
Permission denied, please try again.
laowang@server's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).[hxl@server ~ 17:14:18]$ ssh hxl@server hostname
hxl@server's password:
server.hxl.cloud
l用户。
[root@server ~ 17:12:05]# vim /etc/ssh/sshd_config
[root@server ~ 17:13:16]# systemctl restart sshd
[外链图片转存中…(img-mmRSjzry-1757933486754)]
# laowang输入正确的密码也无法登录
[hxl@server ~ 17:14:01]$ ssh laowang@server
laowang@server's password:
Permission denied, please try again.
laowang@server's password:
Permission denied, please try again.
laowang@server's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).[hxl@server ~ 17:14:18]$ ssh hxl@server hostname
hxl@server's password:
server.hxl.cloud