内容大纲
- Part01-系统优化概述
- Part02-CPU优化总结
- Part03-内存优化总结
- Part04-磁盘I/O优化总结
- Part05-网络优化总结<
学习交流加群风哥微信: itpux-com
/h2>
内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
1. 系统优化概述
系统优化是通过调整系统配置、优化资源使用、提高系统性能的过程。
风哥提示:
# CPU优化:调整CPU调度、频率、亲和性
# 内存优化:调整内存参数、交换空间、缓存
# 磁盘I/O优化:调整I/O调度器、磁盘参数、文件系统
# 网络优化:调整网络参数、TCP参数、网络接口
# 系统服务优化:优化系统服务配置
# 内核优化:调整内核参数
2. CPU优化总结
CP
更多学习教程公众号风哥教程itpux_com
U优化的关键点。
# 1. CPU调度器优化
[root@localhost ~]# echo performance > /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
# 2. CPU亲和性优化
[root@localhost ~]# taskset -pc 0,1,2,3 1234
# 3. 进程优先级优化
[root@localhost ~]# renice -n -5 -p 1234
# 4. CPU参数优化
[root@localhost ~]# cat > /etc/sysctl.d/99-cpu-tuning.conf << 'EOF'
# CPU调优参数
kernel.sched_min_granularity_ns = 1000000
kernel.sched_wakeup_granularity_ns = 2000000
kernel.sched_tunable_scaling = 1
kernel.sched_latency_ns = 20000000
kernel.sched_time_avg_ms = 1000
kernel.sched_nr_migrate = 32
kernel.sched_migration_cost_ns = 500000
EOF
# 5. 应用CPU参数
[root@localhost ~]# sysctl -p /etc/sysctl.d/99-cpu-tuning.conf
# 6. 查看CPU状态
[root@localhost ~]# cpupower frequency-info
analyzing CPU 0:
driver: intel_pstate
CPUs which run at the same hardware frequency: 0
CPUs which need to have their frequency coordinated by software: 0
maximum transition latency: Cannot determine or is not supported.
hardware limits: 1.20 GHz - 3.60 GHz
available cpufreq governors: performance powersave
current policy: frequency should be within 1.20 GHz and 3.60 GHz.
The governor "performance" may decide which speed to use
within this range.
current CPU frequency: Unable to call hardware
current CPU frequency: 3.60 GHz (asserted by call to kernel)
boost state support:
Supported: yes
Active: yes
3600 MHz max turbo 4 active cores
3600 MHz max turbo 3 active cores
3600 MHz max turbo 2 active cores
3600 MHz max turbo 1 active cores
3. 内存优化总结
内存优化的关键点。
更多视频教程www.fgedu.net.cn
# 1. 内存参数优化
[root@localhost ~]# cat > /etc/sysctl.d/99-memory-tuning.conf << 'EOF'
# 内存调优参数
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.vfs_cache_pressure = 75
vm.min_free_kbytes = 131072
vm.overcommit_memory = 1
vm.overcommit_ratio = 100
vm.dirty_expire_centisecs = 3000
vm.dirty_writeback_centisecs = 500
EOF
# 2. 应用内存参数
[root@localhost ~]# sysctl -p /etc/sysctl.d/99-memory-tuning.conf
# 3. 创建swap文件
[root@localhost ~]# dd if=/dev/zero of=/swapfile bs=1G count=4
4+0 records in
4+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 10.1234 s, 424 MB/s
# 4. 设置swap文件权限
[root@localhost ~]# chmod 600 /swapfile
# 5. 格式化swap文件
[root@localhost ~]# mkswap /swapfile
Setting up swapspace version 1, size = 4 GiB (4294967296 bytes)
no label, UUID=12345678-1234-1234-1234-123456789012
# 6. 启用swap文件
[root@localhost ~]# swapon /swapfile
# 7. 永久启用swap
[root@localhost ~]# echo '/swapfile none swap sw 0 0' >> /etc/fstab
# 8. 查看内存状态
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 7.6Gi 2.0Gi 4.0Gi 100Mi 1.6Gi 5.3Gi
Swap: 8.0Gi 0B 8.0Gi
from PG视频:www.itpux.com
iv>
4. 磁盘I/O优化总结
磁盘I/O优化的关键点。
# 1. I/O调度器优化
[root@localhost ~]# echo bfq > /sys/block/sda/queue/scheduler
# 2. 永久设置I/O调度器
[root@localhost ~]# cat > /etc/udev/rules.d/60-io-scheduler.rules << 'EOF'
# 设置I/O调度器
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/scheduler}="bfq"
EOF
# 3. 磁盘预读优化
[root@localhost ~]# blockdev --setra 1024 /dev/sda
# 4. 永久设置磁盘预读
[root@localhost ~]# cat > /etc/rc.d/rc.local << 'EOF'
#!/bin/bash
# 设置磁盘预读
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
blockdev --setra 1024 /dev/sda
blockdev --setra 1024 /dev/sdb
blockdev --setra 1024 /dev/sdc
EOF
# 5. 设置脚本执行权限
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
# 6. 文件系统挂载优化
[root@localhost ~]# cat /etc/fstab | grep /dev/sda1
/dev/sda1 / xfs defaults,noatime 0 0
# 7. 查看磁盘I/O状态
[root@localhost ~]# iostat -x 1 3
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
2.50 0.00 1.00 0.50 0.00 96.00
Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
sda 0.50 0.50 5.00 2.50 0.00 0.00 0.00 0.00 0.00 0.00 0.00 10.00 5.00 0.00 0.00
5. 网络优化总结<
学习交流加群风哥微信: itpux-com
/h2>
网络优化的关键点。
# 1. 网络参数优化
[root@localhost ~]# cat > /etc/sysctl.d/99-network-tuning.conf << 'EOF'
# 网络调优参数
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 5000
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
EOF
# 2. TCP参数优化
[root@localhost ~]# cat > /etc/sysctl.d/99-tcp-tuning.conf << 'EOF'
# TCP调优参数
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_mtu_probing = 1
net.ipv4.tcp_congestion_control = bbr
EOF
# 3. 应用网络参数
[root@localhost ~]# sysctl -p /etc/sysctl.d/99-network-tuning.conf
[root@localhost ~]# sysctl -p /etc/sysctl.d/99-tcp-tuning.conf
# 4. 网络接口优化
[root@localhost ~]# ethtool -s eth0 speed 1000 duplex full autoneg on
[root@localhost ~]# ip link set eth0 mtu 9000
[root@localhost ~]# ethtool -L eth0 combined 16
# 5. 永久设置网络接口参数
[root@localhost ~]# cat > /etc/NetworkManager/dispatcher.d/99-eth0-tuning << 'EOF'
#!/bin/bash
# 网络接口调优脚本
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
if [ "$1" = "eth0" ] && [ "$2" = "up" ]; then
ethtool -s eth0 speed 1000 duplex full autoneg on
ip link set eth0 mtu 9000
ethtool -L eth0 combined 16
fi
EOF
# 6. 设置脚本执行权限
[root@localhost ~]# chmod +x /etc/NetworkManager/dispatcher.d/99-eth0-tuning
# 7. 查看网络状态
[root@localhost ~]# ip addr show eth0
2: eth0:
link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::211:22ff:fe33:4455/64 scope link
valid_lft forever preferred_lft forever
学习交流加群风哥QQ113257174
6. 综合优化案例
系统综合优化实战案例。
# 1. 创建综合优化脚本
[root@localhost ~]# cat > /usr/local/bin/system-optimization.sh << 'EOF'
#!/bin/bash
# script.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# 系统综合优化脚本
LOG_FILE="/var/log/system-optimization.log"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# CPU优化
optimize_cpu() {
log “Optimizing CPU…”
# 设置CPU调度器
echo performance > /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
# 配置CPU参数
cat > /etc/sysctl.d/99-cpu-tuning.conf << 'EOF'
kernel.sched_min_granularity_ns = 1000000
kernel.sched_wakeup_granularity_ns = 2000000
kernel.sched_tunable_scaling = 1
kernel.sched_latency_ns = 20000000
kernel.sched_time_avg_ms = 1000
kernel.sched_nr_migrate = 32
kernel.sched_migration_cost_ns = 500000
EOF
sysctl -p /etc/sysctl.d/99-cpu-tuning.conf
log "CPU optimized."
}
# 内存优化
optimize_memory() {
log "Optimizing memory..."
# 配置内存参数
cat > /etc/sysctl.d/99-memory-tuning.conf << 'EOF'
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.vfs_cache_pressure = 75
vm.min_free_kbytes = 131072
vm.overcommit_memory = 1
vm.overcommit_ratio = 100
vm.dirty_expire_centisecs = 3000
vm.dirty_writeback_centisecs = 500
EOF
sysctl -p /etc/sysctl.d/99-memory-tuning.conf
log "Memory optimized."
}
# 磁盘I/O优化
optimize_disk() {
log "Optimizing disk I/O..."
# 设置I/O调度器
echo bfq > /sys/block/sda/queue/scheduler
# 配置I/O调度器
cat > /etc/udev/rules.d/60-io-scheduler.rules << 'EOF'
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/scheduler}="bfq"
EOF
# 设置磁盘预读
blockdev --setra 1024 /dev/sda
log "Disk I/O optimized."
}
# 网络优化
optimize_network() {
log "Optimizing network..."
# 配置网络参数
cat > /etc/sysctl.d/99-network-tuning.conf << 'EOF'
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 5000
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
EOF
# 配置TCP参数
cat > /etc/sysctl.d/99-tcp-tuning.conf << 'EOF'
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_mtu_probing = 1
net.ipv4.tcp_congestion_control = bbr
EOF
sysctl -p /etc/sysctl.d/99-network-tuning.conf
sysctl -p /etc/sysctl.d/99-tcp-tuning.conf
# 设置网络接口参数
ethtool -s eth0 speed 1000 duplex full autoneg on
ip link set eth0 mtu 9000
ethtool -L eth0 combined 16
log "Network optimized."
}
# 主函数
main() {
log "Starting system optimization..."
# 优化各项
optimize_cpu
optimize_memory
optimize_disk
optimize_network
log "System optimization completed."
}
# 执行主函数
main
EOF
# 2. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/system-optimization.sh
# 3. 创建日志文件
[root@localhost ~]# touch /var/log/system-optimization.log
[root@localhost ~]# chmod 644 /var/log/system-optimization.log
# 4. 测试优化脚本
[root@localhost ~]# /usr/local/bin/system-optimization.sh
# 5. 查看日志
[root@localhost ~]# tail -f /var/log/system-optimization.log
[2026-04-03 10:00:00] Starting system optimization...
[2026-04-03 10:00:01] Optimizing CPU...
[2026-04-03 10:00:02] CPU optimized.
[2026-04-03 10:00:03] Optimizing memory...
[2026-04-03 10:00:04] Memory optimized.
[2026-04-03 10:00:05] Optimizing disk I/O...
[2026-04-03 10:00:06] Disk I/O optimized.
[2026-04-03 10:00:07] Optimizing network...
[2026-04-03 10:00:08] Network optimized.
[2026-04-03 10:00:09] System optimization completed.
# 6. 创建性能对比脚本
[root@localhost ~]# cat > /usr/local/bin/performance-compare.sh << 'EOF'
#!/bin/bash
# 性能对比脚本
LOG_FILE="/var/log/performance-compare.log"
REPORT_FILE="/var/log/performance-compare-report.txt"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 测试CPU性能
test_cpu() {
log “Testing CPU performance…”
echo “=== CPU Performance Test ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# CPU使用率
CPU_USAGE=$(top -bn1 | grep “Cpu(s)” | awk ‘{print $2}’ | sed ‘s/us,//’)
echo “CPU Usage: ${CPU_USAGE}%” >> $REPORT_FILE
# 负载平均值
LOAD_AVG=$(uptime | awk -F’load average:’ ‘{print $2}’)
echo “Load Average:${LOAD_AVG}” >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 测试内存性能
test_memory() {
log “Testing memory performance…”
echo “=== Memory Performance Test ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 内存使用率
MEMORY_USAGE=$(free | grep Mem | awk ‘{printf(“%.0f”), $3/$2 * 100.0}’)
echo “Memory Usage: ${MEMORY_USAGE}%” >> $REPORT_FILE
# 交换使用率
SWAP_USAGE=$(free | grep Swap | awk ‘{printf(“%.0f”), $3/$2 * 100.0}’)
echo “Swap Usage: ${SWAP_USAGE}%” >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 测试磁盘I/O性能
test_disk() {
log “Testing disk I/O performance…”
echo “=== Disk I/O Performance Test ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 磁盘I/O测试
dd if=/dev/zero of=/tmp/testfile bs=1G count=1 oflag=direct 2>&1 | grep copied
echo “Disk write test completed.” >> $REPORT_FILE
dd if=/tmp/testfile of=/dev/null bs=1G count=1 iflag=direct 2>&1 | grep copied
echo “Disk read test completed.” >> $REPORT_FILE
rm -f /tmp/testfile
echo “” >> $REPORT_FILE
}
# 测试网络性能
test_network() {
log “Testing network performance…”
echo “=== Network Performance Test ===” >> $REPORT_FILE
echo “” >> $REPORT_FILE
# 网络延迟测试
ping -c 4 192.168.1.1 >> $REPORT_FILE
# 网络带宽测试
iperf3 -c 192.168.1.1 -t 10 >> $REPORT_FILE
echo “” >> $REPORT_FILE
}
# 主函数
main() {
log “Starting performance comparison…”
# 清空报告文件
> $REPORT_FILE
# 测试各项性能
test_cpu
test_memory
test_disk
test_network
log “Performance comparison completed. Report saved to $REPORT_FILE”
}
# 执行主函数
main
EOF
# 7. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/performance-compare.sh
# 8. 测试性能对比脚本
[root@localhost ~]# /usr/local/bin/performance-compare.sh
# 9. 查看报告
[root@localhost ~]# cat /var/log/performance-compare-report.txt
=== CPU Performance Test ===
CPU Usage: 2.5%
Load Average: 0.10, 0.15, 0.12
=== Memory Performance Test ===
Memory Usage: 50%
Swap Usage: 0%
=== Disk I/O Performance Test ===
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.2345 s, 870 MB/s
Disk write test completed.
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.5678 s, 1.9 GB/s
Disk read test completed.
=== Network Performance Test ===
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.123 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.123 ms
— 192.168.1.1 ping statistics —
4 packets transmitted, 4 received, 0% packet loss
rtt min/avg/max/mdev = 0.123/0.123/0.123/0.000 ms
# 10. 查看日志
[root@localhost ~]# tail -f /var/log/performance-compare.log
[2026-04-03 10:00:00] Starting performance comparison…
[2026-04-03 10:00:01] Testing CPU performance…
[2026-04-03 10:00:02] Testing memory performance…
[2026-04-03 10:00:03] Testing disk I/O performance…
[2026-04-03 10:00:04] Testing network performance…
[2026-04-03 10:00:05] Performance comparison completed. Report saved to /var/log/performance-compare-report.txt
提示
系统优化需要根据实际应用场景进行调整。建议在优化前进行性能测试,优化后进行对比测试,验证优化效果。定期监控系统性能,及时调整优化策略。
