1. 首页 > Linux教程 > 正文

Linux教程FG174-网络性能调优

内容大纲

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

1. 网络性能调优概述

网络性能调优是通过调整网络参数、TCP参数、网络接口等,提高网络性能的过程。

# 网络性能调优的主要内容
# 网络参数调优:调整内核网络参数
# TCP参数调优:优化TCP连接
# 网络接口调优:优化网络接口
# 网络缓存优化:优化网络缓存
# 网络连接优化:优化网络连接
# 网络协议优化:优化网络协议

2. 网络参数调优

调整内核网络参数优化性能。

更多视频教程www.fgedu.net.cn

# 网络参数调优

# 1. 查看网络参数
[root@localhost ~]# sysctl -a | grep net.core
net.core.somaxconn = 128
net.core.netdev_max_backlog = 1000
net.core.rmem_default = 212992
net.core.rmem_max = 212992
net.core.wmem_default = 212992
net.core.wmem_max = 212992

# 2. 调整somaxconn参数
[root@localhost ~]# sysctl -w net.core.somaxconn=65535
net.core.somaxconn = 65535

# 3. 调整netdev_max_backlog参数
[root@localhost ~]# sysctl -w net.core.netdev_max_backlog=5000
net.core.netdev_max_backlog = 5000

# 4. 调整rmem_default参数
[root@localhost ~]# sysctl -w net.core.rmem_default=262144
net.core.rmem_default = 262144

# 5. 调整rmem_max参数
[root@localhost ~]# sysctl -w net.core.rmem_max=16777216
net.core.rmem_max = 16777216

# 6. 调整wmem_default参数
[root@localhost ~]# sysctl -w net.core.wmem_default=262144
net.core.wmem_default = 262144

# 7. 调整wmem_max参数
[root@localhost ~]# sysctl -w net.core.wmem_max=16777216
net.core.wmem_max = 16777216

# 8. 永久保存网络参数
[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 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 EOF # 9. 应用网络参数 [root@localhost ~]# sysctl -p /etc/sysctl.d/99-network-tuning.conf 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 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216

3. TCP参数调优

风哥提示:

优化TCP参数提高网络性能。

# TCP参数调优

# 1. 查看TCP参数
[root@localhost ~]# sysctl -a | grep net.ipv4.tcp
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_fin_timeout = 60
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 75

# 2. 调整tcp_fin_timeout参数
[root@localhost ~]# sysctl -w net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_fin_timeout = 30

# 3. 调整tcp_keepalive_time参数
[root@localhost ~]# sysctl -w net.ipv4.tcp_keepalive_time=600
net.ipv4.tcp_keepalive_time = 600

# 4. 调整tcp_keepalive_probes参数
[root@localhost ~]# sysctl -w net.ipv4.tcp_keepalive_probes=5
net.ipv4.tcp_keepalive_probes = 5

# 5. 调整tcp_keepalive_intvl参数
[root@localhost ~]# sysctl -w net.ipv4.tcp_keepalive_intvl=30
net.ipv4.tcp_keepalive_intvl = 30

# 6. 调整tcp_tw_reuse参数
[root@localhost ~]# sysctl -w net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_reuse = 1

# 7. 调整tcp_tw_recycle参数
[root@localhost ~]# sysctl -w net.ipv4.tcp_tw_recycle=0
net.ipv4.tcp_tw_recycle = 0

# 8. 永久保存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 # 9. 应用TCP参数 [root@localhost ~]# sysctl -p /etc/sysctl.d/99-tcp-tuning.conf 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

4. 网络接口调优

学习交流加群风哥QQ113257174

优化网络接口参数提高性能。

# 网络接口调优

# 1. 查看网络接口参数
[root@localhost ~]# ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
MDI-X: off
Supports Wake-on: pumbg
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link timer ifdown
Link detected: yes

# 2. 设置网络接口速度
[root@localhost ~]# ethtool -s eth0 speed 1000 duplex full autoneg on

# 3. 设置网络接口MTU
[root@localhost ~]# ip link set eth0 mtu 9000

# 4. 查看网络接口队列
[root@localhost ~]# ethtool -l eth0
Channel parameters for eth0:
Pre-set maximums:
RX: 0
TX: 0
Other: 0
Combined: 0
Current hardware settings:
RX: 0
TX: 0
Other: 0
Combined: 8

# 5. 设置网络接口队列
[root@localhost ~]# ethtool -L eth0 combined 16

# 6. 查看网络接口统计
[root@localhost ~]# ethtool -S eth0
NIC statistics:
rx_packets: 12345678
tx_packets: 12345678
rx_bytes: 1234567890
tx_bytes: 1234567890
rx_errors: 0
tx_errors: 0
rx_dropped: 0
tx_dropped: 0

# 7. 永久设置网络接口参数
[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 # 8. 设置脚本执行权限 [root@localhost ~]# chmod +x /etc/NetworkManager/dispatcher.d/99-eth0-tuning

更多学习教程公众号风哥教程itpux_com

5. 网络性能监控

使用iftop、nethogs等工具监控网络性能。

# 网络性能监控

# 1. 使用iftop监控网络流量
[root@localhost ~]# iftop -i eth0
# iftop启动后,显示网络流量信息

# 2. 使用nethogs监控网络流量
[root@localhost ~]# nethogs eth0
# nethogs启动后,显示进程网络流量

# 3. 使用sar监控网络
[root@localhost ~]# sar -n DEV 1 3
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)

10:00:00 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil
10:00:01 AM eth0 100.00 50.00 10.00 5.00 0.00 0.00 0.00 0.10
10:00:01 AM lo 100.00 100.00 10.00 10.00 0.00 0.00 0.00 0.00

10:00:01 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil
10:00:02 AM eth0 100.00 50.00 10.00 5.00 0.00 0.00 0.00 0.10
10:00:02 AM lo 100.00 100.00 10.00 10.00 0.00 0.00 0.00 0.00

10:00:02 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil
10:00:03 AM eth0 100.00 50.00 10.00 5.00 0.00 0.00 0.00 0.10
10:00:03 AM lo 100.00 100.00 10.00 10.00 0.00 0.00 0.00 0.00
Average: eth0 100.00 50.00 10.00 5.00 0.00 0.00 0.00 0.10
Average: lo 100.00 100.00 10.00 10.00 0.00 0.00 0.00 0.00

# 4. 使用ss监控网络连接
[root@localhost ~]# ss -s
Total: 100
TCP: 10 (estab 3, closed 0, orphaned 0, timewait 0)

Transport Total IP IPv6
* 100 – –
RAW 0 0 0
UDP 2 2 0
TCP 10 8 2
INET 12 10 2
FRAG 0 0 0

6. 实战案例

网络性能调优实战案例。

# 实战案例:网络性能调优

# 1. 查看网络接口信息
[root@localhost ~]# ip addr show eth0
2: eth0: mtu 1500 qdisc fq_codel state UP group default qlen 1000
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

# 2. 调整网络参数
[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 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 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 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 # 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 > /usr/local/bin/network-tuning.sh << 'EOF' #!/bin/bash # script.sh # from:www.itpux.com.qq113257174.wx:itpux-com # web: http://www.fgedu.net.cn # 网络调优脚本 LOG_FILE="/var/log/network-tuning.log" # 记录日志函数 log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}

# 设置网络接口参数
set_network_interface() {
log “Setting network interface parameters…”
ethtool -s eth0 speed 1000 duplex full autoneg on
ip link set eth0 mtu 9000
ethtool -L eth0 combined 16
log “Network interface parameters set.”
}

# 检查网络连接数
check_connections() {
CONNECTIONS=$(ss -s | grep TCP | awk ‘{print $2}’)

if [ “$CONNECTIONS” -gt 10000 ]; then
log “WARNING: TCP connections count is ${CONNECTIONS}”
fi
}

# 主函数
main() {
log “Starting network tuning…”
set_network_interface
check_connections
log “Network tuning completed.”
}

# 执行主函数
main
EOF

# 6. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/network-tuning.sh

# 7. 配置开机自动执行
[root@localhost ~]# cat > /etc/systemd/system/network-tuning.service << 'EOF' [Unit] Description=Network Tuning After=network.target [Service] Type=oneshot ExecStart=/usr/local/bin/network-tuning.sh RemainAfterExit=yes [Install] WantedBy=multi-user.target EOF # 8. 启用并启动服务 [root@localhost ~]# systemctl enable --now network-tuning.service Created symlink /etc/systemd/system/multi-user.target.wants/network-tuning.service → /etc/systemd/system/network-tuning.service. Created symlink /etc/systemd/system/network-tuning.service → /etc/systemd/system/network-tuning.service. # 9. 查看服务状态 [root@localhost ~]# systemctl status network-tuning.service ● network-tuning.service - Network Tuning Loaded: loaded (/etc/systemd/system/network-tuning.service; enabled; preset: disabled) Active: active (exited) since Fri 2026-04-03 10:00:00 CST; 5s ago Process: 12345 ExecStart=/usr/local/bin/network-tuning.sh (code=exited, status=0/SUCCESS) Main PID: 12345 (code=exited, status=0/SUCCESS) Apr 03 10:00:00 localhost systemd[1]: Starting Network Tuning... Apr 03 10:00:00 localhost network-tuning.sh[12345]: [2026-04-03 10:00:00] Starting network tuning... Apr 03 10:00:00 localhost network-tuning.sh[12345]: [2026-04-03 10:00:00] Setting network interface parameters... Apr 03 10:00:00 localhost network-tuning.sh[12345]: [2026-04-03 10:00:00] Network interface parameters set. Apr 03 10:00:00 localhost network-tuning.sh[12345]: [2026-04-03 10:00:00] Network tuning completed. Apr 03 10:00:00 localhost systemd[1]: Finished Network Tuning. # 10. 查看日志 [root@localhost ~]# tail -f /var/log/network-tuning.log [2026-04-03 10:00:00] Starting network tuning... [2026-04-03 10:00:00] Setting network interface parameters... [2026-04-03 10:00:00] Network interface parameters set. [2026-04-03 10:00:00] Network tuning completed.

联系我们

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

微信号:itpux-com

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