活动公告

系统通知
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,资源失效请在帖子内回复要求补档,会尽快处理!
10-23 09:31

AlmaLinux网络配置深度优化实战指南全面提升服务器性能与安全性的专业技巧从基础设置到高级调优的完整解决方案

SunJu_FaceMall

3万

主题

2720

科技点

3万

积分

执行版主

碾压王

积分
32881

塔罗立华奏

执行版主 发表于 2025-8-30 18:10:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

AlmaLinux作为RHEL的下游分支,以其稳定性、安全性和长期支持而受到企业用户的青睐。在当今数字化时代,服务器网络性能和安全性对业务运行至关重要。本文将全面介绍AlmaLinux网络配置的深度优化技术,从基础设置到高级调优,帮助系统管理员和运维工程师打造高性能、高安全性的服务器网络环境。

1. 基础网络配置

1.1 网络接口配置

在AlmaLinux中,网络接口配置主要通过NetworkManager或传统的network服务进行管理。NetworkManager是现代Linux系统推荐的默认网络管理工具。
  1. # 查看所有网络接口
  2. ip addr show
  3. # 或使用传统命令
  4. ifconfig -a
复制代码
  1. # 列出所有连接
  2. nmcli connection show
  3. # 创建新的以太网连接
  4. nmcli connection add type ethernet ifname eth0 con-name eth0-static
  5. # 配置静态IP
  6. nmcli connection modify eth0-static ipv4.addresses 192.168.1.100/24
  7. nmcli connection modify eth0-static ipv4.gateway 192.168.1.1
  8. nmcli connection modify eth0-static ipv4.dns "8.8.8.8 8.8.4.4"
  9. nmcli connection modify eth0-static ipv4.method manual
  10. # 启用连接
  11. nmcli connection up eth0-static
复制代码

对于习惯使用传统配置文件的管理员,可以编辑/etc/sysconfig/network-scripts/ifcfg-<interface>文件:
  1. # 编辑eth0配置文件
  2. vi /etc/sysconfig/network-scripts/ifcfg-eth0
复制代码

配置文件内容示例:
  1. TYPE=Ethernet
  2. BOOTPROTO=static
  3. DEFROUTE=yes
  4. PEERDNS=yes
  5. PEERROUTES=yes
  6. IPV4_FAILURE_FATAL=no
  7. IPV6INIT=yes
  8. IPV6_AUTOCONF=yes
  9. IPV6_DEFROUTE=yes
  10. IPV6_PEERDNS=yes
  11. IPV6_PEERROUTES=yes
  12. IPV6_FAILURE_FATAL=no
  13. NAME=eth0
  14. UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  15. DEVICE=eth0
  16. ONBOOT=yes
  17. IPADDR=192.168.1.100
  18. PREFIX=24
  19. GATEWAY=192.168.1.1
  20. DNS1=8.8.8.8
  21. DNS2=8.8.4.4
复制代码

重启网络服务:
  1. # 使用NetworkManager
  2. nmcli connection reload
  3. nmcli connection up eth0
  4. # 或使用传统network服务
  5. systemctl restart network
复制代码

1.2 静态IP与DHCP配置

静态IP适用于服务器环境,确保IP地址不会变化。如上节所示,可以通过nmcli或ifcfg文件配置静态IP。

对于客户端或不需要固定IP的服务器,可以使用DHCP自动获取IP:
  1. # 使用nmcli配置DHCP
  2. nmcli connection modify eth0-dhcp ipv4.method auto
  3. nmcli connection up eth0-dhcp
复制代码

在ifcfg文件中配置DHCP:
  1. TYPE=Ethernet
  2. BOOTPROTO=dhcp
  3. DEFROUTE=yes
  4. PEERDNS=yes
  5. PEERROUTES=yes
  6. IPV4_FAILURE_FATAL=no
  7. IPV6INIT=yes
  8. IPV6_AUTOCONF=yes
  9. IPV6_DEFROUTE=yes
  10. IPV6_PEERDNS=yes
  11. IPV6_PEERROUTES=yes
  12. IPV6_FAILURE_FATAL=no
  13. NAME=eth0
  14. UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  15. DEVICE=eth0
  16. ONBOOT=yes
复制代码

1.3 主机名与DNS设置
  1. # 查看当前主机名
  2. hostnamectl status
  3. # 设置静态主机名
  4. hostnamectl set-hostname server1.example.com
  5. # 编辑/etc/hosts文件确保本地解析
  6. vi /etc/hosts
复制代码

在/etc/hosts文件中添加:
  1. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  2. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  3. 192.168.1.100 server1.example.com server1
复制代码

除了在网络接口配置中指定DNS服务器外,还可以通过修改/etc/resolv.conf文件配置DNS:
  1. # 编辑resolv.conf文件
  2. vi /etc/resolv.conf
复制代码

内容示例:
  1. search example.com
  2. nameserver 8.8.8.8
  3. nameserver 8.8.4.4
  4. nameserver 192.168.1.1
复制代码

注意:如果使用NetworkManager,直接编辑resolv.conf可能会被覆盖。建议通过nmcli或修改ifcfg文件来配置DNS。

1.4 基本网络测试工具
  1. # 测试网络连通性
  2. ping -c 4 google.com
  3. # 持续ping并记录时间戳
  4. ping -D google.com
复制代码
  1. # 跟踪网络路径
  2. traceroute google.com
  3. # 使用ICMP协议进行跟踪
  4. traceroute -I google.com
复制代码

mtr结合了ping和traceroute的功能,提供更详细的网络诊断信息:
  1. # 使用mtr诊断网络路径
  2. mtr google.com
  3. # 使用报告模式,输出10次测试结果
  4. mtr -r -c 10 google.com
复制代码
  1. # 使用netstat查看所有网络连接
  2. netstat -tulnp
  3. # 使用ss命令(更快更现代的替代品)
  4. ss -tulnp
  5. # 查看路由表
  6. netstat -rn
  7. # 或
  8. ip route show
复制代码
  1. # 使用nslookup查询DNS
  2. nslookup google.com
  3. # 使用dig获取更详细的DNS信息
  4. dig google.com
  5. # 查询特定类型的DNS记录
  6. dig google.com MX
复制代码

2. 网络性能优化

2.1 内核参数调优

AlmaLinux的内核参数可以通过修改/etc/sysctl.conf或/etc/sysctl.d/目录下的文件进行优化。
  1. # 编辑sysctl配置文件
  2. vi /etc/sysctl.d/99-network.conf
复制代码

添加以下内容:
  1. # 增加文件描述符限制
  2. fs.file-max = 100000
  3. # 增加网络端口范围
  4. net.ipv4.ip_local_port_range = 1024 65535
  5. # 启用TCP SYN cookie保护,防止SYN Flood攻击
  6. net.ipv4.tcp_syncookies = 1
  7. # 允许重用TIME_WAIT套接字用于新连接
  8. net.ipv4.tcp_tw_reuse = 1
  9. # 快速回收TIME_WAIT套接字
  10. net.ipv4.tcp_tw_recycle = 0
  11. # TCP连接队列长度
  12. net.core.somaxconn = 65535
  13. # 增加TCP最大缓冲区大小
  14. net.core.rmem_max = 16777216
  15. net.core.wmem_max = 16777216
  16. net.ipv4.tcp_rmem = 4096 87380 16777216
  17. net.ipv4.tcp_wmem = 4096 65536 16777216
  18. # 优化TCP拥塞控制算法
  19. net.ipv4.tcp_congestion_control = bbr
  20. # 启用TCP窗口缩放
  21. net.ipv4.tcp_window_scaling = 1
  22. # 优化网络设备队列长度
  23. net.core.netdev_max_backlog = 5000
  24. # 允许更多的PMTU发现
  25. net.ipv4.tcp_no_metrics_save = 1
  26. # 减少keepalive探测时间
  27. net.ipv4.tcp_keepalive_time = 600
  28. net.ipv4.tcp_keepalive_intvl = 60
  29. net.ipv4.tcp_keepalive_probes = 3
  30. # 禁用IP源路由
  31. net.ipv4.conf.all.accept_source_route = 0
  32. net.ipv4.conf.default.accept_source_route = 0
  33. # 禁用ICMP重定向
  34. net.ipv4.conf.all.accept_redirects = 0
  35. net.ipv4.conf.default.accept_redirects = 0
  36. # 启用反向路径过滤
  37. net.ipv4.conf.all.rp_filter = 1
  38. net.ipv4.conf.default.rp_filter = 1
  39. # 记录可疑数据包
  40. net.ipv4.conf.all.log_martians = 1
  41. # 忽略ICMP广播请求
  42. net.ipv4.icmp_echo_ignore_broadcasts = 1
  43. # 忽略 bogus ICMP 错误响应
  44. net.ipv4.icmp_ignore_bogus_error_responses = 1
  45. # 启用IP转发(如果服务器用作路由器)
  46. # net.ipv4.ip_forward = 1
复制代码

应用配置:
  1. # 立即应用sysctl配置
  2. sysctl -p /etc/sysctl.d/99-network.conf
  3. # 或应用所有sysctl配置
  4. sysctl -p
复制代码

对于高负载Web服务器或数据库服务器,可以添加以下优化:
  1. # 增加最大连接跟踪表大小
  2. net.netfilter.nf_conntrack_max = 1000000
  3. # 减少conntrack超时时间
  4. net.netfilter.nf_conntrack_tcp_timeout_established = 300
  5. net.netfilter.nf_conntrack_tcp_timeout_time_wait = 1
  6. net.netfilter.nf_conntrack_tcp_timeout_close_wait = 10
  7. net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 10
  8. # 优化TCP内存限制
  9. net.ipv4.tcp_mem = 786432 1048576 1572864
  10. # 增加最大半连接队列长度
  11. net.ipv4.tcp_max_syn_backlog = 65536
  12. # 启用TCP Fast Open
  13. net.ipv4.tcp_fastopen = 3
  14. # 优化TCP重传超时时间
  15. net.ipv4.tcp_retries2 = 5
复制代码

2.2 网络缓冲区优化

网络缓冲区大小对网络性能有直接影响。可以通过以下方式优化:
  1. # 安装ethtool
  2. dnf install -y ethtool
  3. # 查看当前网络接口设置
  4. ethtool -g eth0
  5. # 启用自动调整网络缓冲区
  6. ethtool -G eth0 rx 4096 tx 4096
复制代码

要使设置永久生效,可以创建NetworkManager调度程序脚本:
  1. # 创建脚本目录
  2. mkdir -p /etc/NetworkManager/dispatcher.d
  3. # 创建脚本文件
  4. vi /etc/NetworkManager/dispatcher.d/02-ethtool-settings
复制代码

添加以下内容:
  1. #!/bin/bash
  2. # 获取接口名称
  3. INTERFACE=$1
  4. # 获取连接状态
  5. STATUS=$2
  6. # 仅在接口启动时应用设置
  7. if [ "$STATUS" = "up" ]; then
  8.     case "$INTERFACE" in
  9.         eth*)
  10.             ethtool -G $INTERFACE rx 4096 tx 4096
  11.             ethtool -K $INTERFACE gso on
  12.             ethtool -K $INTERFACE tso on
  13.             ethtool -K $INTERFACE ufo on
  14.             ethtool -K $INTERFACE gro on
  15.             ;;
  16.     esac
  17. fi
复制代码

使脚本可执行:
  1. chmod +x /etc/NetworkManager/dispatcher.d/02-ethtool-settings
复制代码

2.3 网络协议优化
  1. # 查看当前拥塞控制算法
  2. sysctl net.ipv4.tcp_congestion_control
  3. # 查看可用的拥塞控制算法
  4. sysctl net.ipv4.tcp_available_congestion_control
复制代码

现代AlmaLinux内核支持多种拥塞控制算法,如BBR、CUBIC等。BBR(Bottleneck Bandwidth and RTT)是Google开发的高性能拥塞控制算法,特别适合高延迟、高带宽的网络环境。

启用BBR:
  1. # 确保内核加载了BBR模块
  2. modprobe tcp_bbr
  3. # 设置BBR为默认拥塞控制算法
  4. sysctl -w net.ipv4.tcp_congestion_control=bbr
  5. # 使设置永久生效
  6. echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.d/99-network.conf
复制代码

对于使用大量UDP连接的应用(如DNS、流媒体服务器等),可以优化UDP缓冲区:
  1. # 增加UDP接收缓冲区大小
  2. sysctl -w net.core.rmem_max=26214400
  3. sysctl -w net.ipv4.udp_rmem_min=8192
  4. # 增加UDP发送缓冲区大小
  5. sysctl -w net.core.wmem_max=26214400
  6. sysctl -w net.ipv4.udp_wmem_min=8192
  7. # 使设置永久生效
  8. echo "net.core.rmem_max=26214400" >> /etc/sysctl.d/99-network.conf
  9. echo "net.ipv4.udp_rmem_min=8192" >> /etc/sysctl.d/99-network.conf
  10. echo "net.core.wmem_max=26214400" >> /etc/sysctl.d/99-network.conf
  11. echo "net.ipv4.udp_wmem_min=8192" >> /etc/sysctl.d/99-network.conf
复制代码

2.4 多队列网卡优化

现代服务器通常配备支持多队列的网卡,可以利用多核CPU并行处理网络中断,提高网络性能。
  1. # 查看网卡支持的队列数
  2. ethtool -l eth0
复制代码
  1. # 查看当前RSS设置
  2. ethtool -x eth0
  3. # 配置RSS indirection table
  4. ethtool -X eth0 equal 4
复制代码

RPS是软件层面的多核处理,可以在不支持硬件多队列的网卡上实现类似效果。
  1. # 为eth0启用RPS
  2. echo ff > /sys/class/net/eth0/queues/rx-0/rps_cpus
  3. # 设置RPS流限制
  4. echo 32768 > /proc/sys/net/core/rps_sock_flow_entries
复制代码

要使RPS设置永久生效,可以创建udev规则:
  1. # 创建udev规则文件
  2. vi /etc/udev/rules.d/99-network-rps.rules
复制代码

添加以下内容:
  1. ACTION=="add", SUBSYSTEM=="net", KERNEL=="eth*", RUN+="/bin/sh -c 'echo ff > /sys/class/net/$name/queues/rx-0/rps_cpus'"
复制代码

XPS是发送端的CPU亲和性设置,可以提高发送性能:
  1. # 为eth0启用XPS
  2. echo ff > /sys/class/net/eth0/queues/tx-0/xps_cpus
复制代码
  1. # 查看网络接口的IRQ分配
  2. grep eth0 /proc/interrupts
  3. # 设置IRQ亲和性
  4. echo 1 > /proc/irq/123/smp_affinity
复制代码

要使IRQ亲和性设置永久生效,可以创建systemd服务:
  1. # 创建systemd服务文件
  2. vi /etc/systemd/system/set-network-irq-affinity.service
复制代码

添加以下内容:
  1. [Unit]
  2. Description=Set network IRQ affinity
  3. After=network.target
  4. [Service]
  5. Type=oneshot
  6. ExecStart=/bin/sh -c 'for i in $(grep eth0 /proc/interrupts | cut -d: -f1 | sed "s/ //g"); do echo 1 > /proc/irq/$i/smp_affinity; done'
  7. [Install]
  8. WantedBy=multi-user.target
复制代码

启用服务:
  1. systemctl enable set-network-irq-affinity.service
  2. systemctl start set-network-irq-affinity.service
复制代码

3. 网络安全加固

3.1 防火墙配置

AlmaLinux默认使用firewalld作为防火墙管理工具,它基于nftables/iptables实现。
  1. # 安装firewalld
  2. dnf install -y firewalld
  3. # 启动并启用firewalld
  4. systemctl start firewalld
  5. systemctl enable firewalld
  6. # 查看默认区域
  7. firewall-cmd --get-default-zone
  8. # 查看活动区域
  9. firewall-cmd --get-active-zones
  10. # 查看当前区域规则
  11. firewall-cmd --list-all
  12. # 开放端口
  13. firewall-cmd --permanent --add-port=80/tcp
  14. firewall-cmd --permanent --add-port=443/tcp
  15. # 开放服务
  16. firewall-cmd --permanent --add-service=http
  17. firewall-cmd --permanent --add-service=https
  18. # 重新加载防火墙规则
  19. firewall-cmd --reload
复制代码
  1. # 创建自定义区域
  2. firewall-cmd --permanent --new-zone=webserver
  3. firewall-cmd --permanent --zone=webserver --set-target=DROP
  4. # 为自定义区域添加规则
  5. firewall-cmd --permanent --zone=webserver --add-service=http
  6. firewall-cmd --permanent --zone=webserver --add-service=https
  7. firewall-cmd --permanent --zone=webserver --add-port=22/tcp
  8. # 将接口分配到自定义区域
  9. firewall-cmd --permanent --zone=webserver --change-interface=eth0
  10. # 启用富规则(rich rules)
  11. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'
  12. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" drop'
  13. # 重新加载防火墙规则
  14. firewall-cmd --reload
复制代码

对于更复杂的防火墙需求,可以直接使用nftables:
  1. # 安装nftables
  2. dnf install -y nftables
  3. # 启动并启用nftables
  4. systemctl start nftables
  5. systemctl enable nftables
  6. # 创建nftables规则集
  7. vi /etc/nftables/firewall.nft
复制代码

添加以下内容:
  1. #!/usr/sbin/nft -f
  2. # 清除所有规则
  3. flush ruleset
  4. # 创建表
  5. table inet filter {
  6.     # 创建链
  7.     chain input {
  8.         type filter hook input priority 0; policy drop;
  9.         # 允许本地回环
  10.         iifname lo accept comment "Accept loopback"
  11.         # 允许已建立的连接和相关的连接
  12.         ct state established,related accept comment "Allow established/related connections"
  13.         # 允许ICMP
  14.         icmp type { echo-request, echo-reply, destination-unreachable } accept comment "Allow ICMP"
  15.         # 允许SSH
  16.         tcp dport 22 accept comment "Allow SSH"
  17.         # 允许HTTP/HTTPS
  18.         tcp dport { 80, 443 } accept comment "Allow HTTP/HTTPS"
  19.         # 记录并拒绝其他连接
  20.         log prefix "Dropped: " level info
  21.         reject with icmp type admin-prohibited
  22.     }
  23.     chain forward {
  24.         type filter hook forward priority 0; policy drop;
  25.     }
  26.     chain output {
  27.         type filter hook output priority 0; policy accept;
  28.     }
  29. }
复制代码

应用规则:
  1. nft -f /etc/nftables/firewall.nft
  2. # 保存规则
  3. nft list ruleset > /etc/nftables/ruleset-save
复制代码

3.2 SELinux网络策略

SELinux是AlmaLinux中的强制访问控制系统,可以提供额外的网络安全保护。
  1. # 检查SELinux状态
  2. sestatus
  3. # 设置SELinux为强制模式
  4. setenforce 1
  5. vi /etc/selinux/config
复制代码

确保/etc/selinux/config文件中包含:
  1. SELINUX=enforcing
  2. SELINUXTYPE=targeted
复制代码
  1. # 查看与网络相关的SELinux布尔值
  2. getsebool -a | grep http
  3. getsebool -a | grep ftp
  4. getsebool -a | grep nis
  5. # 允许HTTP脚本网络连接
  6. setsebool -P httpd_can_network_connect=1
  7. # 允许FTP匿名写入
  8. setsebool -P ftpd_anon_write=1
  9. # 允许NFS共享目录
  10. setsebool -P nfs_export_all_rw=1
复制代码
  1. # 查看服务允许的端口
  2. semanage port -l | grep http
  3. # 添加新端口到SELinux策略
  4. semanage port -a -t http_port_t -p tcp 8080
  5. # 查看端口上下文
  6. semanage port -l | grep 8080
复制代码
  1. # 安装SELinux故障排除工具
  2. dnf install -y setroubleshoot-server
  3. # 查看SELinux拒绝日志
  4. sealert -a /var/log/audit/audit.log
  5. # 启用SELinux日志
  6. auditctl -w /var/log/audit/audit.log -p wa -k selinux
复制代码

3.3 网络服务安全配置
  1. # 备份SSH配置文件
  2. cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
  3. # 编辑SSH配置文件
  4. vi /etc/ssh/sshd_config
复制代码

建议的安全配置:
  1. # 禁用root登录
  2. PermitRootLogin no
  3. # 仅允许特定用户登录
  4. AllowUsers user1 user2
  5. # 禁用密码认证,仅允许密钥认证
  6. PasswordAuthentication no
  7. PubkeyAuthentication yes
  8. # 更改默认端口
  9. Port 2222
  10. # 禁用空密码
  11. PermitEmptyPasswords no
  12. # 限制登录尝试次数
  13. MaxAuthTries 3
  14. # 设置登录超时时间
  15. LoginGraceTime 60
  16. # 禁用X11转发
  17. X11Forwarding no
  18. # 仅使用协议2
  19. Protocol 2
  20. # 禁用.rhosts文件认证
  21. IgnoreRhosts yes
  22. # 禁用基于主机的认证
  23. HostbasedAuthentication no
  24. # 设置客户端保持活动时间间隔
  25. ClientAliveInterval 300
  26. ClientAliveCountMax 3
  27. # 使用更强的加密算法
  28. Ciphers chacha20-poly1305@openssl.com,aes256-gcm@openssl.com,aes128-gcm@openssl.com,aes256-ctr,aes192-ctr,aes128-ctr
  29. MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256
  30. KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
复制代码

重启SSH服务:
  1. systemctl restart sshd
复制代码
  1. # 在客户端生成SSH密钥对
  2. ssh-keygen -t ed25519 -b 4096
  3. # 将公钥复制到服务器
  4. ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
  5. # 或者手动复制公钥到服务器
  6. cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
  7. # 设置服务器上的密钥文件权限
  8. chmod 700 ~/.ssh
  9. chmod 600 ~/.ssh/authorized_keys
复制代码
  1. # 安装EPEL仓库
  2. dnf install -y epel-release
  3. # 安装fail2ban
  4. dnf install -y fail2ban
  5. # 启动并启用fail2ban
  6. systemctl start fail2ban
  7. systemctl enable fail2ban
  8. # 复制配置文件
  9. cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  10. # 编辑配置文件
  11. vi /etc/fail2ban/jail.local
复制代码

配置示例:
  1. [DEFAULT]
  2. # 禁止时间(秒)
  3. bantime = 3600
  4. # 找到失败次数
  5. findtime = 600
  6. # 最大尝试次数
  7. maxretry = 3
  8. [sshd]
  9. enabled = true
  10. port = 2222
  11. filter = sshd
  12. logpath = /var/log/secure
  13. maxretry = 3
  14. bantime = 3600
  15. [nginx-http-auth]
  16. enabled = true
  17. filter = nginx-http-auth
  18. port = http,https
  19. logpath = /var/log/nginx/error.log
  20. maxretry = 3
  21. bantime = 3600
复制代码

重启fail2ban:
  1. systemctl restart fail2ban
复制代码

3.4 DDoS防护措施
  1. # 使用iptables限制每分钟新连接数
  2. iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 50 -j DROP
  3. iptables -A INPUT -p tcp --dport 80 -m limit --limit 50/minute --limit-burst 100 -j ACCEPT
  4. # 使用nftables限制连接速率
  5. nft add table inet filter
  6. nft add chain inet filter input { type filter hook input priority 0 \; }
  7. nft add rule inet filter input tcp dport 80 ct state new limit rate 50/minute burst 100 packets accept
  8. nft add rule inet filter input tcp dport 80 ct state new drop
复制代码
  1. # 增加SYN队列长度
  2. sysctl -w net.ipv4.tcp_max_syn_backlog=65536
  3. # 启用SYN cookies
  4. sysctl -w net.ipv4.tcp_syncookies=1
  5. # 减少SYN-ACK重传次数
  6. sysctl -w net.ipv4.tcp_synack_retries=2
  7. # 减少SYN重传次数
  8. sysctl -w net.ipv4.tcp_syn_retries=2
  9. # 使设置永久生效
  10. echo "net.ipv4.tcp_max_syn_backlog=65536" >> /etc/sysctl.d/99-network.conf
  11. echo "net.ipv4.tcp_syncookies=1" >> /etc/sysctl.d/99-network.conf
  12. echo "net.ipv4.tcp_synack_retries=2" >> /etc/sysctl.d/99-network.conf
  13. echo "net.ipv4.tcp_syn_retries=2" >> /etc/sysctl.d/99-network.conf
复制代码
  1. # 下载APF
  2. cd /usr/src
  3. wget http://www.rfxn.com/downloads/apf-current.tar.gz
  4. tar -xvzf apf-current.tar.gz
  5. cd apf-*
  6. # 安装APF
  7. ./install.sh
  8. # 配置APF
  9. vi /etc/apf/conf.apf
复制代码

重要配置项:
  1. # 启用防火墙
  2. DEVEL_MODE="0"
  3. # 设置入站和出站连接策略
  4. IG_TCP_CPORTS="22,80,443"
  5. IG_UDP_CPORTS=""
  6. EG_TCP_CPORTS="21,25,80,443,43"
  7. EG_UDP_CPORTS="20,21,53"
  8. # 启用DOS防护
  9. USE_AD="1"
复制代码

启动APF:
  1. /etc/apf/apf -s
复制代码

如果使用nginx作为Web服务器,可以配置请求限制:
  1. http {
  2.     limit_req_zone $binary_remote_addr zone=login:10m rate=10r/m;
  3.    
  4.     server {
  5.         location /login {
  6.             limit_req zone=login burst=20 nodelay;
  7.             proxy_pass http://backend;
  8.         }
  9.     }
  10. }
复制代码

对于大型网站,建议使用Cloudflare等CDN服务来缓解DDoS攻击。这些服务提供:

1. 分布式流量清洗
2. 基于行为的DDoS检测
3. 自动攻击缓解
4. 全球流量分发

4. 高级网络配置

4.1 网络绑定与冗余

网络绑定(Bonding)可以将多个物理网络接口组合为一个逻辑接口,提高带宽和冗余性。
  1. # 安装绑定工具
  2. dnf install -y bonding-utils
  3. # 加载绑定模块
  4. modprobe bonding
  5. # 创建绑定配置文件
  6. vi /etc/sysconfig/network-scripts/ifcfg-bond0
复制代码

添加以下内容:
  1. DEVICE=bond0
  2. TYPE=Bond
  3. NAME=bond0
  4. BONDING_MASTER=yes
  5. IPADDR=192.168.1.100
  6. PREFIX=24
  7. GATEWAY=192.168.1.1
  8. DNS1=8.8.8.8
  9. DNS2=8.8.4.4
  10. ONBOOT=yes
  11. BOOTPROTO=none
  12. BONDING_OPTS="mode=4 miimon=100 lacp_rate=1 xmit_hash_policy=layer3+4"
复制代码

配置从属接口:
  1. # 编辑eth0配置文件
  2. vi /etc/sysconfig/network-scripts/ifcfg-eth0
复制代码

内容:
  1. DEVICE=eth0
  2. TYPE=Ethernet
  3. NAME=eth0
  4. UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  5. ONBOOT=yes
  6. BOOTPROTO=none
  7. MASTER=bond0
  8. SLAVE=yes
复制代码
  1. # 编辑eth1配置文件
  2. vi /etc/sysconfig/network-scripts/ifcfg-eth1
复制代码

内容:
  1. DEVICE=eth1
  2. TYPE=Ethernet
  3. NAME=eth1
  4. UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  5. ONBOOT=yes
  6. BOOTPROTO=none
  7. MASTER=bond0
  8. SLAVE=yes
复制代码

重启网络服务:
  1. systemctl restart network
复制代码

1. mode=0 (balance-rr): 轮询模式,提供负载均衡和容错
2. mode=1 (active-backup): 主备模式,提供容错
3. mode=2 (balance-xor): XOR模式,提供负载均衡和容错
4. mode=3 (broadcast): 广播模式,提供容错
5. mode=4 (802.3ad): IEEE 802.3ad动态链接聚合,提供负载均衡和容错
6. mode=5 (balance-tlb): 自适应传输负载均衡,提供负载均衡和容错
7. mode=6 (balance-alb): 自适应负载均衡,提供负载均衡和容错
  1. # 查看绑定状态
  2. cat /proc/net/bonding/bond0
  3. # 查看绑定接口状态
  4. ip link show bond0
复制代码

4.2 VLAN配置

VLAN(虚拟局域网)允许在物理网络上创建逻辑隔离的子网。
  1. # 安装VLAN工具
  2. dnf install -y vconfig
  3. # 加载802.1q模块
  4. modprobe 8021q
  5. # 创建VLAN接口配置文件
  6. vi /etc/sysconfig/network-scripts/ifcfg-bond0.100
复制代码

添加以下内容:
  1. DEVICE=bond0.100
  2. VLAN=yes
  3. TYPE=Vlan
  4. PHYSDEV=bond0
  5. VLAN_ID=100
  6. NAME=bond0.100
  7. ONBOOT=yes
  8. BOOTPROTO=none
  9. IPADDR=192.168.100.100
  10. PREFIX=24
复制代码

重启网络服务:
  1. systemctl restart network
复制代码
  1. # 创建VLAN连接
  2. nmcli connection add type vlan ifname bond0.100 dev bond0 id 100
  3. # 配置VLAN连接
  4. nmcli connection modify vlan-bond0.100 ipv4.addresses 192.168.100.100/24
  5. nmcli connection modify vlan-bond0.100 ipv4.method manual
  6. # 启用VLAN连接
  7. nmcli connection up vlan-bond0.100
复制代码

4.3 网络桥接

网络桥接允许将多个网络接口连接到同一个广播域,常用于虚拟化环境。
  1. # 安装网桥工具
  2. dnf install -y bridge-utils
  3. # 创建桥接接口配置文件
  4. vi /etc/sysconfig/network-scripts/ifcfg-br0
复制代码

添加以下内容:
  1. DEVICE=br0
  2. TYPE=Bridge
  3. NAME=br0
  4. ONBOOT=yes
  5. BOOTPROTO=none
  6. IPADDR=192.168.1.100
  7. PREFIX=24
  8. GATEWAY=192.168.1.1
  9. DNS1=8.8.8.8
  10. DNS2=8.8.4.4
  11. DELAY=0
  12. STP=off
复制代码

配置从属接口:
  1. # 编辑eth0配置文件
  2. vi /etc/sysconfig/network-scripts/ifcfg-eth0
复制代码

内容:
  1. DEVICE=eth0
  2. TYPE=Ethernet
  3. NAME=eth0
  4. UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  5. ONBOOT=yes
  6. BOOTPROTO=none
  7. BRIDGE=br0
复制代码

重启网络服务:
  1. systemctl restart network
复制代码
  1. # 创建桥接连接
  2. nmcli connection add type bridge ifname br0
  3. # 配置桥接连接
  4. nmcli connection modify bridge-br0 ipv4.addresses 192.168.1.100/24
  5. nmcli connection modify bridge-br0 ipv4.method manual
  6. nmcli connection modify bridge-br0 bridge.stp no
  7. # 将接口添加到桥接
  8. nmcli connection add type ethernet slave-type bridge ifname eth0 master br0
  9. # 启用桥接连接
  10. nmcli connection up bridge-br0
  11. nmcli connection up bridge-slave-eth0
复制代码
  1. # 查看桥接状态
  2. brctl show
  3. # 查看桥接接口状态
  4. ip link show br0
复制代码

4.4 高级路由策略

策略路由允许根据数据包的源地址、目标地址、端口等属性选择不同的路由表。
  1. # 创建自定义路由表
  2. echo "200 custom_table" >> /etc/iproute2/rt_tables
  3. # 添加路由规则
  4. ip rule add from 192.168.1.0/24 table custom_table
  5. ip rule add to 192.168.1.0/24 table custom_table
  6. # 添加路由到自定义表
  7. ip route add default via 192.168.1.1 table custom_table
  8. ip route add 192.168.1.0/24 dev eth0 table custom_table
  9. # 刷新路由缓存
  10. ip route flush cache
复制代码

要使设置永久生效,可以创建网络脚本:
  1. # 创建路由脚本
  2. vi /etc/network-scripts/route-eth0
复制代码

添加以下内容:
  1. 192.168.1.0/24 via 192.168.1.1 dev eth0 table custom_table
  2. default via 192.168.1.1 dev eth0 table custom_table
复制代码
  1. # 创建规则脚本
  2. vi /etc/network-scripts/rule-eth0
复制代码

添加以下内容:
  1. from 192.168.1.0/24 table custom_table
  2. to 192.168.1.0/24 table custom_table
复制代码

多宿主路由允许服务器连接到多个网络,并根据策略选择出口。
  1. # 添加默认路由到第一个ISP
  2. ip route add default via 10.0.0.1 dev eth0 table 100
  3. # 添加默认路由到第二个ISP
  4. ip route add default via 20.0.0.1 dev eth1 table 200
  5. # 添加路由规则
  6. ip rule add from 10.0.0.0/24 table 100
  7. ip rule add from 20.0.0.0/24 table 200
  8. # 设置主路由表
  9. ip route add default via 10.0.0.1 dev eth0
  10. # 刷新路由缓存
  11. ip route flush cache
复制代码
  1. # 创建多路径路由
  2. ip route add default nexthop via 10.0.0.1 dev eth0 weight 1 nexthop via 20.0.0.1 dev eth1 weight 1
  3. # 设置源路由
  4. ip rule add from 10.0.0.0/24 table 100
  5. ip rule add from 20.0.0.0/24 table 200
  6. # 添加路由到表
  7. ip route add 10.0.0.0/24 dev eth0 table 100
  8. ip route add 20.0.0.0/24 dev eth1 table 200
  9. ip route add default via 10.0.0.1 dev eth0 table 100
  10. ip route add default via 20.0.0.1 dev eth1 table 200
  11. # 刷新路由缓存
  12. ip route flush cache
复制代码

5. 网络监控与故障排除

5.1 网络监控工具
  1. # 安装iftop
  2. dnf install -y iftop
  3. # 监控eth0接口流量
  4. iftop -i eth0
  5. # 显示端口信息
  6. iftop -P -i eth0
  7. # 显示特定网段的流量
  8. iftop -F 192.168.1.0/24 -i eth0
复制代码
  1. # 安装nethogs
  2. dnf install -y nethogs
  3. # 监控进程网络使用
  4. nethogs eth0
  5. # 按流量排序
  6. nethogs -t eth0
复制代码
  1. # 安装vnStat
  2. dnf install -y vnstat
  3. # 初始化数据库
  4. vnstat -u -i eth0
  5. # 查看流量统计
  6. vnstat -i eth0
  7. # 实时监控
  8. vnstat -l -i eth0
复制代码
  1. # 安装bmon
  2. dnf install -y bmon
  3. # 启动bmon
  4. bmon -p eth0
复制代码
  1. # 安装Prometheus Node Exporter
  2. dnf install -y prometheus-node-exporter
  3. # 启动Node Exporter
  4. systemctl start prometheus-node-exporter
  5. systemctl enable prometheus-node-exporter
  6. # 配置Prometheus抓取数据
  7. vi /etc/prometheus/prometheus.yml
复制代码

添加以下内容:
  1. scrape_configs:
  2.   - job_name: 'node_exporter'
  3.     static_configs:
  4.       - targets: ['localhost:9100']
复制代码

重启Prometheus:
  1. systemctl restart prometheus
复制代码

在Grafana中导入Node Exporter仪表板(ID:1860)来监控网络性能。

5.2 性能分析工具
  1. # 安装tcpdump
  2. dnf install -y tcpdump
  3. # 捕获eth0接口上的所有数据包
  4. tcpdump -i eth0
  5. # 捕获特定主机的数据包
  6. tcpdump -i eth0 host 192.168.1.100
  7. # 捕获特定端口的数据包
  8. tcpdump -i eth0 port 80
  9. # 捕获HTTP请求
  10. tcpdump -i eth0 -A 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
  11. # 保存捕获结果到文件
  12. tcpdump -i eth0 -w capture.pcap
  13. # 读取捕获文件
  14. tcpdump -r capture.pcap
复制代码
  1. # 安装Wireshark
  2. dnf install -y wireshark
  3. # 添加用户到wireshark组
  4. usermod -a -G wireshark $USER
  5. newgrp wireshark
  6. # 启动Wireshark GUI
  7. wireshark
复制代码
  1. # 安装netperf
  2. dnf install -y netperf
  3. # 在服务器端启动netserver
  4. netserver
  5. # 在客户端测试TCP吞吐量
  6. netperf -H server_ip -t TCP_STREAM
  7. # 测试UDP吞吐量
  8. netperf -H server_ip -t UDP_STREAM
  9. # 测试请求/响应性能
  10. netperf -H server_ip -t TCP_RR
复制代码
  1. # 安装iperf3
  2. dnf install -y iperf3
  3. # 在服务器端启动iperf3
  4. iperf3 -s
  5. # 在客户端测试TCP带宽
  6. iperf3 -c server_ip
  7. # 测试UDP带宽
  8. iperf3 -c server_ip -u
  9. # 测试双向带宽
  10. iperf3 -c server_ip -d
  11. # 指定测试时间
  12. iperf3 -c server_ip -t 60
复制代码

5.3 常见问题及解决方案

问题:网络接口无法启动,错误信息”Device not found”

解决方案:
  1. # 检查接口是否存在
  2. ip link show
  3. # 检查驱动是否加载
  4. lspci | grep -i ethernet
  5. lsmod | grep e1000
  6. # 重新加载驱动
  7. modprobe -r e1000
  8. modprobe e1000
  9. # 检查NetworkManager状态
  10. systemctl status NetworkManager
  11. journalctl -u NetworkManager
复制代码

问题:网络连接缓慢,延迟高

解决方案:
  1. # 检查网络接口错误
  2. ethtool -S eth0
  3. # 检查网络队列
  4. ethtool -g eth0
  5. # 调整网络缓冲区
  6. ethtool -G eth0 rx 4096 tx 4096
  7. # 检查网络拥塞控制算法
  8. sysctl net.ipv4.tcp_congestion_control
  9. # 更改为BBR算法
  10. sysctl -w net.ipv4.tcp_congestion_control=bbr
  11. # 检查MTU设置
  12. ip link show eth0
  13. # 测试最佳MTU
  14. ping -M do -s 1472 192.168.1.1
复制代码

问题:网络连接频繁断开,不稳定

解决方案:
  1. # 检查网络接口状态
  2. ip link show eth0
  3. # 检查网络接口统计
  4. ethtool -S eth0
  5. # 禁用节能功能
  6. ethtool -s eth0 wol d
  7. ethtool -s eth0 autoneg off speed 1000 duplex full
  8. # 检查网络电缆和交换机
  9. # 尝试更换网络电缆或交换机端口
  10. # 检查系统日志
  11. journalctl -k | grep eth0
复制代码

问题:DNS解析缓慢,影响网络访问

解决方案:
  1. # 检查DNS配置
  2. cat /etc/resolv.conf
  3. # 测试DNS解析速度
  4. time dig example.com
  5. # 使用更快的DNS服务器
  6. echo "nameserver 8.8.8.8" > /etc/resolv.conf
  7. echo "nameserver 1.1.1.1" >> /etc/resolv.conf
  8. # 安装并启用本地DNS缓存
  9. dnf install -y unbound
  10. systemctl start unbound
  11. systemctl enable unbound
  12. # 配置本地DNS缓存
  13. echo "nameserver 127.0.0.1" > /etc/resolv.conf
复制代码

问题:检测到网络攻击,如SYN Flood、DDoS等

解决方案:
  1. # 检查网络连接
  2. ss -tuna | grep SYN_RECV
  3. # 使用iptables限制连接速率
  4. iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j DROP
  5. # 使用sysctl参数防止SYN Flood
  6. sysctl -w net.ipv4.tcp_syncookies=1
  7. sysctl -w net.ipv4.tcp_max_syn_backlog=65536
  8. sysctl -w net.ipv4.tcp_synack_retries=2
  9. # 使用fail2ban防止暴力破解
  10. systemctl start fail2ban
  11. # 使用APF防火墙
  12. /etc/apf/apf -r
  13. # 考虑使用专业的DDoS防护服务
复制代码

6. 最佳实践与案例研究

6.1 Web服务器网络优化案例

某电子商务网站在AlmaLinux上运行,面临高并发访问和网络安全挑战。

1. 网络接口配置
  1. # 配置双网卡绑定
  2. vi /etc/sysconfig/network-scripts/ifcfg-bond0
复制代码

内容:
  1. DEVICE=bond0
  2. TYPE=Bond
  3. NAME=bond0
  4. BONDING_MASTER=yes
  5. IPADDR=203.0.113.100
  6. PREFIX=24
  7. GATEWAY=203.0.113.1
  8. DNS1=8.8.8.8
  9. DNS2=8.8.4.4
  10. ONBOOT=yes
  11. BOOTPROTO=none
  12. BONDING_OPTS="mode=4 miimon=100 lacp_rate=1 xmit_hash_policy=layer3+4"
复制代码

1. 内核参数优化
  1. # 编辑sysctl配置文件
  2. vi /etc/sysctl.d/99-webserver.conf
复制代码

内容:
  1. # 增加文件描述符限制
  2. fs.file-max = 100000
  3. # 增加网络端口范围
  4. net.ipv4.ip_local_port_range = 1024 65535
  5. # TCP优化
  6. net.ipv4.tcp_tw_reuse = 1
  7. net.ipv4.tcp_fin_timeout = 10
  8. net.ipv4.tcp_keepalive_time = 600
  9. net.ipv4.tcp_keepalive_intvl = 60
  10. net.ipv4.tcp_keepalive_probes = 3
  11. net.ipv4.tcp_max_syn_backlog = 65536
  12. net.ipv4.tcp_syncookies = 1
  13. net.ipv4.tcp_synack_retries = 2
  14. net.ipv4.tcp_syn_retries = 2
  15. net.ipv4.tcp_retries2 = 5
  16. net.ipv4.tcp_rmem = 4096 87380 16777216
  17. net.ipv4.tcp_wmem = 4096 65536 16777216
  18. net.core.rmem_max = 16777216
  19. net.core.wmem_max = 16777216
  20. net.core.netdev_max_backlog = 5000
  21. net.core.somaxconn = 65535
  22. # 使用BBR拥塞控制算法
  23. net.ipv4.tcp_congestion_control = bbr
  24. net.ipv4.tcp_fastopen = 3
  25. # 增加连接跟踪表大小
  26. net.netfilter.nf_conntrack_max = 1000000
  27. net.netfilter.nf_conntrack_tcp_timeout_established = 300
复制代码

应用配置:
  1. sysctl -p /etc/sysctl.d/99-webserver.conf
复制代码

1. 网络缓冲区优化
  1. # 创建NetworkManager调度程序脚本
  2. vi /etc/NetworkManager/dispatcher.d/02-ethtool-settings
复制代码

内容:
  1. #!/bin/bash
  2. INTERFACE=$1
  3. STATUS=$2
  4. if [ "$STATUS" = "up" ]; then
  5.     case "$INTERFACE" in
  6.         bond*)
  7.             ethtool -G $INTERFACE rx 4096 tx 4096
  8.             ethtool -K $INTERFACE gso on
  9.             ethtool -K $INTERFACE tso on
  10.             ethtool -K $INTERFACE ufo on
  11.             ethtool -K $INTERFACE gro on
  12.             ;;
  13.     esac
  14. fi
复制代码

使脚本可执行:
  1. chmod +x /etc/NetworkManager/dispatcher.d/02-ethtool-settings
复制代码

1. 防火墙配置
  1. # 配置firewalld
  2. firewall-cmd --permanent --add-service=http
  3. firewall-cmd --permanent --add-service=https
  4. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" drop'
  5. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="172.16.0.0/12" drop'
  6. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.0.0/16" drop'
  7. firewall-cmd --reload
复制代码

1. Nginx配置优化
  1. user nginx;
  2. worker_processes auto;
  3. worker_rlimit_nofile 100000;
  4. events {
  5.     worker_connections 4000;
  6.     use epoll;
  7.     multi_accept on;
  8. }
  9. http {
  10.     open_file_cache max=200000 inactive=20s;
  11.     open_file_cache_valid 30s;
  12.     open_file_cache_min_uses 2;
  13.     open_file_cache_errors on;
  14.     sendfile on;
  15.     tcp_nopush on;
  16.     tcp_nodelay on;
  17.     keepalive_timeout 30;
  18.     keepalive_requests 100000;
  19.     reset_timedout_connection on;
  20.     client_body_timeout 10;
  21.     send_timeout 2;
  22.     # 限制请求速率
  23.     limit_req_zone $binary_remote_addr zone=login:10m rate=10r/m;
  24.     limit_req_zone $binary_remote_addr zone=api:10m rate=100r/m;
  25.     server {
  26.         listen 80 reuseport;
  27.         listen 443 ssl reuseport;
  28.         # 限制登录请求
  29.         location /login {
  30.             limit_req zone=login burst=20 nodelay;
  31.             proxy_pass http://backend;
  32.         }
  33.         # 限制API请求
  34.         location /api {
  35.             limit_req zone=api burst=200 nodelay;
  36.             proxy_pass http://backend;
  37.         }
  38.     }
  39. }
复制代码

1. 监控配置
  1. # 安装Prometheus Node Exporter
  2. dnf install -y prometheus-node-exporter
  3. systemctl start prometheus-node-exporter
  4. systemctl enable prometheus-node-exporter
  5. # 安装nginx_exporter
  6. dnf install -y nginx-exporter
  7. systemctl start nginx-exporter
  8. systemctl enable nginx-exporter
复制代码

经过优化后,该电子商务网站的并发处理能力提高了300%,页面加载时间减少了50%,成功抵御了多次DDoS攻击,网络稳定性显著提高。

6.2 数据库服务器网络优化案例

某金融机构的AlmaLinux数据库服务器需要处理大量交易数据,对网络延迟和吞吐量要求极高。

1. 网络接口配置
  1. # 配置双网卡绑定和VLAN
  2. vi /etc/sysconfig/network-scripts/ifcfg-bond0
复制代码

内容:
  1. DEVICE=bond0
  2. TYPE=Bond
  3. NAME=bond0
  4. BONDING_MASTER=yes
  5. ONBOOT=yes
  6. BOOTPROTO=none
  7. BONDING_OPTS="mode=4 miimon=100 lacp_rate=1 xmit_hash_policy=layer3+4"
复制代码
  1. # 配置VLAN接口
  2. vi /etc/sysconfig/network-scripts/ifcfg-bond0.100
复制代码

内容:
  1. DEVICE=bond0.100
  2. VLAN=yes
  3. TYPE=Vlan
  4. PHYSDEV=bond0
  5. VLAN_ID=100
  6. NAME=bond0.100
  7. ONBOOT=yes
  8. BOOTPROTO=none
  9. IPADDR=10.10.100.100
  10. PREFIX=24
复制代码

1. 内核参数优化
  1. # 编辑sysctl配置文件
  2. vi /etc/sysctl.d/99-database.conf
复制代码

内容:
  1. # 增加文件描述符限制
  2. fs.file-max = 100000
  3. # 增加网络端口范围
  4. net.ipv4.ip_local_port_range = 1024 65535
  5. # TCP优化
  6. net.ipv4.tcp_tw_reuse = 1
  7. net.ipv4.tcp_fin_timeout = 10
  8. net.ipv4.tcp_keepalive_time = 1200
  9. net.ipv4.tcp_keepalive_intvl = 60
  10. net.ipv4.tcp_keepalive_probes = 3
  11. net.ipv4.tcp_max_syn_backlog = 65536
  12. net.ipv4.tcp_syncookies = 1
  13. net.ipv4.tcp_synack_retries = 2
  14. net.ipv4.tcp_syn_retries = 2
  15. net.ipv4.tcp_retries2 = 5
  16. net.ipv4.tcp_rmem = 4096 87380 16777216
  17. net.ipv4.tcp_wmem = 4096 65536 16777216
  18. net.core.rmem_max = 16777216
  19. net.core.wmem_max = 16777216
  20. net.core.netdev_max_backlog = 10000
  21. net.core.somaxconn = 65535
  22. # 使用BBR拥塞控制算法
  23. net.ipv4.tcp_congestion_control = bbr
  24. net.ipv4.tcp_fastopen = 3
  25. # 禁用TCP时间戳
  26. net.ipv4.tcp_timestamps = 0
  27. # 增加连接跟踪表大小
  28. net.netfilter.nf_conntrack_max = 1000000
  29. net.netfilter.nf_conntrack_tcp_timeout_established = 300
  30. # 优化内存管理
  31. vm.swappiness = 10
  32. vm.dirty_ratio = 15
  33. vm.dirty_background_ratio = 5
复制代码

应用配置:
  1. sysctl -p /etc/sysctl.d/99-database.conf
复制代码

1. 网络缓冲区和队列优化
  1. # 创建NetworkManager调度程序脚本
  2. vi /etc/NetworkManager/dispatcher.d/02-ethtool-settings
复制代码

内容:
  1. #!/bin/bash
  2. INTERFACE=$1
  3. STATUS=$2
  4. if [ "$STATUS" = "up" ]; then
  5.     case "$INTERFACE" in
  6.         bond*)
  7.             ethtool -G $INTERFACE rx 8192 tx 8192
  8.             ethtool -K $INTERFACE gso off
  9.             ethtool -K $INTERFACE tso off
  10.             ethtool -K $INTERFACE ufo off
  11.             ethtool -K $INTERFACE gro on
  12.             ethtool -C $INTERFACE rx-usecs 100 tx-usecs 100
  13.             ;;
  14.     esac
  15. fi
复制代码

使脚本可执行:
  1. chmod +x /etc/NetworkManager/dispatcher.d/02-ethtool-settings
复制代码

1. 多队列网卡优化
  1. # 配置RPS
  2. echo ff > /sys/class/net/bond0/queues/rx-0/rps_cpus
  3. echo ff > /sys/class/net/bond0/queues/rx-1/rps_cpus
  4. echo ff > /sys/class/net/bond0/queues/rx-2/rps_cpus
  5. echo ff > /sys/class/net/bond0/queues/rx-3/rps_cpus
  6. # 配置XPS
  7. echo ff > /sys/class/net/bond0/queues/tx-0/xps_cpus
  8. echo ff > /sys/class/net/bond0/queues/tx-1/xps_cpus
  9. echo ff > /sys/class/net/bond0/queues/tx-2/xps_cpus
  10. echo ff > /sys/class/net/bond0/queues/tx-3/xps_cpus
  11. # 设置RPS流限制
  12. echo 32768 > /proc/sys/net/core/rps_sock_flow_entries
复制代码

1. 防火墙配置
  1. # 配置firewalld
  2. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.10.0.0/16" port protocol="tcp" port="3306" accept'
  3. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.20.0.0/16" port protocol="tcp" port="3306" accept'
  4. firewall-cmd --reload
复制代码

1. 数据库网络配置
  1. # MySQL配置优化
  2. vi /etc/my.cnf
复制代码

添加以下内容:
  1. [mysqld]
  2. # 网络配置
  3. bind-address = 10.10.100.100
  4. port = 3306
  5. max_connections = 5000
  6. back_log = 2048
  7. max_connect_errors = 100000
  8. connect_timeout = 10
  9. wait_timeout = 28800
  10. interactive_timeout = 28800
  11. # 缓冲区配置
  12. max_allowed_packet = 256M
  13. thread_stack = 256K
  14. thread_cache_size = 300
  15. thread_concurrency = 16
  16. # InnoDB配置
  17. innodb_buffer_pool_size = 32G
  18. innodb_log_file_size = 2G
  19. innodb_log_buffer_size = 64M
  20. innodb_flush_log_at_trx_commit = 2
  21. innodb_flush_method = O_DIRECT
  22. innodb_file_per_table = 1
  23. innodb_read_io_threads = 16
  24. innodb_write_io_threads = 16
  25. innodb_thread_concurrency = 0
复制代码

1. 监控配置
  1. # 安装Prometheus Node Exporter
  2. dnf install -y prometheus-node-exporter
  3. systemctl start prometheus-node-exporter
  4. systemctl enable prometheus-node-exporter
  5. # 安装MySQL Exporter
  6. dnf install -y mysql_exporter
  7. systemctl start mysql_exporter
  8. systemctl enable mysql_exporter
复制代码

经过优化后,该金融机构的数据库服务器网络延迟降低了70%,吞吐量提高了200%,事务处理速度显著提升,成功应对了高峰期的交易负载,系统稳定性大幅提高。

7. 总结

本文全面介绍了AlmaLinux网络配置的深度优化技术,从基础设置到高级调优,涵盖了网络性能优化和安全加固的各个方面。通过合理配置网络接口、调整内核参数、优化网络协议、配置高级网络功能以及实施有效的安全措施,可以显著提高AlmaLinux服务器的网络性能和安全性。

关键要点包括:

1. 基础网络配置:正确配置网络接口、IP地址、DNS和主机名是网络优化的基础。
2. 网络性能优化:通过调整内核参数、网络缓冲区、拥塞控制算法和多队列网卡设置,可以显著提高网络性能。
3. 网络安全加固:使用防火墙、SELinux、安全服务配置和DDoS防护措施,可以有效提高网络安全性。
4. 高级网络配置:网络绑定、VLAN、网络桥接和高级路由策略可以提供更灵活、更可靠的网络解决方案。
5. 网络监控与故障排除:使用各种网络监控工具和性能分析工具,可以及时发现和解决网络问题。
6. 最佳实践与案例研究:通过实际案例展示了如何针对不同应用场景进行网络优化。

基础网络配置:正确配置网络接口、IP地址、DNS和主机名是网络优化的基础。

网络性能优化:通过调整内核参数、网络缓冲区、拥塞控制算法和多队列网卡设置,可以显著提高网络性能。

网络安全加固:使用防火墙、SELinux、安全服务配置和DDoS防护措施,可以有效提高网络安全性。

高级网络配置:网络绑定、VLAN、网络桥接和高级路由策略可以提供更灵活、更可靠的网络解决方案。

网络监控与故障排除:使用各种网络监控工具和性能分析工具,可以及时发现和解决网络问题。

最佳实践与案例研究:通过实际案例展示了如何针对不同应用场景进行网络优化。

网络优化是一个持续的过程,需要根据实际应用场景和需求不断调整和优化。希望本文能够帮助系统管理员和运维工程师更好地理解和掌握AlmaLinux网络配置优化技术,构建高性能、高安全性的服务器网络环境。
「七転び八起き(ななころびやおき)」
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则