关于 administering-linux
面向 DevOps、SRE、后端和平台工程师的 Linux 系统管理技能,聚焦 systemd 发行版。涵盖服务、进程、文件系统、日志、网络与性能排障。
主要功能
- 01管理 systemd 服务与定时任务
- 02监控进程及系统资源
- 03检查磁盘并分析日志
- 04配置用户、SSH 与防火墙
- 05诊断网络和性能问题
适用场景
- 01排查生产服务故障与系统变慢
- 02部署并管理自定义应用服务
- 03检查磁盘空间及服务日志
- 04配置 SSH 访问和定时任务
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ ancoleman/ ai-design-components --skill administering-linux安装后在 Claude Code 中输入/administering-linux确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
outputs.yamlexamples/configs/sshd_config.hardenedexamples/scripts/backup.shexamples/systemd-units/backup.servicebackup.timerwebapp.servicereferences/filesystem-management.mdnetwork-configuration.mdperformance-tuning.mdsecurity-hardening.mdsystemd-guide.mdtroubleshooting-guide.md详细介绍
Linux 管理
面向现代云原生环境的全面 Linux 系统管理,涵盖服务器管理、应用部署和生产问题排查。
目的
本技能面向 DevOps 工程师、SRE、后端开发人员和平台工程师,讲解 Linux 管理的基础知识和中级内容。重点介绍基于 systemd 的发行版(Ubuntu、RHEL、Debian、Fedora),涵盖服务管理、进程监控、文件系统操作、用户管理、性能调优、日志分析和网络配置。
即使采用了容器化,现代基础设施仍离不开扎实的 Linux 基础知识。容器主机运行 Linux,Kubernetes 节点需要优化,而排查生产问题则需要理解 systemd、进程和日志。
不涵盖:
- 高级网络(BGP、OSPF)——请参阅
network-architecture技能 - 深度安全加固(合规、渗透测试)——请参阅
security-hardening技能 - 大规模配置管理(Ansible、Puppet)——请参阅
configuration-management技能 - 容器编排——请参阅
kubernetes-operations技能
何时使用此技能
在部署自定义应用、排查系统运行缓慢、调查服务故障、优化工作负载、管理用户、配置 SSH、监控磁盘空间、安排任务、诊断网络问题或进行性能调优时使用。
快速入门
基本命令
服务管理:
systemctl start nginx # Start service
systemctl stop nginx # Stop service
systemctl restart nginx # Restart service
systemctl status nginx # Check status
systemctl enable nginx # Enable at boot
journalctl -u nginx -f # Follow service logs
进程监控:
top # Interactive process monitor
htop # Enhanced process monitor
ps aux | grep process_name # Find specific process
kill -15 PID # Graceful shutdown (SIGTERM)
kill -9 PID # Force kill (SIGKILL)
磁盘使用情况:
df -h # Filesystem usage
du -sh /path/to/dir # Directory size
ncdu /path # Interactive disk analyzer
日志分析:
journalctl -f # Follow all logs
journalctl -u service -f # Follow service logs
journalctl --since "1 hour ago" # Filter by time
journalctl -p err # Show errors only
用户管理:
useradd -m -s /bin/bash username # Create user with home dir
passwd username # Set password
usermod -aG sudo username # Add to sudo group
userdel -r username # Delete user and home dir
核心概念
Systemd 架构
Systemd 是标准的 init 系统和服务管理器。Systemd 单元用于定义服务、定时器、目标和其他系统资源。
单元文件位置(按优先级排序):
/etc/systemd/system/- 自定义单元(优先级最高)/run/systemd/system/- 运行时单元(临时)/lib/systemd/system/- 系统提供的单元(不要修改)
主要单元类型: .service(服务)、.timer(计划任务)、.target(单元组)、.socket(由套接字激活)
基本 systemctl 命令:
systemctl daemon-reload # Reload unit files after changes
systemctl list-units --type=service
systemctl list-timers # Show all timers
systemctl cat nginx.service # Show unit file content
systemctl edit nginx.service # Create override file
如需详细了解 systemd,请参阅 references/systemd-guide.md。
进程管理
进程是正在运行且具有唯一 PID 的程序。了解进程状态、信号和资源使用情况对于问题排查至关重要。
进程状态: R(运行中)、S(休眠)、D(不可中断休眠/I/O)、Z(僵尸)、T(已停止)
常见信号: SIGTERM(15),正常终止;SIGKILL(9),强制终止;SIGHUP(1),重新加载配置
进程优先级:
nice -n 10 command # Start with lower priority
renice -n 5 -p PID # Change priority of running process
文件系统层级结构
重要目录:/(根目录)、/etc/(配置)、/var/(可变数据)、/opt/(可选软件)、/usr/(用户程序)、/home/(用户目录)、/tmp/(临时文件)、/boot/(引导加载程序)
文件系统类型速查:
- ext4 - 通用(默认)
- XFS - 大文件、数据库(RHEL 默认)
- Btrfs - 快照、写时复制
- ZFS - 企业级、数据完整性、NAS
有关文件系统管理的详细信息(包括 LVM 和 RAID),请参阅 references/filesystem-management.md。
包管理
Ubuntu/Debian(apt):
apt update && apt upgrade # Update system
apt install package # Install package
apt remove package # Remove package
apt search keyword # Search packages
RHEL/CentOS/Fedora(dnf):
dnf update # Update all packages
dnf install package # Install package
dnf remove package # Remove package
dnf search keyword # Search packages
系统服务请使用原生包管理器;桌面应用和跨发行版兼容场景可使用 snap/flatpak。
决策框架
性能问题排查
调查工作流:
-
确定瓶颈:
top # Quick overview uptime # Load averages -
CPU 问题(使用率 >80%):
top # Press Shift+P to sort by CPU ps aux --sort=-%cpu | head -
内存问题(已使用交换空间):
free -h # Memory usage top # Press Shift+M to sort by memory -
磁盘 I/O 问题(wa% 较高):
iostat -x 1 # Disk statistics iotop # I/O by process -
网络问题:
ss -tunap # Active connections iftop # Bandwidth monitor
如需全面的问题排查指南,请参阅 references/troubleshooting-guide.md。
文件系统选择
快速决策:
- 默认/通用 → ext4
- 数据库服务器 → XFS
- 大文件存储 → XFS 或 ZFS
- NAS/文件服务器 → ZFS
- 需要快照 → Btrfs 或 ZFS
常见工作流
创建 Systemd 服务
步骤 1:创建单元文件
sudo nano /etc/systemd/system/myapp.service
步骤 2:单元文件内容
[Unit]
Description=My Web Application
After=network.target postgresql.service
Requires=postgresql.service
[Service]
Type=simple
User=myapp
Group=myapp
WorkingDirectory=/opt/myapp
Environment="PORT=8080"
ExecStart=/opt/myapp/bin/server
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
StandardOutput=journal
# Security hardening
PrivateTmp=true
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/var/lib/myapp
[Install]
WantedBy=multi-user.target
步骤 3:部署并启动
sudo useradd -r -s /bin/false myapp
sudo mkdir -p /var/lib/myapp
sudo chown myapp:myapp /var/lib/myapp
sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp.service
sudo systemctl status myapp.service
完整示例请参阅 examples/systemd-units/。
Systemd 定时器(替代 Cron)
为定时任务创建服务单元和定时器单元。定时器单元通过 OnCalendar= 指定计划,并通过 Persistent=true 处理错过的任务。服务单元使用 Type=oneshot。完整示例请参阅 examples/systemd-units/backup.timer 和 backup.service。
SSH 加固
生成 SSH 密钥:
ssh-keygen -t ed25519 -C "[email protected]"
ssh-copy-id admin@server
加固 sshd_config:
sudo nano /etc/ssh/sshd_config
关键设置:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
AllowUsers admin deploy
X11Forwarding no
Port 2222 # Optional
应用更改:
sudo sshd -t # Test
sudo systemctl restart sshd # Apply (keep backup session!)
完整的 SSH 配置请参阅 examples/configs/sshd_config.hardened 和 references/security-hardening.md。
性能调优
在 /etc/sysctl.d/99-custom.conf 中配置 sysctl 参数,以调整网络(TCP 缓冲区、BBR 拥塞控制)、内存管理(swappiness、缓存压力)和文件描述符。在 /etc/security/limits.conf 中设置 nofile 和 nproc 的 ulimit。配置 I/O 调度器和 CPU 调节器。全面的调优内容请参阅 references/performance-tuning.md,模板请参阅 examples/configs/。
日志调查
使用 systemctl status myapp 和 journalctl -u myapp 调查问题。可以按时间 --since、严重级别 -p err 筛选日志,或使用 grep 搜索模式。使用 top、df -h、free -h 结合系统指标进行分析。使用 journalctl -k | grep -i oom 检查 OOM 终止。详细工作流请参阅 references/troubleshooting-guide.md。
必备命令
接口管理:
ip addr show # Show all interfaces
ip link set eth0 up # Bring interface up
ip addr add 192.168.1.100/24 dev eth0
路由:
ip route show # Show routing table
ip route get 8.8.8.8 # Show route to IP
ip route add 10.0.0.0/24 via 192.168.1.1
套接字统计:
ss -tunap # All TCP/UDP connections
ss -tlnp # Listening TCP ports
ss -ulnp # Listening UDP ports
ss -tnp state established # Established connections
防火墙配置
Ubuntu(ufw):
sudo ufw status
sudo ufw enable
sudo ufw allow 22/tcp # Allow SSH
sudo ufw allow 80/tcp # Allow HTTP
sudo ufw allow from 192.168.1.0/24 # Allow from subnet
sudo ufw default deny incoming
RHEL/CentOS(firewalld):
firewall-cmd --state
firewall-cmd --list-all
firewall-cmd --add-service=http --permanent
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload
网络配置的完整内容(包括 netplan、NetworkManager 和 DNS)请参阅 references/network-configuration.md。
定时任务
Cron 语法
crontab -e # Edit user crontab
# Format: minute hour day month weekday command
0 2 * * * /usr/local/bin/backup.sh # Daily at 2:00 AM
*/5 * * * * /usr/local/bin/check-health.sh # Every 5 minutes
0 3 * * 0 /usr/local/bin/weekly-cleanup.sh # Weekly Sunday 3 AM
@reboot /usr/local/bin/startup-script.sh # Run at boot
Systemd 定时器日历语法
OnCalendar=daily # Every day at midnight
OnCalendar=*-*-* 02:00:00 # Daily at 2:00 AM
OnCalendar=Mon *-*-* 09:00:00 # Every Monday at 9 AM
OnCalendar=*-*-01 00:00:00 # 1st of every month
OnBootSec=5min # 5 minutes after boot
必备工具
进程监控
top、htop- 实时进程监控器ps- 报告进程状态pgrep/pkill- 按名称查找/终止进程
日志分析
journalctl- 查询 systemd 日志grep- 搜索文本模式tail -f- 持续跟踪日志文件
磁盘管理
df- 磁盘空间使用情况du- 目录空间使用情况lsblk- 列出块设备ncdu- 交互式磁盘分析器
网络工具
ip- 网络配置ss- 套接字统计ping- 测试连接dig/nslookup- DNS 查询tcpdump- 捕获数据包
系统监控
- Netdata - 实时 Web 仪表板
- Prometheus + Grafana - 指标收集
- ELK Stack - 集中式日志记录
与其他技能的集成
Kubernetes 运维
Linux 管理是 Kubernetes 节点管理的基础。节点优化(sysctl 调优)、将 kubelet 作为 systemd 服务运行、通过 journald 查看容器日志、使用 cgroups 设置资源限制。
示例:
# /etc/sysctl.d/99-kubernetes.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
Kubernetes 专属运维内容请参阅 kubernetes-operations 技能。
配置管理
Linux 管理提供相关知识;配置管理则负责将其自动化。Ansible playbook 可自动创建 systemd 服务并调整系统。
大规模自动化请参阅 configuration-management 技能。
安全加固
本技能涵盖 SSH 和防火墙基础知识。高级安全内容(MFA、证书、CIS 基准、合规性)请参阅 security-hardening 技能。
CI/CD 流水线
CI/CD 流水线使用这些技能将内容部署到 Linux 服务器。使用 systemctl 进行部署,并通过 journalctl 进行监控。
部署自动化请参阅 building-ci-pipelines 技能。
参考资料
详细指南
references/systemd-guide.md- 全面的 systemd 参考指南(单元文件、依赖项、目标)references/performance-tuning.md- 完整的 sysctl、ulimits、cgroups、I/O 调度器指南references/filesystem-management.md- LVM、RAID、文件系统类型、权限references/network-configuration.md- ip/ss 命令、netplan、NetworkManager、DNS、防火墙references/security-hardening.md- SSH 加固、防火墙、SELinux/AppArmor 基础知识references/troubleshooting-guide.md- 常见问题、诊断工作流、解决方案
示例
examples/systemd-units/- 服务、定时器和目标单元文件examples/scripts/- 备份、运行状况检查和维护脚本examples/configs/- sshd_config、sysctl.conf、logrotate 示例
特定发行版说明
Ubuntu/Debian
包管理器:apt,网络:netplan,防火墙:ufw,软件源:/etc/apt/sources.list
RHEL/CentOS/Fedora
包管理器:dnf,网络:NetworkManager,防火墙:firewalld,软件源:/etc/yum.repos.d/,默认启用 SELinux
Arch Linux
包管理器:pacman,网络:NetworkManager,滚动发行版,社区软件包使用 AUR
其他资源
官方文档:
- systemd:https://systemd.io/
- Linux 内核:https://kernel.org/doc/
相关技能:
kubernetes-operations- 在 Linux 上进行容器编排configuration-management- 大规模自动化 Linux 管理security-hardening- 高级安全与合规building-ci-pipelines- 通过 CI/CD 部署performance-engineering- 深入分析性能