作为IT工程师,以下是我为您整理的Linux系统优化方案,可显著提升系统性能:
# 编辑sysctl配置文件
sudo nano /etc/sysctl.conf
# 添加或修改以下参数
vm.swappiness = 10 # 减少交换分区使用
vm.vfs_cache_pressure = 50 # 优化文件系统缓存
net.ipv4.tcp_fin_timeout = 30 # 减少TCP连接关闭等待时间
net.ipv4.tcp_tw_reuse = 1 # 启用TIME-WAIT套接字重用
net.ipv4.tcp_syncookies = 1 # 防止SYN洪水攻击
kernel.sched_migration_cost_ns = 5000000 # 减少进程迁移开销
# 应用更改
sudo sysctl -p
EXT4优化:
# 调整文件系统挂载选项(在/etc/fstab中修改)
defaults,noatime,nodiratime,discard,commit=60,data=writeback
Btrfs/XFS优化:
# Btrfs
defaults,noatime,compress=zstd:3,ssd,discard=async
# XFS
defaults,noatime,nodiratime,logbufs=8,logbsize=256k
# 查看启动服务
systemctl list-unit-files --type=service | grep enabled
# 禁用不必要的服务(根据实际情况调整)
sudo systemctl disable bluetooth.service
sudo systemctl disable avahi-daemon.service
sudo systemctl disable cups.service
# 使用zRAM替代部分swap
sudo apt install zram-config # Ubuntu/Debian
sudo dnf install zram-generator # Fedora
# 调整透明大页(THP)设置
echo "madvise" | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
# 查看当前调度器
cat /sys/block/sda/queue/scheduler
# 设置调度器(SSD推荐使用none或kyber,HDD推荐使用mq-deadline)
echo "none" | sudo tee /sys/block/sda/queue/scheduler
# 永久设置(在/etc/default/grub中添加)
GRUB_CMDLINE_LINUX_DEFAULT="... elevator=none"
# 安装cpufrequtils
sudo apt install cpufrequtils
# 设置为性能模式
echo "performance" | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
# 或者使用ondemand模式平衡性能与功耗
sudo cpufreq-set -g ondemand
# 提高网络吞吐量
sudo ethtool -G eth0 rx 4096 tx 4096 # 调整环形缓冲区
sudo ethtool -K eth0 tso on gso on gro on # 启用硬件加速
# 优化TCP窗口大小
echo "net.core.rmem_max = 16777216" | sudo tee -a /etc/sysctl.conf
echo "net.core.wmem_max = 16777216" | sudo tee -a /etc/sysctl.conf
# 添加至crontab -e
0 3 * * * /usr/bin/updatedb # 更新locate数据库
0 4 * * * /usr/bin/fstrim -av # TRIM SSD(仅SSD需要)
30 4 * * 0 /usr/bin/apt update && /usr/bin/apt upgrade -y # 每周更新
# 禁用不必要的桌面特效
# GNOME: 安装gnome-tweaks调整
# KDE: 系统设置 > 工作空间行为 > 桌面特效
# 使用轻量级显示管理器
sudo apt install lightdm
sudo dpkg-reconfigure lightdm
# 安装常用监控工具
sudo apt install htop iotop iftop nmon sysstat
# 使用工具分析
vmstat 1 # 查看系统整体状态
iostat -x 1 # 查看磁盘I/O
dmesg | grep error # 检查系统错误
以上优化需根据具体硬件配置和工作负载进行调整。建议在实施前备份重要数据,并逐步测试每项更改的效果。对于生产环境,应在非高峰时段进行测试。