avatar

在Ubuntu 18.04上设置MTU

用SSH连接服务器经常无缘无故断掉,SSH/SS连接表现出非常不稳定。但是用别的方法测试(ping或者访问服务器网页)速度都非常快,也没有丢包。
一番搜索,发现是MTU值为1500设置太高了,表现为SSH客户端在认证时会卡在debug1: SSH2_MSG_KEXINIT sent
下面分别给出在Ubuntu 18.04和Android下设置MTU的方法。

Ubuntu下的设置方式

这是没有桌面环境的Ubuntu的设置方法,如果有桌面环境,那么直接进入network-manager中修改就完事了。

angelscript
1
vi /etc/netplan/99-netcfg.yaml

以我的文件为例

yaml
1
2
3
4
5
6
7
8
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
mtu: 1400
dhcp6: no

重启后永久生效

Android下的设置方式

首先需要知道移动数据走的是哪一个网卡

ebnf
1
ifconfig

以我的手机为例

angelscript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
lo        Link encap:Local Loopback  
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope: Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:20080 errors:0 dropped:0 overruns:0 frame:0
TX packets:20080 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:45435532 TX bytes:45435532

dummy0 Link encap:Ethernet HWaddr 66:e6:63:4b:93:c8
inet6 addr: fe80::64e6:63ff:fe4b:93c8/64 Scope: Link
UP BROADCAST RUNNING NOARP MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:18 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 TX bytes:1260

wlan0 Link encap:Ethernet HWaddr 26:1e:1b:ed:72:f0 Driver icnss
inet addr:192.168.2.146 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::241e:1bff:feed:72f0/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:513977 errors:0 dropped:0 overruns:0 frame:0
TX packets:479613 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3000
RX bytes:367769689 TX bytes:87048137

rmnet_data0 Link encap:UNSPEC
inet6 addr: fe80::fa03:7b88:43a:a1e/64 Scope: Link
UP RUNNING MTU:2000 Metric:1
RX packets:34 errors:0 dropped:0 overruns:0 frame:0
TX packets:17 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:17154 TX bytes:1200

rmnet_ipa0 Link encap:UNSPEC
UP RUNNING MTU:2000 Metric:1
RX packets:116428 errors:0 dropped:0 overruns:0 frame:0
TX packets:110877 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:178292211 TX bytes:15220756

rmnet_data1 Link encap:UNSPEC
inet addr:172.24.3.137 Mask:255.255.255.252
UP RUNNING MTU:1500 Metric:1
RX packets:175319 errors:0 dropped:0 overruns:0 frame:0
TX packets:110860 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:176170341 TX bytes:15219556

可以看到rmnet_data0是没有连接网络的,移动数据在rmnet_data1上,下面修改MTU

routeros
1
2
su
ip link set dev rmnet_data1 mtu 1400

或者

angelscript
1
2
su
echo 1400 > /sys/class/net/rmnet_data1/mtu

立马生效,不过重启后恢复默认值。
嫌麻烦可以修改init.rc,添加一行,永久生效

angelscript
1
write /sys/class/net/rmnet_data1/mtu 1400

后记

后来发现服务端上也有报错error: Could not load host key: /etc/ssh/ssh_host_ed25519_key

awk
1
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

重新生成密钥之后,多用一段时间再看疗效。

Author: Yangbo Wei
Link: http://weiyangbo.gitee.io/2019/11/14/MTU-reset/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.