性能优化
Linux 服务器运维时使用:通过包管理、文件系统、进程…
需要在 Linux 生产或开发服务器上安装软件、管理服务与进程、处理磁盘/日志/网络问题并进行常规排障时使用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 49/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 85/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
Linux 服务器运维时使用:通过包管理通用
解决思路
- Debian/Ubuntu:更新软件包索引并升级已安装包:apt update && apt upgrade -y
- Debian/Ubuntu:按关键字搜索软件包:apt search nginx
- Debian/Ubuntu:查看软件包详细信息及依赖:apt show nginx
- Debian/Ubuntu:安装指定版本软件包:apt install nginx=1.24.0-1ubuntu1
- Debian/Ubuntu:一次安装多个软件包:apt install -y nginx certbot python3-certbot-nginx
- Debian/Ubuntu:移除软件包但保留配置文件:apt remove nginx
- Debian/Ubuntu:移除软件包并清除所有配置文件:apt purge nginx
- Debian/Ubuntu:移除不再使用的依赖包:apt autoremove -y
- Debian/Ubuntu:列出已安装的软件包:dpkg -l | grep nginx
- Debian/Ubuntu:固定软件包版本防止自动升级:写入 /etc/apt/preferences.d/pin-nginx,内容为 Package: nginx、Pin: version 1.24.0-1ubuntu1、Pin-Priority: 1001
- Debian/Ubuntu:添加外部软件仓库(以 Docker CE 为例):curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg,然后写入 /etc/apt/sources.list.d/docker.list:deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable,最后执行 apt update
- RHEL/CentOS/Fedora:更新所有软件包:dnf update -y
- RHEL/CentOS/Fedora:搜索软件包:dnf search nginx
- RHEL/CentOS/Fedora:查看软件包详细信息:dnf info nginx
- RHEL/CentOS/Fedora:安装软件包:dnf install -y nginx
- RHEL/CentOS/Fedora:安装指定版本:dnf install nginx-1.24.0-1.el9
- RHEL/CentOS/Fedora:移除软件包:dnf remove nginx
- RHEL/CentOS/Fedora:列出已安装软件包:dnf list installed | grep nginx
- RHEL/CentOS/Fedora:启用模块流后安装(以 Node.js 20 为例):dnf module enable nodejs:20 && dnf install -y nodejs
- RHEL/CentOS/Fedora:添加外部仓库:dnf install -y epel-release
- RHEL/CentOS/Fedora:查看仓库列表:dnf repolist --all
- RHEL/CentOS/Fedora:清理缓存的软件包数据:dnf clean all
- 查看内核和 OS 发行版:uname -a 和 cat /etc/os-release
- 查看主机名和系统元数据:hostnamectl
- 查看 CPU 信息:lscpu 和 nproc(处理单元数量)
- 查看内存使用:free -h
- 查看磁盘使用汇总:df -hT(含文件系统类型)和 du -sh /var/log/*(汇总目录大小)
- 查看网络接口和 IP 地址:ip addr show 和 ip route show(路由表)
- 查看运行时间和负载:uptime 和 w(登录用户及负载)
- 列出块设备和分区:lsblk 和 fdisk -l
- 在分区上创建 ext4 文件系统:mkfs.ext4 /dev/sdb1
- 临时挂载文件系统:mount /dev/sdb1 /mnt/data
- 通过 fstab 添加持久挂载:echo '/dev/sdb1 /mnt/data ext4 defaults,noatime 0 2' >> /etc/fstab 并执行 mount -a
- 检查并修复文件系统(先卸载):umount /dev/sdb1 && fsck.ext4 -y /dev/sdb1
- 实时监控磁盘 I/O:iostat -xz 2
- 查找大于 100 MB 的文件:find / -xdev -type f -size +100M -exec ls -lh {} \;
- 检查 inode 使用情况(inode 耗尽可能表现为磁盘满):df -i
- 列出所有进程及完整信息:ps auxf
- 交互式查看进程:top 或优先使用 htop(若已安装)
- 按名称查找进程:pgrep -la nginx
- 查看进程树:pstree -p
- 发送优雅停止信号(SIGTERM):kill <pid>
- 强制杀死无响应进程(SIGKILL):kill -9 <pid>
- 按名称杀死所有匹配进程:pkill nginx
- 查看进程打开的文件:lsof -p <pid>
- 查看哪个进程监听端口:ss -tlnp | grep :80 或 lsof -i :80
- 运行不受挂起影响的进程(退出登录后仍保持):nohup /opt/myapp/start.sh > /var/log/myapp.log 2>&1 &
- 用 cgroups v2 限制运行中进程的 CPU:systemd-run --scope -p CPUQuota=25% --unit=limit-myapp /opt/myapp/start.sh
- 编辑当前用户的 crontab:crontab -e
- 列出当前用户的 cron 任务:crontab -l
- 编写 cron 表达式:格式为 分 时 日 月 周 command;示例:每天 2:30 备份 30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1;每周日午夜清理 0 0 * * 0 /usr/local/bin/cleanup.sh;每 5 分钟健康检查 */5 * * * * /usr/local/bin/healthcheck.sh
- 使用系统级 cron 目录:ls /etc/cron.daily/ 和 ls /etc/cron.weekly/
- 限制可使用 cron 的用户:echo 'deploy' >> /etc/cron.allow
- 跟踪指定服务的 systemd journal:journalctl -u nginx -f
- 查看自上次启动以来的日志:journalctl -b
- 查看指定时间范围的日志:journalctl --since '2025-01-15 08:00' --until '2025-01-15 12:00'
- 仅查看 error 及以上级别日志:journalctl -p err
- 跟踪传统 syslog:tail -f /var/log/syslog(Debian/Ubuntu)或 tail -f /var/log/messages(RHEL/CentOS)
- 查看内核环形缓冲区消息:dmesg -T(人类可读时间戳)和 dmesg --level=err,warn
- 查看日志目录磁盘占用:du -sh /var/log/*
- 为自定义应用配置 logrotate:写入 /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
- 强制运行 logrotate 进行测试:logrotate -f /etc/logrotate.d/myapp
- 配置集中式日志:在 /etc/systemd/journal-upload.conf 中设置 URL=http://logserver.example.com:19532,将 journal 转发到远程 syslog
- 测试连通性:ping -c 4 8.8.8.8
- DNS 查询:dig example.com 或 nslookup example.com
- 路由追踪:traceroute example.com
- 列出监听端口及关联进程:ss -tlnp
- 查看活动连接:ss -tunap
- Ubuntu 防火墙 UFW 管理:ufw allow 22/tcp、ufw allow 80/tcp、ufw allow 443/tcp、ufw enable、ufw status verbose
- RHEL/CentOS 防火墙 firewalld 管理:firewall-cmd --permanent --add-service=http、firewall-cmd --permanent --add-service=https、firewall-cmd --reload、firewall-cmd --list-all
- 排查磁盘满:用 df -h 和 du -sh /var/log/* 定位;清理旧日志、运行 logrotate -f、删除临时文件
- 排查 inode 耗尽:用 df -i 检查;删除大量小文件,检查 /tmp 和邮件 spool
- 排查高 CPU:用 top、ps aux --sort=-%cpu 定位;重启或杀死问题进程
- 排查高内存/交换:用 free -h、vmstat 1 检查;调整 vm.swappiness、增加内存、定位内存泄漏
- 排查服务无法启动:用 systemctl status <svc>、journalctl -u <svc> 检查;检查配置语法、文件权限、端口冲突
- 排查 DNS 解析失败:用 dig @8.8.8.8 example.com、cat /etc/resolv.conf 检查;修复 nameserver、重启 systemd-resolved
- 排查软件包依赖错误:Debian/Ubuntu 用 apt --fix-broken install,RHEL 系用 dnf distro-sync;解决被 hold 或冲突的软件包
- 排查 SSH 连接被拒:用 ss -tlnp | grep 22、systemctl status sshd 检查;确保 sshd 运行且防火墙允许 22 端口
适用边界 · 注意事项
- 非 Linux 系统或没有 apt/dnf、systemd 的环境不适用;这些命令依赖 Linux 发行版和相应包管理器。
- 没有 root 或 sudo 权限、没有 SSH 访问权限时,包管理、挂载、防火墙、日志转发等管理操作无法执行。
- 生产环境中未评估影响时不要直接执行可能中断服务或锁死访问的命令,如 fsck、kill -9、启用防火墙、清理日志/磁盘。