一、系统负载

计算机核心资源:

  1. CPU: 计算(lscpu)
  2. 内存: 缓存数据(掉电丢失)
  3. 硬盘: 持久化存储数据
  4. 网络: 传播数据

1.cpu:

[root@c001 ~]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
座:                 1
NUMA 节点:         1
厂商 ID:           AuthenticAMD
CPU 系列:          25
型号:              80
型号名称:        AMD Ryzen 7 5800H with Radeon Graphics
步进:              0
CPU MHz:             3194.003
BogoMIPS:            6388.00
超管理器厂商:  VMware
虚拟化类型:     完全
L1d 缓存:          32K
L1i 缓存:          32K
L2 缓存:           512K
L3 缓存:           16384K
NUMA 节点0 CPU:    0

2.给系统加负载

md5sum /dev/zero & = 后台单核 CPU 压力测试(满载)

因为:

  • /dev/zero 无限出 0

  • md5sum
    

    无限计算哈希

    → 会把

    1 个 CPU 核心跑满 100%

用途:

  • 测试 CPU 稳定性
  • 测试散热
  • 测试系统负载
  • 模拟高 CPU 场景
#给系统加负载
root@c001 ~ 12:58:21# md5sum /dev/zero &
[1] 65683

3.查看使用情况 top 命令,使用P(CPU降序排序) M(内存降序排序)

image-20260403130354658

top作用:动态查看进程信息,包括不同状态任务数量,CPU消耗和内存消耗。

[root@c001 ~]# top
top - 10:14:52 up  1:36,  3 users,  load average: 1.51, 1.26, 0.71
Tasks: 350 total,   4 running, 346 sleeping,   0 stopped,   0 zombie
%Cpu(s): 86.5 us, 13.5 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   1003432 total,   308524 used,   694908 free,      764 buffers
KiB Swap:  2097148 total,        0 used,  2097148 free.   144812 cached Mem

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                            
 81290 root      20   0  108072    616    524 R 97.6  0.1   9:12.75 md5sum                                             
   618 root      20   0       0      0      0 R  0.3  0.0   0:01.17 xfsaild/dm-1                                       
  2091 root      20   0  133836   5440   4088 S  0.3  0.5   0:19.21 sshd                                               
     1 root      20   0   50852   7192   2528 S  0.0  0.7   0:00.88 systemd                                            
     2 root      20   0       0      0      0 S  0.0  0.0   0:00.01 kthreadd                                           
     3 root      20   0       0      0      0 S  0.0  0.0   0:00.04 ksoftirqd/0                                        
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                                       
     6 root      20   0       0      0      0 S  0.0  0.0   0:00.31 kworker/u256:0                                     
     7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0                                        
     8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                                             
     9 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/0                                            
    10 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/1                                            
    11 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/2                                            
    12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/3                                            
    13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/4 

cpu指标:

%Cpu(s): 86.5 us, 13.5 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

内存指标:

KiB Mem:   1003432 total,   308524 used,   694908 free,      764 buffers
KiB Swap:  2097148 total,        0 used,  2097148 free.   144812 cached Mem

4.磁盘容量:

[root@c001 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   18G  4.8G   13G   28% /
devtmpfs                 484M     0  484M    0% /dev
tmpfs                    490M     0  490M    0% /dev/shm
tmpfs                    490M  6.7M  484M    2% /run
tmpfs                    490M     0  490M    0% /sys/fs/cgroup
/dev/sda1                497M   96M  401M   20% /boot


5.硬盘速度,模拟下载

[root@c001 ~]# wget http://192.168.42.100/02.%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/CentOS-7-x86_64-DVD-2207-02.iso
--2026-04-02 10:22:29--  http://192.168.42.100/02.%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/CentOS-7-x86_64-DVD-2207-02.iso
正在连接 192.168.42.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:4746903552 (4.4G) [application/octet-stream]
正在保存至: “CentOS-7-x86_64-DVD-2207-02.iso”

 1% [=>                                                                                                                                                                   ] 61,675,406  3.14MB/s 剩余 28m 36s

6.安装iotop,iotop 查看每个进程的读写速度,可以排序

C
[root@c001 ~]# yum install iotop
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
软件包 iotop-0.6-4.el7.noarch 已安装并且是最新版本
无须任何处理

7.安装sysstat,使用sar -dp 1,进行磁盘 IO 统计

[root@c001 ~]# yum install -y sysstat
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 sysstat.x86_64.0.10.1.5-20.el7_9 将被 安装
--> 正在处理依赖关系 libsensors.so.4()(64bit),它被软件包 sysstat-10.1.5-20.el7_9.x86_64 需要
--> 正在检查事务
---> 软件包 lm_sensors-libs.x86_64.0.3.4.0-8.20160601gitf9185e5.el7_9.1 将被 安装
--> 解决依赖关系完成

依赖关系解决

=====================================================================================================================
 Package                    架构              版本                                          源                  大小
=====================================================================================================================
正在安装:
 sysstat                    x86_64            10.1.5-20.el7_9                               updates            315 k
为依赖而安装:
 lm_sensors-libs            x86_64            3.4.0-8.20160601gitf9185e5.el7_9.1            updates             42 k

事务概要
=====================================================================================================================
安装  1 软件包 (+1 依赖软件包)

总下载量:357 k
安装大小:1.2 M
Downloading packages:
(1/2): lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7_9.1.x86_64.rpm                          |  42 kB  00:00:00     
(2/2): sysstat-10.1.5-20.el7_9.x86_64.rpm                                                     | 315 kB  00:00:01     
---------------------------------------------------------------------------------------------------------------------
总计                                                                                 227 kB/s | 357 kB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7_9.1.x86_64                                        1/2 
  正在安装    : sysstat-10.1.5-20.el7_9.x86_64                                                                   2/2 
  验证中      : sysstat-10.1.5-20.el7_9.x86_64                                                                   1/2 
  验证中      : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7_9.1.x86_64                                        2/2 

已安装:
  sysstat.x86_64 0:10.1.5-20.el7_9                                                                                   

作为依赖被安装:
  lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7_9.1                                                        

完毕!

[root@c001 ~]# sar -dp 1
Linux 3.10.0-123.el7.x86_64 (c001)      2026年04月02日  _x86_64_        (1 CPU)

10时37分27秒       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
10时37分28秒       sda      8.70      0.00     82.61      9.50      0.01      1.00      1.00      0.87
10时37分28秒       sr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
10时37分28秒 centos-swap      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
10时37分28秒 centos-root      8.70      0.00     82.61      9.50      0.01      1.00      1.00      0.87

10时37分28秒       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
10时37分29秒       sda      8.00      0.00     72.00      9.00      0.00      0.50      0.50      0.40
10时37分29秒       sr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
10时37分29秒 centos-swap      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
10时37分29秒 centos-root      8.00      0.00     72.00      9.00      0.00      0.50      0.50      0.40

10时37分29秒       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
10时37分30秒       sda     12.50      0.00    162.50     13.00      0.01      0.50      0.50      0.62
10时37分30秒       sr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
10时37分30秒 centos-swap      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
10时37分30秒 centos-root     12.50      0.00    162.50     13.00      0.01      0.50      0.50      0.62

二、服务管理(systemctl 实践)

systemd :

系统初始化进程(PID=1),负责系统启动、服务管理

核心功能:并行启动服务、按需激活、日志管理、资源限制、系统状态监控

替代传统 SysVinit,是现代 Linux 发行版(CentOS7+/Ubuntu15.04+)的标准 init 系统

pid为1的程序是systemd,也是系统第一个进程,负责操作系统整个资源的管理。
系统中服务也由systemd管理。

systemctl action service-name
action: status start stop restart enalbe disable is-active is-enabled
service-name: sshd firewalld mysqld

systemd与systemctl关系:

  1. systemd 一直运行的程序(守护进程)
  2. systemctl 是给systemd下指令的工具,人与机器之间交互使用。

类比与web服务器:

  1. httpd
  2. 浏览器访问页面

systemctl

主要用法:

systemctl start/stop/restart/status 服务名  # 启动/停止/重启/查看状态
systemctl enable/disable 服务名            # 开机自启/关闭自启

systemctl daemon-reload  # 通知 systemd 加载新 unit ,文件重新加载unit配置,使新建/修改的unit文件生效

1.查看服务状态

[root@c001 ~]# systemctl status sshd
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
   Active: active (running) since 四 2026-04-02 08:38:48 CST; 2h 45min ago
 Main PID: 1100 (sshd)
   CGroup: /system.slice/sshd.service
           └─1100 /usr/sbin/sshd -D

2.运行记录

4月 02 08:38:48 c001 systemd[1]: Started OpenSSH server daemon.
4月 02 08:38:48 c001 sshd[1100]: Server listening on 0.0.0.0 port 22.
4月 02 08:38:48 c001 sshd[1100]: Server listening on :: port 22.
4月 02 08:40:24 c001 sshd[2091]: Accepted password for root from 192.168.100.1 port 63624 ssh2
4月 02 10:30:19 c001 sshd[101884]: Failed password for root from 192.168.100.1 port 57468 ssh2
4月 02 10:30:23 c001 sshd[101884]: Accepted password for root from 192.168.100.1 port 57468 ssh2
4月 02 11:18:10 c001 sshd[51288]: Accepted password for root from 192.168.100.1 port 55863 ssh2

3.停止服务

[root@c001 ~]# systemctl stop sshd
[root@c001 ~]# systemctl is-active sshd
inactive
[root@c001 ~]# systemctl status sshd
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
   Active: inactive (dead) since 四 2026-04-02 11:26:10 CST; 55s ago

4.使用工具再次连接,能连接上去,服务未启动。

[root@c001 ~]# systemctl start sshd
[root@c001 ~]# systemctl is-active sshd
active

5.# 禁止sshd服务开机启动[root@c001 ~]# systemctl disable sshd

[root@c001 ~]# systemctl disable sshd
rm '/etc/systemd/system/multi-user.target.wants/sshd.service'
[root@c001 ~]# systemctl is-enabled sshd
disabled

6.重启验证:机器启动后是否可以通过终端工具登录(不行)

[root@c001 ~]# reboot
连接断开
连接主机...
java.net.ConnectException: Connection refused: connect
连接主机...
java.net.ConnectException: Connection refused: connect
连接主机...

7.# 设置sshd服务开机启动

[root@c001 ~]# systemctl enable sshd
Created symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service.
[root@c001 ~]# systemctl is-enabled sshd
enabled

8.# 重启验证:机器启动后是否可以通过终端工具登录(行)

[root@c001 ~]# reboot

连接主机成功
Last login: Thu Apr  2 11:39:01 2026
[root@c001 ~]#

三、开发一个 study 服务

开发 studyd 服务主程序 study

脚本说明:这是一个无限循环的脚本,每 5 秒会向 /var/log/study.log 文件中追加一行

包含当前时间的日志,内容为 [时间]: I’M studying [ Linux ] 。

1.编辑vim /usr/bin/study

root@c001 ~ 17:48:02# vim /usr/bin/study

2.把/usr/bin/study里面的内容修改如下

#!/bin/bash
# 第一行内容是脚本的 "解释器声明"(shebang),指定该脚本使用 /bin/bash 
作为解释器执行。系统会根据这一行找到对应的 shell 程序来解析后续命令。
# 启动一个无限循环:while 是循环关键字,true 是一个永远为真的条件,因此
这个循环会一直执行下去,直到被外部终止(如 Ctrl+C)。
while true
# 循环体的开始标记,do 和后面的 done 之间的内容是循环中重复执行的命令。
do
  
  # 执行 date 命令(获取当前系统时间),并通过 $(...) 捕获其输出,将结果
赋值给变量 DATE。
  DATE=$(date)
  
  # echo 命令输出字符串,其中 $DATE 会被替换为变量的值
  # >> 是追加重定向符号,将输出内容追加到 /var/log/study.log 文件中
  # 最终输出内容类似 Fri Oct 31 10:00:00 CST 2025: I'M studying [ 
Linux ]。
  echo "$DATE: I'M studying [ Linux ]" >> /var/log/study.log
  
  # 让脚本暂停执行 5 秒(sleep 命令用于延迟,单位默认为秒),避免循环执
行过快。
  sleep 5
# 循环体的结束标记,与前面的 while 和 do 配合,标志着一次循环的结束。
done

3.给/usr/bin/study修改权限,加上x可执行权限,Linux系统中,脚本必须添加执行权限才能运行:

root@c001 ~ 18:53:06# chmod +x /usr/bin/study

4.创建 studyd 服务单元文件,systemd通过 .service 配置文件识别、管理服务,该文件是服务的配置核心

root@c001 ~ 18:53:23# cp /usr/lib/systemd/system/sshd.service \
> /etc/systemd/system/studyd.service
cp:是否覆盖"/etc/systemd/system/studyd.service"? y
root@c001 ~ 18:57:41# 

5.写入极简核心配置(保留必要参数)

root@c001 ~ 18:57:41#  vim /etc/systemd/system/studyd.service

[Unit]
# 服务基础信息:描述服务用途,便于识别
Description=study server daemon

[Service]
# 服务执行规则:指定启动时运行的脚本路径
ExecStart=/usr/bin/study

[Install]
# 开机自启配置:绑定多用户命令行模式,实现服务器开机自启
WantedBy=multi-user.target

6.重载systemd配置

systemd不会自动识别新增/修改的服务文件,必须执行重载命令:

root@c001 ~ 19:16:50# systemctl daemon-reload

7、启用并立即启动服务

一条命令实现开机自启+立即运行

root@c001 ~ 19:17:11# # systemctl enable studyd --now


• enable:设置服务开机自动启动

• --now:无需重启系统,直接启动服务

8.查看服务运行状态

验证服务是否正常运行:

root@c001 ~ 19:17:49#  systemctl status studyd
studyd.service - study server daemon
   Loaded: loaded (/etc/systemd/system/studyd.service; enabled)
   Active: active (running) since 四 2026-04-02 19:14:49 CST; 3min 10s ago
 Main PID: 829 (study)
   CGroup: /system.slice/studyd.service
           ├─ 829 /bin/bash /usr/bin/study
           └─4969 sleep 5

4月 02 19:14:49 c001 systemd[1]: Started study server daemon.

9.服务功能验证

实时查看日志,确认服务正常写入数据:

root@c001 ~ 19:17:59#  tail -f /var/log/study.log
2026年 04月 02日 星期四 19:17:19 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:17:24 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:17:29 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:17:34 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:17:39 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:17:44 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:17:49 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:17:54 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:17:59 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:18:04 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:18:09 CST: I'M studying [ Linux 
2026年 04月 02日 星期四 19:18:14 CST: I'M studying [ Linux 

10、常见问题排查

  1. 服务启动失败

○ 排查:检查脚本路径是否正确、是否添加执行权限

○ 命令:chmod +x /usr/bin/study

  1. systemd找不到服务

○ 排查:未执行配置重载命令

○ 命令:systemctl daemon-reload

  1. 日志文件无内容

○ 排查:脚本权限、日志文件路径是否正确

○ 命令:手动执行脚本测试 /usr/bin/study

11、文档总结

  1. 自定义systemd服务 = 业务脚本 + .service 单元配置文件
  2. 核心流程:写脚本→赋权限→建配置→重载配置→启动服务
  3. 自定义服务可通过 systemctl 统一管理,与系统服务无差异
  4. 该案例是Linux服务开发的基础模板,可适配所有自定义后台任务

四、复习 vim

[root@centos7 ~ 14:28:48]# cp /usr/lib/systemd/system/sshd.service .
[root@centos7 ~ 14:28:48]# vim sshd.service
  1. :set nu 显示行号
  2. :3,5d 【或者 光标移动到第三行d3d】 删除3-5行
  3. :5,6d 删除5-6行
  4. :6,9d 删除6-9行
  5. /Open 搜索并定位到特定字符串位置
  6. cw (change word)替换一个单词(自动进入插入模式),输入study,按esc
  7. /sbin 搜索,C(删除到最后,并进入插入模式),补全路径local/bin/study,按esc
  8. ZZ 保存退出。

最终文件内容如下:
[Unit]
Description=study server daemon

[Service]
ExecStart=/usr/local/bin/study

[Install]
WantedBy=multi-user.target

补充:
u 撤销上一步操作
ctrl+r 重做上一步操作

五、分析sshd建立连接流程

操作步骤:

  1. windows 管理员身份打开cmd,执行arp -d 10.1.8.10(自己Linux服务IP)
  2. 开启wireshark,捕捉vmware 8 nat网卡流量
  3. 使用终端登录 Linux 服务
  4. 分析 抓包结果

一、实验基础信息

• 实验环境:Windows 主机 + VMware NAT 模式(VMnet8)+ CentOS 7 Linux 服务器

• Linux 服务器 IP:192.168.100.101

• 核心工具:Windows CMD、Wireshark、SSH 客户端、sshpass

• 核心目标:掌握 SSH 连接流程、OSI 七层对应关系、抓包分析、SSH 配置与非交互登录

二、SSH 连接建立流程 + Wireshark 抓包分析(结合 OSI 七层模型)

(一)抓包操作步骤

  1. 清空 ARP 缓存

Windows 以管理员身份打开 CMD,执行: arp -d 10.1.8.10

C:\Windows\System32>arp -d 192.168.100.101

作用:删除本地 ARP 缓存中 Linux 服务器的 MAC 地址,确保抓包能捕获完整 ARP 解析过程。

  1. 启动 Wireshark 抓包

选择VMware Network Adapter VMnet8(NAT 网卡),开始捕获流量。

image-20260402194721562
  1. 发起 SSH 登录

Windows CMD 执行 SSH 登录命令:

[zy@c001 ~]$ ssh 192.168.100.101
root@192.168.100.101's password:
Last login: Thu Apr  2 19:52:35 2026 from 192.168.100.1
  1. 停止抓包并分析

登录成功后停止抓包,筛选流量分析。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

(二)OSI 七层模型与 SSH 连接对应关系

OSI 七层 层名称 SSH 连接中作用
7层 应用层 SSH 协议本身,负责身份认证、加密数据传输、命令执行
6层 表示层 对 SSH 数据进行加密/解密、编码/解码,保证数据安全
5层 会话层 建立、管理、终止 SSH 会话连接,维护会话状态
4层 传输层 基于 TCP 协议(端口 22),提供可靠传输、三次握手、流量控制
3层 网络层 IP 协议寻址,Windows 与 Linux 之间 IP 路由、ARP 地址解析
2层 数据链路层 MAC 地址寻址,以太网帧封装,物理设备间数据传输
1层 物理层 网卡、网线、网络接口等物理硬件传输比特流

(三)抓包结果核心流量分析

客户端发起 TCP 连接,服务端返回公钥

客户端验证服务端公钥(known_hosts),协商加密算法

双方生成会话密钥,建立加密通道

客户端发送用户名 + 密码(或密钥)进行身份认证

认证通过后,建立交互式会话,可执行远程命令

  1. ARP 解析流量

清空缓存后,Windows 先发送 ARP 请求:谁是 192.168.100.101

Linux 返回 ARP 响应:192.168.100.101的 MAC 地址是 XXX。

对应:OSI 2 层(数据链路)+ 3 层(网络)。

  1. TCP 三次握手

目标端口:22(SSH 默认端口)

○ 第一次:Windows → Linux(SYN)

○ 第二次:Linux → Windows(SYN+ACK)

○ 第三次:Windows → Linux(ACK)

对应:OSI 4 层(传输层)。

  1. SSH 协议握手

双方协商 SSH 版本、加密算法、会话密钥,建立加密通道。

对应:OSI 5/6/7 层。

  1. 身份认证与数据传输

用户名/密码验证、命令执行、日志输出,全程加密传输。

对应:OSI 7 层(应用层)。

三、Linux 端创建普通用户 zy并设置密码

以 root 用户执行,创建专用测试用户:

Bash # 创建用户zy 非交互设置密码为123456(–stdin直接读取密码) echo 123456 | passwd --stdin zy

• 命令解释:

○ useradd:创建系统用户,默认家目录 /home/laoma。

○ passwd --stdin:免交互设置密码,适合脚本/批量操作。

• 执行结果:提示「所有的身份验证令牌已经成功更新」,用户创建完成。

root@c001 ~ 20:14:53# useradd zy
useradd:用户“zy”已存在
root@c001 ~ 20:22:18# echo 123456 | passwd --stdin zy
更改用户 zy 的密码 。
passwd:所有的身份验证令牌已经成功更新。
root@c001 ~ 20:22:46#

四、SSH 客户端配置文件优化(.ssh/config)

(一)前置准备

  1. 生成 .ssh 目录(不存在时执行)

Bash # 临时连接本地,自动生成.ssh目录,按Ctrl+C终止 ssh localhost

  1. 复制系统模板配置文件

cp /etc/ssh/ssh_config .ssh/config

3.编辑.ssh/config文件,添加以下配置:

 vim .ssh/config

Host *
    User 你的默认用户名  # 如root/laoma
    StrictHostKeyChecking no
    UserKnownHostsFile /dev/null  # 可选,不记录主机key

(二)配置参数解释

  1. Host *:匹配所有远程主机,配置全局生效。

  2. User root:SSH 登录时默认使用 root 用户,无需手动输入 root@IP。

  3. StrictHostKeyChecking no:跳过主机密钥确认,避免首次登录提示「Are you sure you want to continue connecting」。

(四)验证配置

Bash ssh 192.168.100.101

• 效果:直接提示输入密码,无密钥确认提示,默认使用 root 登录。

五、sshpass 非交互密码登录工具(免手动输密码)

(一)安装 sshpass

Bash # root用户执行,安装工具 yum install -y sshpass

(二)前置操作(首次连接信任主机)

Bash ssh root@192.168.100.101# 提示密钥指纹时,输入yes,添加到已知主机列表

(三)sshpass 核心用法

sshpass语法格式:sshpass -p 密码 ssh 用户名@IP 命令 sshpass -p redhat ssh root@192.168.100.101 id

执行结果:

root@c001 ~ 20:32:29# sshpass -p redhat ssh root@192.168.100.101 id
uid=0(root) gid=0(root) 组=0(root)

• 作用:非交互输入密码,适合脚本自动化远程操作,无需人工干预。

六:window 终端工具使用秘钥登录Linux

  1. 生成密钥对(私钥和公钥)

  2. 将公钥复制给Linux服务器

  3. 测试

  4. 生成密钥对(私钥和公钥)
    xshell 为例: 工具-用户秘钥管理者-生成,打开界面基本上是下一步即可。
    找到私钥-属性-公钥标签,复制公钥。

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6QegQ79cwBfXWTS97wTl5BQyFG3Rh90X1Wunc2g3ZZXT1FfQ1IdMLHscmdecWIpI66VOf/SBmLfFkVdPqIEVoe7qvXKh07wYAM0pC211rjbP5K7MJpyiLBk6XOic6nl0Ve8q6keV0GnuXxvNfEHQrqFeYMhYPwd7XZl453Sd1gN8gv+1xdAfl9SKqaAlN3QwKBm6di38Sjpiwzhbzh9Ikc385NLTkjoVDB0RLFAjh5wptv0FMfKarZB8XcL+cuytLpMHk1squCdQ89A/QGJHw9Lgl7qMFwXsqPx2QaE1AzEkInWYYyaLrMfOP41Tw2PbemXQI+lYJKgEOD7AdZggRQ== rsa 2centos7
  1. 写入对方文件~/.ssh/authorized_keys
[zy@centos7 ~ 14:29:01]$ vim .ssh/authorize_keys
[zy@centos7 ~ 14:43:02]$ ll .ssh/authorize_keys 
-rw-rw-r-- 1 zy zy 393 Apr  6 14:43 .ssh/authorize_keys
[zy@centos7 ~ 14:43:21]$ chmod 600 .ssh/authorize_keys 

  1. 登录时候,选择验证方式:公钥,并指定对应公钥。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

4.测试:直接连接主机而不需要输入密码

思考:

刚刚实现 windows 使用 laoma 用户秘钥登录 centos7,如何实现 windows 使用 root 用户秘钥登录 centos7?

[root@centos7 ~ 10:19:01]# cat /home/laoma/.ssh/authorized_keys >> /root/.ssh/authorized_keys

七:ssh 工具配置文件

1、 ~/.ssh/config,用户级别,给用户自己定制,优先级更高
/etc/ssh/ssh_config,系统(全局)级别,默认设置,针对系统中所有用户生效
sshd 服务配置文件
/etc/ssh/sshd_config,针对sshd服务生效。

2、控制root用户登录。因为所有Linux系统管理员都是root,防止暴力破解。

PermitRootLogin no 禁止root用户登录
PermitRootLogin prohibit-password 禁止root用户使用密码登录
[root@10 ~ 20:41:38]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.20260403  
[root@10 ~ 20:41:55]# vim /etc/ssh/sshd_config

2.1:/Root,定位。如果第一个搜索到的内容不是需要的,使用n(next)查找下一个。

2.2:yy 复制对应行,p 粘贴到下一行。(保留原有行)

2.3:自行删除第一#

2.4:w 跳转到yes位置,cw 替换一个单词,输入prohibit-password,按esc

2.5:ZZ 保存退出。

最终配置:

#PermitRootLogin no
PermitRootLogin prohibit-password

2.6:重新加载服务
[root@centos7 ~ 10:34:57]# systemctl reload sshd

2.7:验证

禁止root用户使用密码登录。

配置含义:
设置了root只能用密钥登录,彻底禁用密码登录,其他用户可以使用密钥登录

2.8:配置「用户 / 组登录白名单 / 黑名单」(可选)

配置项 作用 示例
AllowUsers 仅允许列出的用户登录(白名单) AllowUsers laoma zy
DenyUsers 禁止列出的用户登录(黑名单) DenyUsers test
AllowGroups 仅允许列出的组登录 AllowGroups wheel
DenyGroups 禁止列出的组登录 DenyGroups testgrp

配置方法:
在sshd_config末尾新增一行(比如只允许 zy root登录):

AllowUsers zy root

注意:多个用户 / 组用空格分隔,配置后只有列出来的用户能登录!

2.9验证

DenyUsers, AllowUsers, DenyGroups,AllowGroups
例如,AllowUsers zy root

验证配置是否生效
验证 1:root 密码登录是否被禁止

C:\Users\Lenovo>ssh root@10.1.8.10                                                                                      
root@10.1.8.10's password:                                                                                              
Permission denied, please try again.                                                                                    
root@10.1.8.10's password: 

结论:root用户被禁止密码登录

验证 2:zy 密码登录是否被禁止

C:\Users\Lenovo>ssh zy@10.1.8.10                                                                                        
zy@10.1.8.10's password:                                                                                                
Last login: Mon Apr  6 21:08:40 2026 from 10.1.8.1                                                                      
[zy@10 ~ 21:44:14]$  

结论:zy用户,没有被禁止密码登录

八、日志:

  1. systemd-journal (systemd子系统)

  2. rsyslog 服务对应
    rsyslog 将日志记录到对应的文件中:

    /var/log/messages | 大多数系统日志(排除认证、邮件、定时任务、调试日志)

    ​ /var/log/secure | 安全/认证相关日志(如ssh登录、用户验证)
    /var/log/maillog | 邮件服务相关日志(如sendmail/postfix)
    ​ /var/log/cron | 定时任务执行日志(crontab/anacron)
    ​ /var/log/boot.log | 系统启动过程中的非syslog控制台消息

查看日志内容:

[root@centos7 ~ 17:27:11]# tail -f /var/log/secure 
Apr  7 14:55:27 centos7 polkitd[709]: Unregistered Authentication Agent for unix-process:3345:464551 (system bus name :1.100, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus)
Apr  7 15:47:31 centos7 sshd[1261]: pam_unix(sshd:session): session closed for user root
Apr  7 16:57:19 centos7 sshd[3428]: Connection closed by 10.1.8.1 port 59390 [preauth]
Apr  7 17:25:49 centos7 login: pam_unix(login:session): session opened for user root by LOGIN(uid=0)

新增加一个ssh连接,日志更新内容如下:

Apr  7 17:26:21 centos7 sshd[3473]: Accepted password for root from 10.1.8.1 port 57567 ssh2
Apr  7 17:26:21 centos7 sshd[3473]: pam_unix(sshd:session): session opened for user root by (uid=0)

断开连接,日志更新内容如下:

pr  7 18:39:43 centos7 sshd[3569]: pam_unix(sshd:session): session closed for user root

3.模拟sshd故障,并排故。

3.1首先模拟一个故障:

[root@centos7 ~ 18:51:50]# cp /etc/ssh/sshd_config{,.0403}
[root@centos7 ~ 18:52:56]# ls /etc/ssh
moduli            ssh_host_ecdsa_key        ssh_host_rsa_key
ssh_config        ssh_host_ecdsa_key.pub    ssh_host_rsa_key.pub
sshd_config       ssh_host_ed25519_key
sshd_config.0403  ssh_host_ed25519_key.pub
[root@centos7 ~ 18:53:01]# echo hello world >>/etc/ssh/ssh
ssh_config                ssh_host_ed25519_key
sshd_config               ssh_host_ed25519_key.pub
sshd_config.0403          ssh_host_rsa_key
ssh_host_ecdsa_key        ssh_host_rsa_key.pub
ssh_host_ecdsa_key.pub    
[root@centos7 ~ 18:53:01]# echo hello world >>/etc/ssh/sshd_config
[root@centos7 ~ 18:54:06]# cat /etc/ssh/sshd_config
# Example of overriding settings on a per-user basis
#Match User anoncvs
#	X11Forwarding no
#	AllowTcpForwarding no
#	PermitTTY no
#	ForceCommand cvs server
UseDNS no
hello world
[root@centos7 ~ 18:54:22]# systemctl restart sshd
Job for sshd.service failed because the control process exited with error code. See "systemctl status sshd.service" and "journalctl -xe" for details.

3.2根据提示,查看状态:

[root@centos7 ~ 18:54:41]# systemctl status sshd.service 
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Tue 2026-04-07 18:57:30 CST; 37s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
  Process: 3674 ExecStart=/usr/sbin/sshd -D $OPTIONS (code=exited, status=255)
 Main PID: 3674 (code=exited, status=255)

Apr 07 18:57:30 centos7.zy.cloud systemd[1]: sshd.service: main p...
Apr 07 18:57:30 centos7.zy.cloud systemd[1]: Failed to start Open...
Apr 07 18:57:30 centos7.zy.cloud systemd[1]: Unit sshd.service en...
Apr 07 18:57:30 centos7.zy.cloud systemd[1]: sshd.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

发现状态日志没有对应提示信息

3.3继续查看message日志:

[root@centos7 ~ 18:58:08]# tail /var/log/messages 
Apr  7 19:01:02 centos7 systemd: sshd.service failed.
Apr  7 19:01:44 centos7 systemd: sshd.service holdoff time over, scheduling restart.
Apr  7 19:01:44 centos7 systemd: Stopped OpenSSH server daemon.
Apr  7 19:01:44 centos7 systemd: Starting OpenSSH server daemon...
Apr  7 19:01:44 centos7 sshd: /etc/ssh/sshd_config: line 141: Bad configuration option: hello
Apr  7 19:01:44 centos7 sshd: /etc/ssh/sshd_config: terminating, 1 bad configuration options
Apr  7 19:01:44 centos7 systemd: sshd.service: main process exited, code=exited, status=255/n/a
Apr  7 19:01:44 centos7 systemd: Failed to start OpenSSH server daemon.
Apr  7 19:01:44 centos7 systemd: Unit sshd.service entered failed state.
Apr  7 19:01:44 centos7 systemd: sshd.service failed.

发现故障:line 141: Bad configuration option: hello

3.4根据提示解决问题,删除hello world

[root@centos7 ~ 19:02:11]# vim /etc/ssh/sshd_config
[root@centos7 ~ 19:05:33]# systemctl restart sshd
[root@centos7 ~ 19:08:37]# systemctl is-active sshd
active

一切恢复正常,解决了问题

4.故障2:移走/etc/ssh/sshd_config配置文件,导致ssh配置文件丢失,导致服务器起不来,ssh服务必须读取/etc/ssh/sshd_config配置文件才能启动

4.1模拟故障:

# 备份sshd配置文件(防止彻底搞丢)
[root@centos7 ~ 19:08:53]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# 1. 把sshd配置文件移到当前目录(等效文件丢失)
[root@centos7 ~ 19:15:11]# mv /etc/ssh/sshd_config ~
# 2. 重启sshd服务 → 触发启动失败
[root@centos7 ~ 19:15:42]# systemctl restart sshd
Job for sshd.service failed because the control process exited with error code. See "systemctl status sshd.service" and "journalctl -xe" for details.

4.2排查故障

# 1. 查看sshd服务状态(初步看失败)
[root@centos7 ~ 19:16:06]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Tue 2026-04-07 19:18:12 CST; 23s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
  Process: 3813 ExecStart=/usr/sbin/sshd -D $OPTIONS (code=exited, status=1/FAILURE)
 Main PID: 3813 (code=exited, status=1/FAILURE)

Apr 07 19:18:12 centos7.zy.cloud systemd[1]: sshd.service: main p...
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: Failed to start Open...
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: Unit sshd.service en...
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: sshd.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
# 2. 查看sshd专属日志(精准找原因:核心命令)
[root@centos7 ~ 19:18:36]# journalctl -e -u sshd
Apr 07 19:16:06 centos7.zy.cloud systemd[1]: Unit sshd.service enter
Apr 07 19:16:06 centos7.zy.cloud systemd[1]: sshd.service failed.
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: sshd.service holdoff ti
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: Stopped OpenSSH server 
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: Starting OpenSSH server
Apr 07 19:16:48 centos7.zy.cloud sshd[3809]: /etc/ssh/sshd_config: N
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: sshd.service: main proc
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: Failed to start OpenSSH
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: Unit sshd.service enter
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: sshd.service failed.
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: sshd.service holdoff ti
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: Stopped OpenSSH server 
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: Starting OpenSSH server
Apr 07 19:17:30 centos7.zy.cloud sshd[3811]: /etc/ssh/sshd_config: N
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: sshd.service: main proc
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: Failed to start OpenSSH
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: Unit sshd.service enter
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: sshd.service failed.
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: sshd.service holdoff ti
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: Stopped OpenSSH server 
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: Starting OpenSSH server
Apr 07 19:18:12 centos7.zy.cloud sshd[3813]: /etc/ssh/sshd_config: N
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: sshd.service: main proc
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: Failed to start OpenSSH
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: Unit sshd.service enter
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: sshd.service failed.
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: sshd.service holdoff ti
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: Stopped OpenSSH server 
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: Starting OpenSSH server
Apr 07 19:18:55 centos7.zy.cloud sshd[3822]: /etc/ssh/sshd_config: N
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: sshd.service: main proc
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: Failed to start OpenSSH
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: Unit sshd.service enter
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: sshd.service failed.
lines 180-213/213 (END)
Apr 07 19:16:06 centos7.zy.cloud systemd[1]: Unit sshd.service entered failed state.
Apr 07 19:16:06 centos7.zy.cloud systemd[1]: sshd.service failed.
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: sshd.service holdoff time over, scheduling restart.
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: Stopped OpenSSH server daemon.
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: Starting OpenSSH server daemon...
Apr 07 19:16:48 centos7.zy.cloud sshd[3809]: /etc/ssh/sshd_config: No such file or directory
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: sshd.service: main process exited, code=exited, status=1/FAILURE
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: Failed to start OpenSSH server daemon.
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: Unit sshd.service entered failed state.
Apr 07 19:16:48 centos7.zy.cloud systemd[1]: sshd.service failed.
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: sshd.service holdoff time over, scheduling restart.
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: Stopped OpenSSH server daemon.
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: Starting OpenSSH server daemon...
Apr 07 19:17:30 centos7.zy.cloud sshd[3811]: /etc/ssh/sshd_config: No such file or directory
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: sshd.service: main process exited, code=exited, status=1/FAILURE
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: Failed to start OpenSSH server daemon.
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: Unit sshd.service entered failed state.
Apr 07 19:17:30 centos7.zy.cloud systemd[1]: sshd.service failed.
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: sshd.service holdoff time over, scheduling restart.
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: Stopped OpenSSH server daemon.
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: Starting OpenSSH server daemon...
Apr 07 19:18:12 centos7.zy.cloud sshd[3813]: /etc/ssh/sshd_config: No such file or directory
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: sshd.service: main process exited, code=exited, status=1/FAILURE
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: Failed to start OpenSSH server daemon.
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: Unit sshd.service entered failed state.
Apr 07 19:18:12 centos7.zy.cloud systemd[1]: sshd.service failed.
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: sshd.service holdoff time over, scheduling restart.
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: Stopped OpenSSH server daemon.
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: Starting OpenSSH server daemon...
Apr 07 19:18:55 centos7.zy.cloud sshd[3822]: /etc/ssh/sshd_config: No such file or directory
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: sshd.service: main process exited, code=exited, status=1/FAILURE
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: Failed to start OpenSSH server daemon.
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: Unit sshd.service entered failed state.
Apr 07 19:18:55 centos7.zy.cloud systemd[1]: sshd.service failed.

根据日志发现故障:

Apr 07 19:16:48 centos7.zy.cloud sshd[3809]: /etc/ssh/sshd_config: No such file or directory

没有/etc/ssh/sshd_config这个配置文件

4.3解决故障:

# 1. 查询:sshd_config 属于哪个安装包
[root@centos7 ~ 19:26:28]# yum provides /etc/ssh/sshd_config
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
openssh-server-7.4p1-21.el7.x86_64 : An open source SSH server
                                   : daemon
Repo        : base
Matched from:
Filename    : /etc/ssh/sshd_config



openssh-server-7.4p1-22.el7_9.x86_64 : An open source SSH server
                                     : daemon
Repo        : updates
Matched from:
Filename    : /etc/ssh/sshd_config



openssh-server-7.4p1-23.el7_9.x86_64 : An open source SSH server
                                     : daemon
Repo        : updates
Matched from:
Filename    : /etc/ssh/sshd_config



openssh-server-7.4p1-23.el7_9.x86_64 : An open source SSH server
                                     : daemon
Repo        : @updates
Matched from:
Filename    : /etc/ssh/sshd_config
# 2. 安装下载工具(yumdownloader)
[root@centos7 ~ 19:29:50]# yum install -y yum-utils
# 3. 下载openssh-server安装包
[root@centos7 ~ 19:30:14]# yumdownloader openssh-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
openssh-server-7.4p1-23.el7_9.x86_64.rpm       | 459 kB   00:00     
# 4. 查看rpm包内的文件(确认包含sshd_config)
[root@centos7 ~ 19:32:41]# rpm2cpio openssh-server-*.rpm | cpio -t
./etc/pam.d/sshd
./etc/ssh/sshd_config
./etc/sysconfig/sshd
./usr/lib/systemd/system/sshd-keygen.service
./usr/lib/systemd/system/sshd.service
./usr/lib/systemd/system/sshd.socket
./usr/lib/systemd/system/sshd@.service
./usr/lib64/fipscheck/sshd.hmac
./usr/libexec/openssh/sftp-server
./usr/sbin/sshd
./usr/sbin/sshd-keygen
./usr/share/man/man5/moduli.5.gz
./usr/share/man/man5/sshd_config.5.gz
./usr/share/man/man8/sftp-server.8.gz
./usr/share/man/man8/sshd.8.gz
./var/empty/sshd
1946 blocks


# 5. 从rpm包中提取sshd_config文件
[root@centos7 ~ 19:31:14]# rpm2cpio openssh-server-*.rpm | cpio -id ./etc/ssh/sshd_config
1946 blocks
#6.下载tree安装包,直观看出etc目录结构
[root@centos7 ~ 19:31:32]# yum install -y tree
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================
 Package     Arch          Version                Repository   Size
====================================================================
Installing:
 tree        x86_64        1.6.0-10.el7           base         46 k

Transaction Summary
====================================================================
Install  1 Package

Total download size: 46 k
Installed size: 87 k
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm                   |  46 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : tree-1.6.0-10.el7.x86_64                         1/1 
  Verifying  : tree-1.6.0-10.el7.x86_64                         1/1 

Installed:
  tree.x86_64 0:1.6.0-10.el7                                        

Complete!
#查看sshd_config文件是否恢复了
[root@centos7 ~ 19:32:27]# tree etc
etc
└── ssh
    └── sshd_config

1 directory, 1 file

#你用 rpm2cpio 提取的文件,位置在这里:
#~/etc/ssh/sshd_config
#(是你当前目录下新建的 etc 文件夹,不是系统的 /etc/ssh)
#文件没放到系统指定路径 = 配置文件依然是丢失状态
#sshd 还是找不到文件 = 启动必然失败
# 6. 把提取的文件放回原路径(关键!)
[root@centos7 ~ 19:39:10]# mv etc/ssh/sshd_config /etc/ssh/
#验证sshd服务是否可以启动
[root@centos7 ~ 19:42:27]# systemctl restart sshd
[root@centos7 ~ 19:43:59]# systemctl is-active sshd
active

Dependencies Resolved

====================================================================
Package Arch Version Repository Size

Installing:
tree x86_64 1.6.0-10.el7 base 46 k

Transaction Summary

Install 1 Package

Total download size: 46 k
Installed size: 87 k
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm | 46 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : tree-1.6.0-10.el7.x86_64 1/1
Verifying : tree-1.6.0-10.el7.x86_64 1/1

Installed:
tree.x86_64 0:1.6.0-10.el7

Complete!
#查看sshd_config文件是否恢复了
[root@centos7 ~ 19:32:27]# tree etc
etc
└── ssh
└── sshd_config

1 directory, 1 file

#你用 rpm2cpio 提取的文件,位置在这里:
#~/etc/ssh/sshd_config
#(是你当前目录下新建的 etc 文件夹,不是系统的 /etc/ssh)
#文件没放到系统指定路径 = 配置文件依然是丢失状态
#sshd 还是找不到文件 = 启动必然失败

6. 把提取的文件放回原路径(关键!)

[root@centos7 ~ 19:39:10]# mv etc/ssh/sshd_config /etc/ssh/
#验证sshd服务是否可以启动
[root@centos7 ~ 19:42:27]# systemctl restart sshd
[root@centos7 ~ 19:43:59]# systemctl is-active sshd
active


Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐