使用Ubuntu18.4做为工作环境有段时间,外网用有线,内网使用wifi连接.,当二个网络同时使用时,不能正常上网.
如下为网卡信息:
root@ubuntu:~# ifconfig
(有线-外网)
enx000ec6df4436: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.58.59 netmask 255.255.0.0 broadcast 192.168.255.255
inet6 fe80::20e:c6ff:fedf:4436 prefixlen 64 scopeid 0x20<link>
ether 00:0e:c6:df:44:36 txqueuelen 1000 (以太网)
RX packets 8624 bytes 3192080 (3.1 MB)
RX errors 0 dropped 268 overruns 0 frame 0
TX packets 1288 bytes 225384 (225.3 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
(wifi-内网)
wlp4s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.53.107 netmask 255.255.255.0 broadcast 192.168.53.255
inet6 fe80::98e:a056:5ef2:af6a prefixlen 64 scopeid 0x20<link>
ether 14:ab:c5:11:a0:1c txqueuelen 1000 (以太网)
RX packets 242 bytes 13637 (13.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 657 bytes 73728 (73.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
网络检查,发现到外网网关及外网DNS都是正常的.这说明网络是正常的,应该是DNS域名解析的问题.
ping百度域名不通
root@ubuntu:~# ping baidu.com
ping: baidu.com: 未知的名称或服务
ping外网网关正常
root@ubuntu:~# ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.574 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.566 ms
ping外网DNS正常
root@ubuntu:~# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=108 time=255 ms
ping百度的IP正常
root@ubuntu:~# ping 39.156.69.79
PING 39.156.69.79 (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79: icmp_seq=1 ttl=49 time=39.6 ms
检查DNS,发现wlp4s0(wifi-内网),由于是动态获取的上面也有一个DNS
root@ubuntu:~# systemd-resolve --status
Link 4 (enx000ec6df4436)
Current Scopes: DNS
LLMNR setting: yes
MulticastDNS setting: no
DNSSEC setting: no
DNSSEC supported: no
DNS Servers: 223.5.5.5
8.8.8.8
192.168.1.1
Link 3 (wlp4s0)
Current Scopes: DNS
LLMNR setting: yes
MulticastDNS setting: no
DNSSEC setting: no
DNSSEC supported: no
DNS Servers: 192.168.53.14
DNS Domain: ~.
检查/etc/resolv.conf文件,参考https://my.oschina.net/u/2306127/blog/1930116,https://oldtang.com/6118.html可以得知由系统服务systemd-resolved
root@ubuntu:~# cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 127.0.0.53
options edns0
检查systemd-resolved服务状态,可以得知当前使用的DNS服务器是192.168.53.14也就是wifi的DNS,而wifi是内网.
注:可以使用这个命令查询当前的DNS,Using degraded feature set (TCP) for DNS server 192.168.53.14.中最后一条为当前的DNS.
root@ubuntu:~# systemctl status systemd-resolved
● systemd-resolved.service - Network Name Resolution
Loaded: loaded (/lib/systemd/system/systemd-resolved.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-03-13 11:05:22 CST; 3h 2min ago
Docs: man:systemd-resolved.service(8)
https://www.freedesktop.org/wiki/Software/systemd/resolved
https://www.freedesktop.org/wiki/Software/systemd/writing-network-configuration-managers
https://www.freedesktop.org/wiki/Software/systemd/writing-resolver-clients
Main PID: 870 (systemd-resolve)
Status: "Processing requests..."
Tasks: 1 (limit: 4915)
CGroup: /system.slice/systemd-resolved.service
└─870 /lib/systemd/systemd-resolved
3月 13 13:55:23 ubuntu systemd-resolved[870]: Using degraded feature set (TCP) for DNS server 192.168.53.14.
3月 13 13:56:51 ubuntu systemd-resolved[870]: Using degraded feature set (UDP) for DNS server 192.168.53.14.
3月 13 13:58:59 ubuntu systemd-resolved[870]: Using degraded feature set (TCP) for DNS server 192.168.53.14.
3月 13 13:59:20 ubuntu systemd-resolved[870]: Using degraded feature set (UDP) for DNS server 192.168.53.14.
3月 13 14:00:23 ubuntu systemd-resolved[870]: Using degraded feature set (TCP) for DNS server 192.168.53.14.
在使用多种方法设置DNS,例如修改/etc/systemd/resolved.conf文件之类的都方法,最终系统还是选择了wifi的DNS,既然wifi的DNS是动态获取到的.哪修改wifi的IP为手动的应该就能解决这个问题.

设置完成后,关闭一下wifi,重新连接wifi.
再次检查,systemd-resolved状态后,可以见到最后生效的DNS是外网IP.也能正常ping百度.
root@ubuntu:~# systemctl status systemd-resolved
● systemd-resolved.service - Network Name Resolution
Loaded: loaded (/lib/systemd/system/systemd-resolved.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-03-13 14:53:24 CST; 21min ago
Docs: man:systemd-resolved.service(8)
https://www.freedesktop.org/wiki/Software/systemd/resolved
https://www.freedesktop.org/wiki/Software/systemd/writing-network-configuration-managers
https://www.freedesktop.org/wiki/Software/systemd/writing-resolver-clients
Main PID: 910 (systemd-resolve)
Status: "Processing requests..."
Tasks: 1 (limit: 4915)
CGroup: /system.slice/systemd-resolved.service
└─910 /lib/systemd/systemd-resolved
3月 13 15:13:25 ubuntu systemd-resolved[910]: Using degraded feature set (TCP) for DNS server 192.168.53.14.
3月 13 15:13:45 ubuntu systemd-resolved[910]: Using degraded feature set (UDP) for DNS server 192.168.53.14.
3月 13 15:13:46 ubuntu systemd-resolved[910]: Using degraded feature set (TCP) for DNS server 192.168.53.14.
3月 13 15:14:06 ubuntu systemd-resolved[910]: Using degraded feature set (UDP) for DNS server 192.168.53.14.
3月 13 15:14:11 ubuntu systemd-resolved[910]: Using degraded feature set (TCP) for DNS server 192.168.53.14.
3月 13 15:14:42 ubuntu systemd-resolved[910]: Using degraded feature set (UDP) for DNS server 192.168.53.14.
3月 13 15:14:46 ubuntu systemd-resolved[910]: Grace period over, resuming full feature set (UDP+EDNS0) for DNS server 223.5.5.5.
3月 13 15:14:46 ubuntu systemd-resolved[910]: Using degraded feature set (UDP) for DNS server 223.5.5.5.
3月 13 15:14:46 ubuntu systemd-resolved[910]: Using degraded feature set (UDP) for DNS server 223.5.5.5.
3月 13 15:14:55 ubuntu systemd-resolved[910]: Using degraded feature set (UDP) for DNS server 223.5.5.5.
root@ubuntu:~# ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=53 time=39.4 ms
从图形界面修改网络设置的文件位置,这个目录应该是使用NetworkManager管理网络的网络配置文件目录.
root@ubuntu:~# grep -sr 192.168.53.59 /etc/
/etc/NetworkManager/system-connections/TP-LINK_XXX:address1=192.168.53.59/24,192.168.53.14
root@ubuntu:~# ls /etc/NetworkManager/system-connections/
HUAWEI_STK_bc20 P300_AP TP-LINK_XXX