Ubuntu 18.04 DNS配置解决域名无法访问或者访问很慢的问题

之前在公司自己弄了个OP的路由器,之前一直都跑的好好的(经常在公司内网和自己的OP之间切换),但是这周发现内网网口的时候上网无问题,但是用自己OP的时候无法访问外网,ping的通IP,但是域名无法解析,网站也无法打开,确定是DNS配置出了问题,花了点时间查了下资料,修复了此问题,省的重新装机了,下面记录下踩到的坑,以及解决的方法.

踩到的坑

其实整体思路就是配置好DNS服务器就可以了,找到相关配置文件修改下其实就行,但是下面这几种方式,我试过了,不行.

修改/etc/resolv.conf配置,但是一旦重启,就恢复默认值了。有个博主推荐安装下resolvconf,然后修改/etc/resolvconf/resolv.conf.d/ 目录下head文件(head就是在/etc/resolv.conf头部添加配置,tail就是在尾部)

sudo apt install resolvconf
sudo vim /etc/resolvconf/resolv.conf.d/head

在head中添加一行,

nameserver 114.114.114.114

注意,一定要在最后回车换行.重启后,可以看到/etc/resolv.conf里面配置变成了

nameserver 114.114.114.114
nameserver 127.0.0.53

其实可以关闭本地DNS的,如下操作即可,

sudo systemctl disable systemd-resolved.service
sudo systemctl stop systemd-resolved

重启后,发现/etc/resolv.conf里面只剩下114的DNS配置了.
但是这个方式并没有解决我切换到自己的OP时无法联网的问题,并且这么操作后,公司内网也打不开了,这种方式失败.

另一种方式,其实这这个类似,修改/etc/systemd/resolved.conf配置文件,修改成

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
DNS=8.8.8.8
FallbackDNS=1.1.1.1
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

如此修改后,/etc/resolv.conf重启后会默认设置DNS服务器为DNS=8.8.8.8,思路是和第一种方式一致的,但是还是没用.

还有修改/etc/NetworkManager下面的配置文件的,但是依然没用.

解决方法

修改/etc/netplan/01-network-manager-all.yaml配置文件,具体操作方法如下:

先看下电脑的网卡配置信息,

ifconfig

拿我的电脑举栗子,显示如下,

docker0: flags=4099  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:5a:e8:db:2b  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno1: flags=4163  mtu 1500
        inet 172.16.15.69  netmask 255.255.255.0  broadcast 172.16.15.255
        inet6 fe80::7f3e:10c5:5cbb:fdee  prefixlen 64  scopeid 0x20
        ether 1c:a0:b8:77:a1:3c  txqueuelen 1000  (Ethernet)
        RX packets 35475  bytes 15108483 (15.1 MB)
        RX errors 0  dropped 16447  overruns 0  frame 0
        TX packets 7396  bytes 1520156 (1.5 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 23  memory 0xa3300000-a3320000  

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 9045  bytes 8238003 (8.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9045  bytes 8238003 (8.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlx502b73c9007e: flags=4163  mtu 1500
        inet 192.168.8.129  netmask 255.255.255.0  broadcast 192.168.8.255
        inet6 fe80::3c12:8ccf:3ad7:3978  prefixlen 64  scopeid 0x20
        ether 50:2b:73:c9:00:7e  txqueuelen 1000  (Ethernet)
        RX packets 14476  bytes 14118753 (14.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11756  bytes 2047466 (2.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

从中可以看出,我这边是两个网卡的,一个是eno1(本地有线连接),另一个是wlx502b73c9007e(自己加的无线网卡),记下来这两个网卡的名称和IP,下面用得上.

然后开始配置/etc/netplan/01-network-manager-all.yaml

sudo gedit /etc/netplan/01-network-manager-all.yaml

修改后,显示为

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  #renderer: networkd
  
  ethernets:
    wlx502b73c9007e :
      addresses: [192.168.8.129/24]
      dhcp4: yes
      optional: true
      gateway4: 192.168.8.1
      nameservers:
        addresses: [8.8.8.8,8.8.4.4,114.114.114.114]

然后执行下面命令:

sudo netplan apply
sudo systemctl start systemd-networkd.service

应用所做修改就可以了.

我这里只修改了,我自己添加的网卡配置,eno1网卡按照默认的配置运行,当然大家也可以按照自己的需求去修改DNS的服务器,我这里用的就是Google和114,大部分场景我这边都可以覆盖了.

好的,打完收工,最后查了下,好像从18.04开始,都是使用netplan配置网卡了,所以之前的方式通通失效.

你可能感兴趣的:(系统工具配置,ubuntu,服务器,网络)