如何在 CLI 中永久设置 MTU

Nic*_*ick 5 networking windows-subsystem-for-linux 20.04

我在 WSL2 中使用 Ubuntu 20.04,并且无法永久设置 MTU。不管我怎么做,重启后还是1500。我在本机运行 Ubuntu 时遇到了同样的问题,然后切换到 Windows,因为 Ubuntu 无法通过 VPN 工作,因为 MTU 错误。

难道真的没有办法永久设置MTU吗?

我尝试了/etc/dhcp/dhclient.conf

  GNU nano 4.8                                                                                               /etc/dhcp/dhclient.conf                                                                                                         # Configuration file for /sbin/dhclient.                                                                                                                                                                                                     #                                                                                                                                                                                                                                            # This is a sample configuration file for dhclient. See dhclient.conf's                                                                                                                                                                      #       man page for more information about the syntax of this file                                                                                                                                                                          #       and a more comprehensive list of the parameters understood by                                                                                                                                                                        #       dhclient.                                                                                                                                                                                                                            #                                                                                                                                                                                                                                            # Normally, if the DHCP server provides reasonable information and does                                                                                                                                                                      #       not leave anything out (like the domain name, for example), then                                                                                                                                                                     #       few changes must be made to this file, if any.                                                                                                                                                                                       #                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;                                                                                                                                                                                                                                                                                                                                                                                                            send host-name = gethostname();                                                                                                                                                                                                              default interface-mtu 1340;                                                                                                                                                                                                                  supersede interface-mtu 1340;                                                                                                                                                                                                                request subnet-mask, broadcast-address, time-offset, routers,                                                                                                                                                                                        domain-name, domain-name-servers, domain-search, host-name,                                                                                                                                                                                  dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,                                                                                                                                                                     netbios-name-servers, netbios-scope,                                                                                                                                                                                                         rfc3442-classless-static-routes, ntp-servers;                                                                                                                                                                                                                                                                                                                                                                                                                                     #send dhcp-client-identifier 1:0:a0:24:ab:fb:9c;                                                                                                                                                                                             #send dhcp-lease-time 3600;                                                                                                                                                                                                                  #supersede domain-name "fugue.com home.vix.com";                                                                                                                                                                                             #prepend domain-name-servers 127.0.0.1;                                                                                                                                                                                                      #require subnet-mask, domain-name-servers;                                                                                                                                                                                                   timeout 300;                                                                                                                                                                                                                                 #retry 60;                                                                                                                                                                                                                                   #reboot 10;                                                                                                                                                                                                                                  #select-timeout 5;                                                                                                                                                                                                                           #initial-interval 2;                                                                                                                                                                                                                         #script "/sbin/dhclient-script";                                                                                                                                                                                                             #media "-link0 -link1 -link2", "link0 link1";                                                                                                                                                                                                #reject 192.33.137.209;                                                                                                                                                                                                                                                                                                                                                                                                                                                                   #alias {                                                                                                                                                                                                                                     #  interface "eth0";                                                                                                                                                                                                                         #  fixed-address 192.5.5.213;                                                                                                                                                                                                                #  option subnet-mask 255.255.255.255;                                                                                                                                                                                                       #}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        #lease {                                                                                                                                                                                                                                     #  interface "eth0";                                                                                                                                                                                                                         #  fixed-address 192.33.137.200;                                                                                                                                                                                                             #  medium "link0 link1";                                                                                                                                                                                                                     #  option host-name "andare.swiftmedia.com";                                                                                                                                                                                                 #  option subnet-mask 255.255.255.0;                                                                                                                                                                                                         #  option broadcast-address 192.33.137.255;                                                                                                                                                                                                  #  option routers 192.33.137.250;                                                                                                                                                                                                            #  option domain-name-servers 127.0.0.1;                                                                                                                                                                                                     #  renew 2 2000/1/12 00:00:01;                                                                                                                                                                                                               #  rebind 2 2000/1/12 00:00:01;                                                                                                                                                                                                              #  expire 2 2000/1/12 00:00:01;                                                                                                                                                                                                              #}       
Run Code Online (Sandbox Code Playgroud)

我也尝试过弄乱 netplan,但我不认为我的系统使用它。

还尝试了 /etc/network/interfaces

mtu 1340
Run Code Online (Sandbox Code Playgroud)

net*_*bat 6

您如何验证您的真实 MTU?

我想你的 Ubuntu 使用 Netplan 管理器。你问的是 CLI,而不是 GUI。这是我的建议。

MTU验证

通过带有“不分段”选项和指定数据包大小的数据包来 Ping 您的默认 GW 或 LAN 中另一个最近的活动节点。更改数据包大小并找到对等节点响应的限制大小:

ping 192.168.1.1 -c 2 -M do -s 2000
ping 192.168.1.1 -c 2 -M do -s 1500
ping 192.168.1.1 -c 2 -M do -s 1400
ping 192.168.1.1 -c 2 -M do -s 1472
ping 192.168.1.1 -c 2 -M do -s 1473
Run Code Online (Sandbox Code Playgroud)

如果数据包大小大于 MTU,则 ping 响应如下:

ping: local error: message too long, mtu=1500
Run Code Online (Sandbox Code Playgroud)

请注意,您在 ping 命令(-s 选项)中使用的数据包大小必须为 MTU 减去 28 字节(IP 标头和 ICMP 标头长度),即 1500-28=1472。

如果未超过 MTU,则响应为例如

2 packets transmitted, 2 received, 0% packet loss, time 1014ms
Run Code Online (Sandbox Code Playgroud)

环境

我假设你的接口名称是 eth1。

cd /etc/netplan
ls -l
sudo cp 01-network-manager-all.yaml  02-eth1-mtu.yaml
sudo vi 02-eth1-mtu.yaml
Run Code Online (Sandbox Code Playgroud)

新文件02-eth1-mtu.yaml的内容示例:

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    eth1:
      dhcp4: true
      mtu: 1000
Run Code Online (Sandbox Code Playgroud)

新设置将通过命令应用netplan try。如果在执行命令时按 Enter 键,则等待时间将小于 120 秒。

sudo netplan try
Run Code Online (Sandbox Code Playgroud)

现在是时候通过上述方法验证新的 MTU 大小了。

禁用新设置而不删除

sudo mv /etc/netplan/02-eth1-mtu.yaml /etc/netplan/02-eth1-mtu.yaml.bak
sudo netplan try
Run Code Online (Sandbox Code Playgroud)