关于 linux-administration
面向运维人员的 Linux 管理技能,适用于 Debian/Ubuntu 和 RHEL/CentOS 服务器及开发环境,涵盖日常系统维护与命令行故障排查。
主要功能
- 01软件包安装与更新
- 02文件系统和磁盘管理
- 03进程与资源检查
- 04Cron 和 systemd 定时任务
- 05日志分析与网络检查
适用场景
- 01维护 Linux 服务器
- 02排查磁盘占满或 CPU 过高
- 03安排定期维护任务
- 04调查服务启动失败
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ bagelhole/ devops-security-agent-skills --skill linux-administration安装后在 Claude Code 中输入/linux-administration确认已加载。
详细介绍
Linux 管理
用于管理 Debian/Ubuntu 和 RHEL/CentOS 发行版生产服务器、开发环境及基础设施主机的核心 Linux 系统管理技能。
使用场景
- 在任何环境中配置和维护 Linux 服务器
- 安装、更新或卸载软件包
- 管理文件系统、磁盘使用情况和挂载点
- 调查失控进程或资源占用过高的问题
- 使用 cron 或 systemd 定时器安排定期任务
- 分析系统和应用程序日志以排查问题
前提条件
- 拥有目标系统的 root 或 sudo 访问权限
- 已配置 SSH 访问(参见
ssh-configuration技能) - 熟悉终端文本编辑器(vim、nano)
- 可使用软件包管理器(Debian/Ubuntu 上的
apt,RHEL 8+/Fedora 上的dnf)
软件包管理
Debian / Ubuntu(apt)
# Update package index and upgrade all installed packages
apt update && apt upgrade -y
# Search for a package by keyword
apt search nginx
# Show detailed package info including dependencies
apt show nginx
# Install a specific version of a package
apt install nginx=1.24.0-1ubuntu1
# Install multiple packages in one command
apt install -y nginx certbot python3-certbot-nginx
# Remove a package but keep its config files
apt remove nginx
# Remove a package and purge all config files
apt purge nginx
# Remove unused dependency packages
apt autoremove -y
# List all installed packages
dpkg -l | grep nginx
# Pin a package to prevent automatic upgrades
cat <<'EOF' > /etc/apt/preferences.d/pin-nginx
Package: nginx
Pin: version 1.24.0-1ubuntu1
Pin-Priority: 1001
EOF
# Add an external repository (example: Docker CE)
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" \
> /etc/apt/sources.list.d/docker.list
apt update
RHEL / CentOS / Fedora(dnf)
# Update all packages
dnf update -y
# Search for a package
dnf search nginx
# Show package details
dnf info nginx
# Install a package
dnf install -y nginx
# Install a specific version
dnf install nginx-1.24.0-1.el9
# Remove a package
dnf remove nginx
# List installed packages
dnf list installed | grep nginx
# Enable a module stream (example: Node.js 20)
dnf module enable nodejs:20
dnf install -y nodejs
# Add an external repository
dnf install -y epel-release
# View repository list
dnf repolist --all
# Clean cached package data
dnf clean all
系统信息
# Kernel and OS release
uname -a
cat /etc/os-release
# Hostname and system metadata
hostnamectl
# CPU information
lscpu
nproc # Number of processing units
# Memory usage (human-readable)
free -h
# Disk usage summary
df -hT # Include filesystem type
du -sh /var/log/* # Summarize directory sizes
# Network interfaces and IP addresses
ip addr show
ip route show # Routing table
# Uptime and load average
uptime
w # Who is logged in and load
文件系统管理
# List block devices and partitions
lsblk
fdisk -l
# Create a new ext4 filesystem on a partition
mkfs.ext4 /dev/sdb1
# Mount a filesystem temporarily
mount /dev/sdb1 /mnt/data
# Add a persistent mount via fstab
echo '/dev/sdb1 /mnt/data ext4 defaults,noatime 0 2' >> /etc/fstab
mount -a # Mount everything in fstab
# Check and repair a filesystem (unmount first)
umount /dev/sdb1
fsck.ext4 -y /dev/sdb1
# Monitor disk I/O in real time
iostat -xz 2
# Find files larger than 100 MB
find / -xdev -type f -size +100M -exec ls -lh {} \;
# Check inode usage (out-of-inodes can mimic out-of-disk)
df -i
进程管理
# List all processes with full details
ps auxf
# Interactive process viewer (prefer htop if installed)
top
htop
# Find processes by name
pgrep -la nginx
# Show process tree
pstree -p
# Send graceful stop signal (SIGTERM)
kill <pid>
# Force kill an unresponsive process (SIGKILL)
kill -9 <pid>
# Kill all processes matching a name
pkill nginx
# Show open files for a process
lsof -p <pid>
# Show which process is listening on a port
ss -tlnp | grep :80
lsof -i :80
# Run a process immune to hangups (persists after logout)
nohup /opt/myapp/start.sh > /var/log/myapp.log 2>&1 &
# Limit CPU usage of a running process with cgroups v2
systemd-run --scope -p CPUQuota=25% --unit=limit-myapp /opt/myapp/start.sh
Cron 任务管理
# Edit the current user's crontab
crontab -e
# List current user's cron jobs
crontab -l
# Example crontab entries
# ┌───── minute (0-59)
# │ ┌───── hour (0-23)
# │ │ ┌───── day of month (1-31)
# │ │ │ ┌───── month (1-12)
# │ │ │ │ ┌───── day of week (0-7, 0 and 7 = Sunday)
# * * * * * command
# Run a backup every day at 2:30 AM
30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
# Run a cleanup every Sunday at midnight
0 0 * * 0 /usr/local/bin/cleanup.sh
# Run a health check every 5 minutes
*/5 * * * * /usr/local/bin/healthcheck.sh
# Place system-wide cron scripts in drop-in directories
ls /etc/cron.daily/
ls /etc/cron.weekly/
# Restrict cron access to specific users
echo "deploy" >> /etc/cron.allow
日志管理
# Follow systemd journal for a specific service
journalctl -u nginx -f
# Show logs since last boot
journalctl -b
# Show logs from a specific time range
journalctl --since "2025-01-15 08:00" --until "2025-01-15 12:00"
# Show only error-level and above
journalctl -p err
# Tail traditional syslog
tail -f /var/log/syslog # Debian/Ubuntu
tail -f /var/log/messages # RHEL/CentOS
# Kernel ring buffer messages
dmesg -T # Human-readable timestamps
dmesg --level=err,warn
# Check disk usage of log directory
du -sh /var/log/*
# Configure logrotate for a custom application
cat <<'EOF' > /etc/logrotate.d/myapp
/var/log/myapp/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 myapp myapp
sharedscripts
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}
EOF
# Force a logrotate run for testing
logrotate -f /etc/logrotate.d/myapp
# Centralized logging: forward journal to a remote syslog
# In /etc/systemd/journal-upload.conf:
# URL=http://logserver.example.com:19532
网络基础知识
# Test connectivity
ping -c 4 8.8.8.8
# DNS lookup
dig example.com
nslookup example.com
# Trace route to host
traceroute example.com
# List listening ports and associated processes
ss -tlnp
# Show active connections
ss -tunap
# Firewall management (UFW on Ubuntu)
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
ufw status verbose
# Firewall management (firewalld on RHEL/CentOS)
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
firewall-cmd --list-all
故障排查
| 症状 | 诊断命令 | 常见修复方法 |
|---|---|---|
| 磁盘已满 | df -h and du -sh /var/log/* | 清理旧日志,运行 logrotate -f,删除临时文件 |
| inode 耗尽 | df -i | 删除大量小文件,检查 /tmp 和邮件队列目录 |
| CPU 使用率过高 | top、ps aux --sort=-%cpu | 找出问题进程并重启或终止 |
| 内存占用过高 / 正在使用交换空间 | free -h、vmstat 1 | 调整 vm.swappiness,增加 RAM,找出内存泄漏 |
| 服务无法启动 | systemctl status <svc>、journalctl -u <svc> | 检查配置语法、文件权限和端口冲突 |
| DNS 解析失败 | dig @8.8.8.8 example.com、cat /etc/resolv.conf | 修正 nameserver 条目,重启 systemd-resolved |
| 软件包依赖错误 | apt --fix-broken install or dnf distro-sync | 解决被保留或冲突的软件包 |
| SSH 连接被拒绝 | ss -tlnp | grep 22、systemctl status sshd | 确保 sshd 正在运行,且防火墙允许端口 22 |
相关技能
ssh-configuration-- 安全远程访问 Linux 服务器user-management-- 创建和管理用户、组及 sudosystemd-services-- 编写和管理 systemd 单元文件performance-tuning-- 内核和应用程序性能优化backup-recovery-- 通过自动备份保护服务器数据
评论
支持 Markdown