2026.4.2随堂笔记
一、计算机核心资源:
-
cpu:计算(lscpu)
-
内存:缓存数据(掉电丢失)
-
硬盘:持久化存储数据
-
网络:传播数据
二、查看cpu
bash
[root@centos01 ~ 09:19:33]#
[root@centos01 ~ 09:19:33]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2
On-line CPU(s) list: 0,1
Thread(s) per core: 1
Core(s) per socket: 1
[root@centos01 ~ 09:39:01]#
2.1查看使用情况
bash
[root@centos01 ~ 09:41:20]# top
top - 09:41:43 up 4:00, 3 users, load average: 0.00, 0.01, 0.05
Tasks: 398 total, 1 running, 397 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.8 sy, 0.0 ni, 99.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 3869044 total, 721692 used, 3147352 free, 1084 buffers
KiB Swap: 4079612 total, 0 used, 4079612 free. 243772 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 53812 7656 2528 S 0.0 0.2 0:01.55 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd+
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0+
7 root rt 0 0 0 0 S 0.0 0.0 0:00.33 migration+
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
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/5
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/6
cpu指标
%Cpu(s): 0.0 us,(user用户) 0.8 sy,(system系统)
2.2给系统添加负载
bash
[root@centos01 ~ 09:41:44]# md5sum /dev/zero &
[1] 6019
# 查看
[root@centos01 ~ 09:44:06]# top
top - 09:45:33 up 4:04, 2 users, load average: 0.76, 0.26, 0.13
Tasks: 397 total, 3 running, 394 sleeping, 0 stopped, 0 zombie
%Cpu(s): 39.4 us, 1.0 sy, 0.0 ni, 59.4 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
KiB Mem: 3869044 total, 718820 used, 3150224 free, 1084 buffers
KiB Swap: 4079612 total, 0 used, 4079612 free. 243816 cached Mem
cpu指标
%Cpu(s): 39.4 us, 1.0 sy,
2.3磁盘容量
bash
[root@centos01 ~ 09:49:50]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 46G 3.4G 43G 8% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 80K 1.9G 1% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
[root@centos01 ~ 10:06:26]#
2.4磁盘速度 模拟下载
bash
[root@centos01 ~ 10:06:26]# 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
[root@centos01 ~ 09:49:28]# yum install -y iotop
iotop 查看每个进程的读写速度,可以排序
2.5硬盘名称:sda 第一块sata接口硬盘硬盘 sdb
bash
[root@centos01 ~ 10:08:44]# sar -dp 1
Linux 3.10.0-123.el7.x86_64 (centos01) 2026年04月02日 _x86_64_ (2 CPU)
10时09分45秒 DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
10时09分46秒 sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时09分46秒 sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时09分46秒 centos-swap 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10时09分46秒 centos-root 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
单位是k rd_sec/s wr_sec/s
2.6清理文件
bash
[root@c001 ~ 19:06:21]# rm -f CentOS-7-x86_64-DVD-2207-02.iso*
三、服务管理
3.1 pid为1的程序是systemd,也是系统第一个进程,负责操作系统整个资源的管理。
3.1.1 系统中服务也由systemd管理。
- systemctl action service-name
- action: status start stop restart enalbe disable is-active is-enabled
- service-name: sshd firewalld mysqld
3.1.2 systemd与systemctl关系:
- systemd 一直运行的程序(守护进程)
- systemctl 是给systemd下指令的工具,人与机器之间交互使用。
3.1.3 类比与web服务器:
- httpd
- 浏览器访问页面
3.2#查看服务状态
bash
[root@c001 ~ 19:12:01]# systemctl status sshd
sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
Active: active (running) since 四 2026-04-02 18:53:43 CST; 18min ago
Process: 1117 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS)
Main PID: 1129 (sshd)
CGroup: /system.slice/sshd.service
└─1129 /usr/sbin/sshd -D
4月 02 18:53:43 c001 systemd[1]: Started OpenSSH server daemon.
4月 02 18:53:43 c001 sshd[1129]: Server listening on 0.0.0.0 port 22.
4月 02 18:53:43 c001 sshd[1129]: Server listening on :: port 22.
4月 02 18:53:58 c001 sshd[2360]: Accepted password for root from 192.168.1...h2
4月 02 18:54:52 c001 sshd[2382]: Accepted password for root from 192.168.1...h2
4月 02 19:00:37 c001 sshd[2409]: Accepted password for root from 192.168.1...h2
4月 02 19:02:02 c001 sshd[2454]: Accepted password for root from 192.168.1...h2
4月 02 19:03:04 c001 sshd[2479]: Accepted password for root from 192.168.1...h2
Hint: Some lines were ellipsized, use -l to show in full.
[root@c001 ~ 19:12:24]#
3.3 # 使用工具再次连接,连接不上去,服务未启动。
bash
[root@c001 ~ 19:12:24]# systemctl start sshd
[root@c001 ~ 19:13:45]# systemctl is-active sshd
active
3.4# 使用工具再次连接,能连接上,服务启动。
bash
[root@c001 ~ 19:14:00]# systemctl disable sshd
rm '/etc/systemd/system/multi-user.target.wants/sshd.service'
[root@c001 ~ 19:14:46]# systemctl is-enabled sshd
disabled
3.5 设置sshd服务开机启动
bash
[root@c001 ~ 19:15:07]# systemctl enable sshd
ln -s '/usr/lib/systemd/system/sshd.service' '/etc/systemd/system/multi-user.target.wants/sshd.service'
[root@c001 ~ 19:15:44]# systemctl is-enabled sshd
enabled
3.6重启验证:机器启动后是否可以通过终端工具
bash
[root@c001 ~ 19:16:14]# reboot
四、开发一个study服务
4.1开发 studyd 服务主程序 study
脚本说明:这是一个无限循环的脚本,每 5 秒会向 /var/log/study.log 文件中追加一行
包含当前时间的日志,内容为 [时间]: I'M studying [ Linux ]
bash
[root@centos01 ~ 13:49:41]# vim /usr/bin/study
bash
#!/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
4.2增加执行权限
bash
[root@centos01 ~ 13:51:25]# chmod +x /usr/bin/study
4.3创建 studyd 服务单元文件
bash
[root@centos01 ~ 13:54:00]# cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/studyd.service
[root@centos01 ~ 13:54:32]# vim /etc/systemd/system/studyd.service
[Unit]
Description=study server daemon
[Service]
ExecStart=/usr/bin/study
[Install]
WantedBy=multi-user.target
4.4通知 systemd 读取 unit 变化
bash
[root@centos01 ~ 13:56:30]# systemctl daemon-reload
4.5启用并启动服务
bash
[root@centos01 ~ 13:57:11]# systemctl start studyd
4.6查看服务状态
bash
[root@centos01 ~ 13:57:53]# systemctl status studyd
studyd.service - study server daemon
Loaded: loaded (/etc/systemd/system/studyd.service; disabled)
Active: active (running) since 四 2026-04-02 13:57:53 CST; 23s ago
Main PID: 5416 (study)
CGroup: /system.slice/studyd.service
├─5416 /bin/bash /usr/bin/study
└─5427 sleep 5
4月 02 13:57:53 centos01 systemd[1]: Started study server daemon.
4.7验证日志
bash
[root@centos01 ~ 13:58:16]# tail -f /var/log/study.log
2026年 04月 02日 星期四 13:57:53 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:57:58 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:58:03 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:58:08 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:58:13 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:58:18 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:58:23 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:58:28 CST: I'M studying [ Linux ]
2026年 04月 02日 星期四 13:58:33 CST: I'M studying [ Linux ]
五、复习vim
bash
[root@centos7 ~ 14:28:48]# cp /usr/lib/systemd/system/sshd.service .
[root@centos7 ~ 14:28:48]# vim sshd.service
- :set nu 显示行号
- :3,5d 【或者 光标移动到第三行d3d】 删除3-5行
- :5,6d 删除5-6行
- :6,9d 删除6-9行
- /Open 搜索并定位到特定字符串为止
- cw change word 替换一个单词(自动进入插入模式),输入study,按esc
- /sbin 搜索,C(删除到最后,并进入插入模式),补全路径local/bin/study,按esc
- ZZ 保存退出。
六、ssh命令传递密码工具 sshpass
bash
[root@c001 ~ 23:28:46]# yum install -y sshpass
已加载插件:fastestmirror
base | 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
正在解决依赖关系
--> 正在检查事务
---> 软件包 sshpass.x86_64.0.1.06-2.el7 将被 安装
--> 解决依赖关系完成
6.1 确保之前连接过,记得输入yes
bash
[jqz@c001 ~ 23:51:59]$ ssh jqz@192.168.100.101
The authenticity of host '192.168.100.101 (192.168.100.101)' can't be established.
ECDSA key fingerprint is 31:d8:e0:17:83:dd:25:05:7a:c6:dc:fd:8c:65:66:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.100.101' (ECDSA) to the list of known hosts.
jqz@192.168.100.101's password:
Last login: Thu Apr 2 23:51:48 2026 from c001
6.2 接下来传递密码123登录:
bash
[jqz@c001 ~ 23:53:10]$ sshpass -p 123 ssh jqz@192.168.100.101 id
uid=1000(jqz) gid=1000(jqz) 组=1000(jqz) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
6.3为自己创建一个普通的 zyl 用户,并设置密码
bash
[root@centos7 ~ 23:56:49]# useradd zyl
[root@centos7 ~ 00:05:28]# echo 123 | passwd --stdin zyl
更改用户 zyl 的密码 。
passwd:所有的身份验证令牌已经成功更新。
6.4配置 ssh 工具配置文件
bash
[zyl@centos7 ~ 00:06:51]$ cp /etc/ssh/ssh_config .ssh/config
cp: 无法创建普通文件".ssh/config": 没有那个文件或目录
[zyl@centos7 ~ 00:08:57]$ mkdir -p ~/.ssh
[zyl@centos7 ~ 00:09:49]$ cp /etc/ssh/ssh_config .ssh/config
[zyl@centos7 ~ 00:11:14]$ vi .ssh/config
[zyl@centos7 ~ 00:13:13]$ ls -d .ssh
.ssh
[zyl@centos7 ~ 00:13:20]$ vi .ssh/config
6.5切换用户
bash
[zyl@centos7 ~ 00:21:30]$ ssh 192.168.100.101
Warning: Permanently added '192.168.100.101' (ECDSA) to the list of known hosts.
root@192.168.100.101's password:
Last login: Thu Apr 2 23:56:49 2026
[root@centos7 ~ 00:21:35]#