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 | cat > ~/monitor.sh <<'SCRIPT' |
第二步:本地测试
~/monitor.sh——执行脚本cat /tmp/system-report-*.txt——查看生成的报告- 验证每个命令的输出是否正确
第三步:设置定时上报
1 | # 编辑 crontab |
在打开的编辑器中,添加以下行(注意:将 $USER 替换为你的实际用户名;此行在 crontab 中不要加 # 号,# 在 crontab 中代表注释,加了就不会执行):
1 | */5 * * * * /home/$USER/monitor.sh >> /tmp/monitor-cron.log 2>&1 |
- 等待 5-10 分钟,查看
/tmp/下生成了几个报告文件 tail -f /tmp/monitor-cron.log——查看执行日志
第四步:远程传输(如果有远程主机)
1 | # 将报告 scp 到远程主机 |
如果没有远程主机,模拟:
mkdir -p /tmp/reports-remotecp /tmp/system-report-*.txt /tmp/reports-remote/
第五步:网络连接排查
ping -c 3 localhost——本地连通性ss -tulnp | grep 22——SSH 端口在监听吗?curl -I http://localhost——如果本机有 Web 服务ip addr show | grep "inet "——确认自己的 IP
第六步:清理与查看
ls -lh /tmp/system-report-*——查看生成了多少报告du -sh /tmp/system-report-*——报告占了多少空间- 清理:
rm /tmp/system-report-*.txt
今日必刷(全部终端实操)💪
- 执行
~/monitor.sh生成一份系统报告,查看报告内容 - 用
ss -tulnp列出所有监听端口,找到 SSH 端口 - 用
du -sh /var/log/* 2>/dev/null | sort -rh | head -3找出 /var/log 下最大的 3 个项 - 设置一个 crontab:每 10 分钟执行
monitor.sh(注意 cron 中应使用 monitor.sh 的绝对路径,如/home/你的用户名/monitor.sh) - 排错题:cron 定时执行的 monitor.sh 生成的报告
/tmp下找不到——可能的原因?(至少 3 个) - 排错题:
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 |