Linux 运维实战笔记(一):系统资源监控、服务管理与 SSH 配置
·
计算机核心资源:
CPU: 计算(lscpu)
[root@c001 ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 2
座: 2
NUMA 节点: 1
厂商 ID: AuthenticAMD
CPU 系列: 25
型号: 97
型号名称: AMD Ryzen 9 7945HX with Radeon Graphics
步进: 2
CPU MHz: 2495.313
BogoMIPS: 4990.62
超管理器厂商: VMware
虚拟化类型: 完全
L1d 缓存: 32K
L1i 缓存: 32K
L2 缓存: 1024K
L3 缓存: 32768K
NUMA 节点0 CPU: 0-3
内存: 缓存数据(掉电丢失)
按CPU使用率降序排序进程
[root@c001 ~]# top
top - 10:20:50 up 1:01, 2 users, load average: 2.39, 1.09, 0.46
Tasks: 437 total, 4 running, 433 sleeping, 0 stopped, 0 zombie
%Cpu(s): 66.2 us, 3.0 sy, 0.0 ni, 30.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
54865 root 20 0 108072 616 524 R 99.7 0.0 4:30.80 md5sum
57233 root 20 0 108072 620 524 R 99.7 0.0 1:59.01 md5sum
57895 root 20 0 108072 620 524 R 99.7 0.0 1:16.74 md5sum
2275 root 20 0 133900 5484 4088 S 0.7 0.1 0:21.52 sshd
2363 root 20 0 125060 2952 1168 S 0.3 0.1 0:07.34 top
1 root 20 0 50868 7228 2524 S 0.0 0.2 0:01.08 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
按内存使用率降序排序进程
[root@c001 ~]# top
top - 10:27:58 up 1:09, 2 users, load average: 2.92, 2.50, 1.38
Tasks: 436 total, 4 running, 432 sleeping, 0 stopped, 0 zombie
%Cpu(s): 65.9 us, 2.9 sy, 0.0 ni, 31.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 3868576 total, 365184 used, 3503392 free, 888 buffers
KiB Swap: 4079612 total, 0 used, 4079612 free. 155940 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
54865 root 20 0 108072 616 524 R 99.7 0.0 11:37.77 md5sum
57233 root 20 0 108072 620 524 R 99.7 0.0 9:05.95 md5sum
57895 root 20 0 108072 620 524 R 99.7 0.0 8:23.63 md5sum
2275 root 20 0 133900 5484 4088 S 0.3 0.1 0:23.39 sshd
57943 root 20 0 124100 1936 1176 R 0.3 0.1 0:00.71 top
1 root 20 0 50868 7228 2524 S 0.0 0.2 0:01.08 systemd
CPU指标
%Cpu(s): 65.8 us, 2.7 sy, 0.0 ni, 31.4 id, 0.0 wa, 0.0 hi, 0.1 si, 0.0 st
内存指标
KiB Mem: 3868576 total, 365184 used, 3503392 free, 888 buffers
KiB Swap: 4079612 total, 0 used, 4079612 free. 155940 cached Mem
硬盘: 持久化存储数据。
[root@c001 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 4.4G 46G 9% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/nvme0n1p2 497M 88M 409M 18% /boot
/dev/nvme0n1p1 200M 9.6M 191M 5% /boot/efi
/dev/mapper/centos-home 146G 33M 146G 1% /home
网络: 传播数据
[root@c001 ~]# iotop
Total DISK READ : 0.00 B/s | Total DISK WRITE : 0.00 B/s
Actual DISK READ: 0.00 B/s | Actual DISK WRITE: 0.00 B/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
65747 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.98 % [kworker/0:0]
1 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % systemd --s~erialize 23
2 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kthreadd]
3 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/0]
5 be/0 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kworker/0:0H]
7 rt/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [migration/0]
8 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [rcu_bh]
9 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [rcuob/0]
10 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [rcuob/1]
硬盘名称:sda 第一块sata接口硬盘硬盘 sdb
[root@c001 ~]# sar -dp 1
Linux 3.10.0-123.el7.x86_64 (c001) 2026年04月02日 _x86_64_ (4 CPU)
10时34分52秒 DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
10时34分53秒 nvme0n1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分53秒 sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分53秒 centos-swap 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分53秒 centos-root 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分53秒 centos-home 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分53秒 DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
10时34分54秒 nvme0n1 3.00 0.00 26.00 8.67 0.00 0.33 0.33 0.10
10时34分54秒 sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分54秒 centos-swap 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分54秒 centos-root 3.00 0.00 26.00 8.67 0.00 0.33 0.33 0.10
10时34分54秒 centos-home 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
^C
10时34分54秒 DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
10时34分55秒 nvme0n1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分55秒 sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分55秒 centos-swap 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分55秒 centos-root 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时34分55秒 centos-home 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
平均时间: nvme0n1 1.20 0.00 10.44 8.67 0.00 0.33 0.33 0.04
平均时间: sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: centos-swap 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均时间: centos-root 1.20 0.00 10.44 8.67 0.00 0.33 0.33 0.04
平均时间: centos-home 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
服务管理
pid为1的程序是systemd,也是系统第一个进程,负责操作系统整个资源的管理。
系统中服务也由systemd管理。
systemctl 命令语法
systemctl action service-name
action: status start stop restart enalbe disable is-active is-enabled
service-name: sshd firewalld mysqld
systemd与systemctl关系:
- systemd 一直运行的程序(守护进程)
- systemctl 是给systemd下指令的工具,人与机器之间交互使用。
查看服务状态
[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 11:01:24 CST; 18min ago
Process: 1265 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS)
Main PID: 1269 (sshd)
CGroup: /system.slice/sshd.service
└─1269 /usr/sbin/sshd -D
运行记录
4月 02 11:01:24 c001 systemd[1]: Started OpenSSH server daemon.
4月 02 11:01:24 c001 sshd[1269]: Server listening on 0.0.0.0 port 22.
4月 02 11:01:24 c001 sshd[1269]: Server listening on :: port 22.
4月 02 11:02:42 c001 sshd[2286]: Accepted password for root from 192.168.220.1...h2
Hint: Some lines were ellipsized, use -l to show in full.
重点关注:
1. ,Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
配置文件位置 /usr/lib/systemd/system/sshd.service
第一个enable 代表开机自动启动
第二个enable 暂不关注
2. Active: active (running)
停止服务
[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 17:22:32 CST; 11s ago
Main PID: 1272 (code=exited, status=0/SUCCESS)
4月 02 15:53:07 c001 sshd[41153]: Accepted password for root from 192.168.220....h2
4月 02 15:53:11 c001 sshd[41298]: Accepted publickey for root from 192.168.220...c6
4月 02 15:53:29 c001 sshd[41478]: Accepted password for root from 192.168.220....h2
4月 02 15:53:37 c001 sshd[41745]: Accepted publickey for root from 192.168.220...c6
4月 02 15:53:46 c001 sshd[41863]: pam_unix(sshd:auth): authentication failure;...ot
4月 02 15:53:46 c001 sshd[41863]: pam_succeed_if(sshd:auth): requirement "uid ...t"
4月 02 15:53:47 c001 sshd[41863]: Failed password for root from 192.168.220.1 ...h2
4月 02 15:53:50 c001 sshd[41863]: Accepted password for root from 192.168.220....h2
4月 02 17:22:32 c001 systemd[1]: Stopping OpenSSH server daemon...
4月 02 17:22:32 c001 sshd[1272]: Received signal 15; terminating.
4月 02 17:22:32 c001 systemd[1]: Stopped OpenSSH server daemon.
Hint: Some lines were ellipsized, use -l to show in full.
# 使用工具再次连接,连接不上去,服务未启动。
# 启动服务
[root@c001 ~]# systemctl start sshd
[root@c001 ~]# systemctl is-active sshd
active
# 使用工具再次连接,能连接上,服务启动。
sshd服务开机启动测试
# 禁止sshd服务开机启动
[root@c001 ~]# systemctl disable sshd
rm '/etc/systemd/system/multi-user.target.wants/sshd.service'
[root@c001 ~]# systemctl is-enabled sshd
disabled
# 重启验证:机器启动后是否可以通过终端工具登录(不行)
[root@c001 ~]# reboot
# 设置sshd服务开机启动
[root@c001 ~]# systemctl enable sshd
ln -s '/usr/lib/systemd/system/sshd.service' '/etc/systemd/system/multi-user.target.wants/sshd.service'
[root@c001 ~]# systemctl is-enabled sshd
enabled
# 重启验证:机器启动后是否可以通过终端工具登录(行)
[root@c001 ~]# reboot
开发一个服务
开发服务流程:
- 准备服务程序 /usr/local/bin/study
- 准备服务配置文件 /etc/systemd/system/studyd.service (程序后面的d,daemon,守护称呼)
- 通知systemd 变化 systemctl daemon-reload
- 激活服务 systemctl enable studyd --now
[root@centos7 ~ 16:15:19]# vim /usr/local/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
[root@c001 bin]# chmod +x /usr/local/bin/study
[root@c001 bin]# cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/studyd.service
[root@c001 bin]# vim /etc/systemd/system/studyd.service
[Unit]
Description=study server daemon
[Service]
ExecStart=/usr/local/bin/study
[Install]
WantedBy=multi-user.target
[root@c001 system]# systemctl daemon-reload
[root@c001 system]# systemctl enable studyd && systemctl start studyd
[root@c001 system]# systemctl status studyd
studyd.service - study server daemon
Loaded: loaded (/etc/systemd/system/studyd.service; enabled)
Active: active (running) since 四 2026-04-02 13:24:29 CST; 3s ago
Main PID: 29688 (study)
CGroup: /system.slice/studyd.service
├─29688 /bin/bash /usr/local/bin/study
└─29690 sleep 5
4月 02 13:24:29 c001 systemd[1]: Started study server daemon.
[root@c001 system]# tail -f /var/log/study.log
2026年 04月 02日 星期四 13:06:41 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:06:46 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:06:51 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:06:56 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:17:57 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:18:02 CST: I'M studying [ Linux ]
复习 vim
[root@c001 system]# cp /usr/lib/systemd/system/sshd.service .
[root@c001 system]# 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 192.168.220.147(自己Linux服务IP)
2. 开启wireshark,捕捉vmware 8 nat网卡流量
3. 使用终端登录 Linux 服务
4. 分析 抓包结果
ssh 工具使用
windows中使用普通命令行操作:
- ssh 192.168.220.147
- ssh root@192.168.220.147或者ssh 7854zx@192.168.220.147
3-1. ssh 7854zx@192.168.220.147 hostname
3-2. ssh 7854zx@192.168.220.147 id
# 为自己创建一个普通 laoma 用户,并设置密码
[root@c001 ~]# useradd 7854zx
[root@c001 ~]# echo 123456 | passwd --stdin 7854zx
更改用户 7854zx 的密码 。
passwd:所有的身份验证令牌已经成功更新。
配置 ssh 工具配置文件
# 如果.ssh目录不存在,执行以下命令
[7854zx@c001 ~]$ ssh localhost # 然后ctrl+c 停止即可
# 准备配置文件
[7854zx@c001 ~]$ cp /etc/ssh/ssh_config .ssh/config
[7854zx@c001 ~]$ vim .ssh/config
# 删除前19行
# 取消相应行注释
Host *
# 新增 User 行:指明默认登录用户
User root
# 首次登录不要提示输入yes/no(不要对主机key校验)
# StrictHostKeyChecking ask
StrictHostKeyChecking no
# 验证
[7854zx@c001 ~]$ ssh 192.168.220.147
Warning: Permanently added '192.168.220.147' (ECDSA) to the list of known hosts.
root@192.168.220.147's password:
Last login: Thu Apr 2 17:35:39 2026 from c001
ssh命令行传递密码工具 sshpass
[root@c001 ~]# yum install -y sshpass
# 确保之前连接过,记得输入yes
# 接下来传递密码redhat登录:
[root@c001 ~]# sshpass -p 123456 ssh root@192.168.220.147 id
# 显示结果如下
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
更多推荐

所有评论(0)