1. 首页 > Linux教程 > 正文

Linux教程FG123-内核参数临时调优(sysctl -w)

本文档风哥主要介绍Linux内核参数临时调优,包括sysctl -w命令的使用、常见内核参数调优、生产环境实战案例等内容,参考Red Hat Enterprise Linux 10官方文档中的System
administration章节,适合系统管理员在生产环境中使用。更多视频教程www.fgedu.net.cn

Part01-基础概念与理论知识

1.1 内核参数临时调优概念

内核参数临时调优是指通过sysctl命令临时修改内核参数,无需重启系统即可生效,但重启后会恢复默认值。这种调优方式适用于测试和临时调整,不适合长期使用。学习交流加群风哥微信: itpux-com

临时调优的特点:

  • 无需重启系统,立即生效
  • 重启后恢复默认值
  • 适合测试和临时调整
  • 可以快速验证参数效果
  • 不影响系统启动配置

1.2 sysctl命令基础

sysctl命令用于查看和修改内核参数,是Linux系统中常用的系统调优工具。

# 查看所有内核参数
$ sysctl -a

# 查看指定内核参数
$ sysctl net.ipv4.tcp_max_syn_backlog

# 临时修改内核参数
$ sysctl -w net.ipv4.tcp_max_syn_backlog=65535

# 查看修改后的参数
$ sysctl net.ipv4.tcp_max_syn_backlog

1.3 临时调优原则

临时调优的原则:

  • 先测试后应用:在测试环境中验证参数效果
  • 逐步调整:从小到大逐步调整参数值
  • 监控效果:调整后监控系统性能变化
  • 记录变更:记录所有临时调整的参数
  • 及时恢复:如果出现问题,及时恢复默认值
风哥提示:临时调优是测试参数效果的好方法,但不要在生产环境中依赖临时调优,重要的参数调整应该通过永久配置实现。

Part02-生产环境规划与建议

2.1 常见内核参数

常见的内核参数包括:

# 网络参数
net.core.somaxconn = 65535 # 最大连接数
net.ipv4.tcp_max_syn_backlog = 65535 # SYN队列长度
net.ipv4.tcp_fin_timeout = 30 # FIN超时时间
net.ipv4.tcp_keepalive_time = 1200 # 保活时间

# 内存参数
vm.swappiness = 10 # 交换分区使用倾向
vm.overcommit_memory = 1 # 内存过度分配
vm.overcommit_ratio = 90 # 内存过度分配比例

# 文件系统参数
fs.file-max = 65536 # 最大文件句柄数

# 内核参数
kernel.sem = 250 32000 100 128 # 信号量参数
kernel.shmmax = 4294967296 # 最大共享内存段
kernel.shmall = 1048576 # 共享内存总大小

2.2 调优建议

调优建议:

  • 网络服务器:增加连接数和队列长度
  • 数据库服务器:优化内存和I/O参数
  • 高并发系统:调整文件句柄和网络参数
  • 内存密集型应用:优化内存管理参数

2.3 测试策略

测试策略:

  1. 建立基准性能测试
  2. 逐步调整参数
  3. 运行性能测试
  4. 分析测试结果
  5. 确定最佳参数值
生产环境建议:临时调优后,应将有效的参数通过永久配置保存,避免重启后丢失。学习交流加群风哥QQ113257174

Part03-生产环境项目实施方案

3.1 sysctl -w命令使用

3.1.1 基本使用方法

# 临时修改单个参数
$ sysctl -w net.core.somaxconn=65535
net.core.somaxconn = 65535

# 临时修改多个参数
$ sysctl -w net.ipv4.tcp_max_syn_backlog=65535 net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 30

# 从文件加载参数
$ sysctl -p /etc/sysctl.d/99-custom.conf

3.1.2 查看当前参数值

# 查看单个参数
$ sysctl net.core.somaxconn
net.core.somaxconn = 65535

# 查看多个参数
$ sysctl net.core.somaxconn net.ipv4.tcp_max_syn_backlog
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535

# 查看所有网络相关参数
$ sysctl -a | grep net

3.2 参数验证

参数验证步骤:

# 1. 查看当前值
$ sysctl net.core.somaxconn
net.core.somaxconn = 128

# 2. 修改参数
$ sysctl -w net.core.somaxconn=65535
net.core.somaxconn = 65535

# 3. 验证修改结果
$ sysctl net.core.somaxconn
net.core.somaxconn = 65535

# 4. 验证系统行为
$ ss -s
Total: 12345 (kernel 123456)
TCP: 1234 (estab 123, closed 1111, orphaned 0, synrecv 0, timewait 111/0), ports 123

Transport Total IP IPv6
* 12345 – –
RAW 0 0 0
UDP 123 123 0
TCP 1234 1234 0
INET 1357 1357 0
FRAG 0 0 0

3.3 错误处理

常见错误及处理:

# 错误:参数不存在
$ sysctl -w net.ipv4.tcp_invalid_param=1
sysctl: cannot stat /proc/sys/net/ipv4/tcp_invalid_param: No such file or directory

# 错误:权限不足
$ sysctl -w net.core.somaxconn=65535
sysctl: setting key “net.core.somaxconn”: Permission denied

# 错误:参数值无效
$ sysctl -w net.core.somaxconn=-1
sysctl: setting key “net.core.somaxconn”: Invalid argument

风哥提示:临时调优时遇到错误,应检查参数名称是否正确、权限是否足够、参数值是否在有效范围内。更多学习教程公众号风哥教程itpux_com

Part04-生产案例与实战讲解

4.1 网络性能调优案例

案例:Web服务器网络性能调优

# 查看当前网络参数
$ sysctl net.core.somaxconn net.ipv4.tcp_max_syn_backlog net.ipv4.tcp_fin_timeout
net.core.somaxconn = 128
net.ipv4.tcp_max_syn_backlog = 128
net.ipv4.tcp_fin_timeout = 60

# 临时调整网络参数
$ sysctl -w net.core.somaxconn=65535 net.ipv4.tcp_max_syn_backlog=65535 net.ipv4.tcp_fin_timeout=30
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 30

# 验证调整结果
$ sysctl net.core.somaxconn net.ipv4.tcp_max_syn_backlog net.ipv4.tcp_fin_timeout
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 30

# 测试网络性能
$ ab -n 10000 -c 1000 http://localhost/

This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests

Server Software: nginx/1.24.0
Server Hostname: localhost
Server Port: 80

Document Path: /
Document Length: 612 bytes

Concurrency Level: 1000
Time taken for tests: 2.345 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 8380000 bytes
HTML transferred: 6120000 bytes
Requests per second: 4264.56 [#/sec] (mean)
Time per request: 234.500 [ms] (mean)
Time per request: 0.235 [ms] (mean, across all concurrent requests)
Transfer rate: 3498.76 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 10 5.2 9 123
Processing: 5 25 8.7 23 156
Waiting: 2 18 6.3 16 123
Total: 5 35 10.1 32 234

Percentage of the requests served within a certain time (ms)
50% 32
66% 35
75% 38
80% 40
90% 45
95% 50
98% 60
99% 70
100% 234 (longest request)

4.2 内存性能调优案例

案例:数据库服务器内存调优

# 查看当前内存参数
$ sysctl vm.swappiness vm.overcommit_memory
vm.swappiness = 60
vm.overcommit_memory = 0

# 临时调整内存参数
$ sysctl -w vm.swappiness=10 vm.overcommit_memory=1
vm.swappiness = 10
vm.overcommit_memory = 1

# 验证调整结果
$ sysctl vm.swappiness vm.overcommit_memory
vm.swappiness = 10
vm.overcommit_memory = 1

# 监控内存使用
$ free -h
total used free shared buff/cache available
Mem: 32G 16G 8G 1.5G 8G 14G
Swap: 8G 0B 8G

$ vmstat 1 10
procs ———–memory———- —swap– —–io—- -system– ——cpu—–
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 8388608 4194304 8388608 0 0 0 0 60 120 5 5 90 0 0
0 0 0 8388608 4194304 8388608 0 0 0 0 62 125 5 5 90 0 0
0 0 0 8388608 4194304 8388608 0 0 0 0 58 118 5 5 90 0 0
0 0 0 8388608 4194304 8388608 0 0 0 0 61 122 5 5 90 0 0
0 0 0 8388608 4194304 8388608 0 0 0 0 60 121 5 5 90 0 0
0 0 0 8388608 4194304 8388608 0 0 0 0 63 124 5 5 90 0 0
0 0 0 8388608 4194304 8388608 0 0 0 0 59 119 5 5 90 0 0
0 0 0 8388608 4194304 8388608 0 0 0 0 61 123 5 5 90 0 0
0 0 0 8388608 4194304 8388608 0 0 0 0 60 120 5 5 90 0 0
0 0 0 8388608 4194304 8388608 0 0 0 0 62 125 5 5 90 0 0

4.3 磁盘性能调优案例

案例:文件服务器磁盘调优

# 查看当前I/O调度器
$ cat /sys/block/sda/queue/scheduler
[mq-deadline] kyber bfq none

# 临时调整I/O调度器
$ echo “bfq” > /sys/block/sda/queue/scheduler

# 验证调整结果
$ cat /sys/block/sda/queue/scheduler
mq-deadline kyber [bfq] none

# 查看当前文件句柄限制
$ sysctl fs.file-max
fs.file-max = 65536

# 临时调整文件句柄限制
$ sysctl -w fs.file-max=655360
fs.file-max = 655360

# 验证调整结果
$ sysctl fs.file-max
fs.file-max = 655360

# 测试磁盘性能
$ dd if=/dev/zero of=/tmp/test bs=1M count=1000 oflag=direct
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 2.345 s, 447 MB/s

生产环境建议:临时调优验证有效后,应将参数通过永久配置保存,确保系统重启后仍然生效。from Linux:www.itpux.com

Part05-风哥经验总结与分享

5.1 最佳实践

内核参数临时调优的最佳实践:

  • 先测试后应用:在测试环境中验证参数效果
  • 逐步调整:从小到大逐步调整参数值
  • 监控效果:调整后监控系统性能变化
  • 记录变更:记录所有临时调整的参数
  • 及时恢复:如果出现问题,及时恢复默认值
  • 永久保存:验证有效的参数通过永久配置保存

5.2 故障排查

常见问题及排查:

  1. 参数修改后系统性能下降:恢复默认值,重新调整
  2. 参数修改后服务无法启动:恢复默认值,检查参数值范围
  3. 参数修改后系统不稳定:重启系统,恢复默认配置
  4. 参数名称错误:检查参数名称是否正确
  5. 权限不足:使用root用户或sudo执行

5.3 风哥建议

风哥建议:

  • 建立调优基线:记录系统默认参数和性能基准
  • 制定调优计划:根据系统角色制定调优计划
  • 定期review:定期review调优效果,调整策略
  • 文档化:记录所有调优过程和结果
  • 持续学习:关注内核新特性和调优技巧
风哥提示:内核参数调优是一个持续的过程,需要根据系统负载和应用需求不断调整。建议建立调优文档,记录每次调优的参数、效果和原因。

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息