Ubuntu网络配置优化指南
/etc/netplan/*.yaml)替代传统/etc/network/interfaces,支持静态IP和DHCP。静态IP配置示例:network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses: ["192.168.1.100/24"]
gateway4: 192.168.1.1
nameservers:
addresses: ["8.8.8.8", "8.8.4.4"]
应用配置:sudo netplan apply。动态IP则设置dhcp4: yes。/etc/sysctl.conf:net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 4194304
net.ipv4.tcp_rmem = "4096 87380 16777216"
net.ipv4.tcp_wmem = "4096 65536 16777216"
生效:sudo sysctl -p。sudo sysctl -w net.ipv4.tcp_congestion_control=bbr
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
验证:ss -tulnp | grep bbr(显示bbr即为生效)。sudo sysctl -w net.ipv4.tcp_fin_timeout = 30
sudo sysctl -w net.ipv4.tcp_tw_reuse = 1
sudo ip link set eth0 mtu 9000
开机自动生效:添加到/etc/rc.local(需赋予执行权限)。ethtool调整网卡队列大小:sudo ethtool -G eth0 rx 4096 tx 4096
/etc/security/limits.conf:* soft nofile 65535
* hard nofile 65535
临时生效:ulimit -n 65535。sudo sysctl -w net.ipv4.tcp_fastopen = 3
sudo ethtool -K eth0 tx off rx off sg on tso on gro on lro on
iftop(按流量排序)、nload(分接口统计)查看实时流量:sudo apt install iftop nload
sudo iftop -i eth0
sudo nload eth0
iperf3测试网络吞吐量(需服务器配合):sudo apt install iperf3
# 服务器端
iperf3 -s
# 客户端
iperf3 -c <服务器IP>
ss -tulnp查看当前TCP/UDP连接及进程信息,netstat(旧版)或ip命令替代。/etc/sysctl.conf:net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
sudo sysctl -p
sudo systemctl restart systemd-networkd # 或NetworkManager
sudo apt update && sudo apt upgrade
sudo ubuntu-drivers autoinstall # 自动安装合适的网络驱动
以上优化措施需根据实际网络环境(如家庭宽带、数据中心)和应用场景(如Web服务器、数据库)调整,建议通过监控工具验证效果后再全面应用。