萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Linux 修改 MTU (最大傳輸單元) 完整指南:臨時、永久與 Jumbo Frame 啟用

MTU 核心概念

  • 乙太網標準 MTU = 1500 Bytes (不含 18 Bytes 幀頭尾)。
  • PPPoE 封裝額外 8 Bytes → 實際有效載荷 1492 Bytes。
  • Jumbo Frame 通常設 9000 Bytes,需交換機/路由器/網卡全鏈路支援。
  • Path MTU Discovery (PMTUD) 依賴 ICMP "Packet Too Big",防火牆封鎖 ICMP 會導致黑洞連線。

1. 臨時修改 (立即生效,重啟失效)

# 語法
ip link set dev <介面> mtu <值>
ifconfig <介面> mtu <值> up

# 範例:啟用 Jumbo Frame
ip link set dev eth0 mtu 9000
# 驗證
ip link show eth0 | grep mtu

2. 永久修改 (持久化)

CentOS/RHEL/Fedora (NetworkManager / network-scripts)

# NetworkManager (推薦)
nmcli con mod "System eth0" 802-3-ethernet.mtu 9000
nmcli con up "System eth0"

# 傳統 network-scripts
vi /etc/sysconfig/network-scripts/ifcfg-eth0
MTU=9000
# IPv6 另加
IPV6_MTU=1280
systemctl restart network

Debian/Ubuntu (systemd-networkd / netplan / interfaces)

# /etc/network/interfaces (傳統)
auto eth0
iface eth0 inet static
    address 192.168.1.10/24
    gateway 192.168.1.1
    mtu 9000

# Netplan (Ubuntu 18.04+)
# /etc/netplan/01-netcfg.yaml
network:
  version: 2
  ethernets:
    eth0:
      mtu: 9000
      addresses: [192.168.1.10/24]
      gateway4: 192.168.1.1
# 套用
netplan apply

# systemd-networkd
# /etc/systemd/network/10-eth0.network
[Link]
MTUBytes=9000
systemctl restart systemd-networkd

3. 交換機/路由器端同步啟用 Jumbo Frame

  • Cisco: system mtu 9000 (全域) 或 interface gi1/0/1; mtu 9000。
  • H3C: jumboframe enable / mtu 9000。
  • 家用路由器:Web 介面「Jumbo Frame」或「MTU」設為 9000。

注意:僅修改主機 MTU 而交換機未啟用,會導致大封包被丟棄、連線不穩。

4. MTU 測試與最佳值探測

# ping 測試 (Linux/macOS)
ping -M do -s 1472 192.168.1.1   # 1472 + 28 (IP+ICMP頭) = 1500
# 逐步增加 -s 數值直到出現 "local error: Message too long"
# 最大成功值 + 28 = 實際可用 MTU

# Windows
ping -f -l 1472 192.168.1.1

# 自動化腳本探測
for mtu in {1500..9000..100}; do
  if ping -c 1 -M do -s $((mtu-28)) 192.168.1.1 >/dev/null 2>&1; then
    echo "MTU $mtu OK"
  else
    echo "MTU $mtu FAIL"; break
  fi
done

5. TCP MSS Clamping (解決 PMTUD 黑洞)

# iptables 調整 MSS (通常 = MTU - 40)
iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1460
# nftables
nft add rule ip filter forward tcp flags syn tcp option maxseg size set 1460
copyright © 萬盛學電腦網 all rights reserved