● Linux系统优化及性能调优

调整最大TCP连接数:

sysctl -w net.ipv4.ip_local_port_range="1024 65535"
sysctl -w net.core.somaxconn="1024"
sysctl -w net.ipv4.tcp_max_syn_backlog="65535"

启用TCP连接的时间等待 (TCP TIME-WAIT) 状态减少:

sysctl -w net.ipv4.tcp_tw_reuse="1"
sysctl -w net.ipv4.tcp_tw_recycle="1"

调整TCP接收/发送缓冲区大小:

sysctl -w net.core.rmem_max="16777216"
sysctl -w net.core.wmem_max="16777216"
sysctl -w net.ipv4.tcp_rmem="4096 87380 16777216"
sysctl -w net.ipv4.tcp_wmem="4096 65536 16777216"

调整TCP KeepAlive 设置:

sysctl -w net.ipv4.tcp_keepalive_time="300"
sysctl -w net.ipv4.tcp_keepalive_intvl="15"
sysctl -w net.ipv4.tcp_keepalive_probes="5"

启用TCP的SYN Cookie保护:

sysctl -w net.ipv4.tcp_syncookies="1"

调整网络设备队列长度:

sysctl -w net.core.netdev_max_backlog="1000"

调整TCP的最大有效载荷段大小(MSS):

sysctl -w net.ipv4.tcp_mtu_probing="1"

开启路由转发功能: ① 临时开启:

sudo sysctl net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1

这将使IPv4的路由转发功能在当前会话中生效。在系统重新启动后,该设置将失效。

② 永久生效,编辑/etc/sysctl.conf文件:

net.ipv4.ip_forward = 1

使配置生效:

sudo sysctl -p

禁ping: ① 使用防火墙规则: 使用iptables禁止ping:

sudo iptables -A INPUT -p icmp --icmp-type echo-request -j DROP

使用firewalld:

sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" protocol value="icmp" drop'

② 修改内核参数,禁用ICMP协议,阻止ping请求。编辑/etc/sysctl.conf文件:

net.ipv4.icmp_echo_ignore_all = 1

汇总优化项目:

vi /etc/sysctl.conf

# 超时重传的次数 ==> 建议减少重试次数, 以太网环境下
net.ipv4.tcp_syn_retries = 3
net.ipv4.tcp_synack_retries = 3

# 队列满员情况下处理机制 ==> 建议保持默认
net.ipv4.tcp_abort_on_overflow = 0

# 半、全连接队列大小设置 ==> 建议适当增大
net.ipv4.tcp_max_syn_backlog = 1024
net.core.somaxconn = 1024

# 默认就开启的  ==>非外层设备也可以考虑关闭
net.ipv4.tcp_syncookies = 1

# 快速回收连接 ==> 建议减少超时时间
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1

# tcp 心跳保持 ==> 纯TCP服务建议调整
net.ipv4.tcp_keepalive = 120
net.ipv4.tcp_keepalive_intval = 30
net.ipv4.tcp_keepalive_probes = 3

# time wait 保持个数 ==> 建议适当增大
net.ipv4.tcp_max_tw_buckets = 30000 

sysctl -p

参考:《Linux系统优化及性能调优》