Day 5:本周串联实操

Day 5:本周串联实操

今日目标:网络诊断 → 远程连接 → 文件传输 → 系统监控 → 定时上报——一整条运维链路 🎯

命令速览(本周全部命令)

ping curl wget ss netstat ip ifconfig ssh ssh-keygen ssh-copy-id scp df du free uptime uname who crontab

场景:搭建一个简易的服务器监控上报系统

你有一台远程服务器,需要写一个监控脚本,每 5 分钟采集系统状态并通过 scp 上报到管理机。

第一步:系统信息采集

创建监控脚本 ~/monitor.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
cat > ~/monitor.sh <<'SCRIPT'
#!/bin/bash
REPORT="/tmp/system-report-$(date +%Y%m%d-%H%M%S).txt"

echo "=== System Report: $(date) ===" > $REPORT
echo "" >> $REPORT

echo "--- Hostname ---" >> $REPORT
hostname >> $REPORT
echo "" >> $REPORT

echo "--- Uptime & Load ---" >> $REPORT
uptime >> $REPORT
echo "" >> $REPORT

echo "--- Memory ---" >> $REPORT
free -h >> $REPORT
echo "" >> $REPORT

echo "--- Disk Usage ---" >> $REPORT
df -h / >> $REPORT
echo "" >> $REPORT

echo "--- Top 5 Large Directories in /var ---" >> $REPORT
du -sk /var/log/* 2>/dev/null | sort -rn | head -5 >> $REPORT
echo "" >> $REPORT

echo "--- Listening Ports ---" >> $REPORT
ss -tulnp 2>/dev/null || netstat -tulnp 2>/dev/null >> $REPORT
echo "" >> $REPORT

echo "--- Logged-in Users ---" >> $REPORT
who >> $REPORT

echo "Report saved to: $REPORT"
SCRIPT

chmod +x ~/monitor.sh

第二步:本地测试

  1. ~/monitor.sh——执行脚本
  2. cat /tmp/system-report-*.txt——查看生成的报告
  3. 验证每个命令的输出是否正确

第三步:设置定时上报

1
2
# 编辑 crontab
crontab -e

在打开的编辑器中,添加以下行(注意:将 $USER 替换为你的实际用户名;此行在 crontab 中不要# 号,# 在 crontab 中代表注释,加了就不会执行):

1
*/5 * * * * /home/$USER/monitor.sh >> /tmp/monitor-cron.log 2>&1
  1. 等待 5-10 分钟,查看 /tmp/ 下生成了几个报告文件
  2. tail -f /tmp/monitor-cron.log——查看执行日志

第四步:远程传输(如果有远程主机)

1
2
# 将报告 scp 到远程主机
scp /tmp/system-report-*.txt user@remote-host:/tmp/reports/

如果没有远程主机,模拟:

  1. mkdir -p /tmp/reports-remote
  2. cp /tmp/system-report-*.txt /tmp/reports-remote/

第五步:网络连接排查

  1. ping -c 3 localhost——本地连通性
  2. ss -tulnp | grep 22——SSH 端口在监听吗?
  3. curl -I http://localhost——如果本机有 Web 服务
  4. ip addr show | grep "inet "——确认自己的 IP

第六步:清理与查看

  1. ls -lh /tmp/system-report-*——查看生成了多少报告
  2. du -sh /tmp/system-report-*——报告占了多少空间
  3. 清理:rm /tmp/system-report-*.txt

今日必刷(全部终端实操)💪

  1. 执行 ~/monitor.sh 生成一份系统报告,查看报告内容
  2. ss -tulnp 列出所有监听端口,找到 SSH 端口
  3. du -sh /var/log/* 2>/dev/null | sort -rh | head -3 找出 /var/log 下最大的 3 个项
  4. 设置一个 crontab:每 10 分钟执行 monitor.sh(注意 cron 中应使用 monitor.sh 的绝对路径,如 /home/你的用户名/monitor.sh
  5. 排错题:cron 定时执行的 monitor.sh 生成的报告 /tmp 下找不到——可能的原因?(至少 3 个)
  6. 排错题df -h 显示 / 分区 98% 已满,你需要快速定位是哪个目录在吃磁盘——写出从 / 开始逐层排查的命令序列

📚 命令详解

命令 详细参考
ping 网络管理-ping
curl 网络管理-curl
wget 网络管理-wget
ss 网络管理-ss
netstat 网络管理-netstat
ip 网络管理-ip
ifconfig 网络管理-ifconfig
ssh 网络管理-ssh
ssh-keygen 网络管理-ssh
ssh-copy-id 网络管理-ssh
scp 网络管理-scp
df 文件与目录管理-df
du 文件与目录管理-du
free 系统管理-free
uptime 进程管理-uptime
uname 系统管理-uname
who 权限与用户管理-who
crontab 系统管理-crontab