Ubuntu 18 and Slow Network

Recently my Ubuntu 18 laptop had intermittent very slow internet/network. After checking the usual causes (router, WiFi, etc.), I found this helpful article. Turns out what fixed it was one of those suggestions.

Edit this file:

sudo vi /etc/nsswitch.conf

Then change the line that says:

hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns

To this:

hosts:          files dns mdns4_minimal [NOTFOUND=return] mdns

So, in hindsight, it seems to have been a DNS problem.

Update 1

While this seemed to fix the problem temporarily, network problems started again. After an hour or two of reading about the issue, I made another change. I learned that Ubuntu 18 changed the way network is configured, using a new service called “netplan”. The /etc/netplan directory should have config files, but mine was empty! I don’t know how it got emptied; I certainly didn’t do it. But I needed to create a default config file. I created a file called 01-netcfg.yaml:

network:
  version: 2
  renderer: networkd
#  ethernets:
#    wlan0:
#      dhcp4: true
#      nameservers:
#        addresses: [192.168.1.1]

After some experimentation I commented out the last few lines. This tells Ubuntu that the network manager service will control network. Then I configured it in the network WiFi GUI, enabling DHCP wasn’t enough; I also added my home router (192.168.1.1) to the DNS list.

Networking (more specifically, DNS resolution) was still slow and occasionally intermittent. Finally, I had to flush the DNS cache:

sudo systemd-resolve --flush-caches

Now, everything seems to be working again.

Update 2

This too only fixed things temporarily. I still was getting intermittent network problems – but only on WiFi, not when wired. It looks like Ubuntu 18’s new “netplan” was conflicting with the “networking” service. I configured netplan and stopped the networking service. That is:

In the /etc/netplan directory, I had this file named 10-netcfg.yaml, which tells the system that the desktop Network Manager app will control network setup:

network:
version: 2
renderer: NetworkManager

I created additional files AFTER the ones that are there, so they override this. These files look like this:

20-wlan0.yaml

# Enable this only if you don't want to use the desktop GUI
network:
  ethernets:
    wlan0:
      addresses: []
      dhcp4: true
      optional: true
  version: 2

30-eth0.yaml

# Enable this only if you don't want to use the desktop GUI
network:
  ethernets:
    wlan0:
      addresses: []
      dhcp4: true
      optional: true
  version: 2

Because they sort after the first file, they override it. Next, run this command:

mclement@clements6:~$ sudo netplan --debug generate
DEBUG:command generate: running ['/lib/netplan/generate']
** (generate:27016): DEBUG: 09:12:23.854: Processing input file /etc/netplan/10-netcfg.yaml..
** (generate:27016): DEBUG: 09:12:23.854: starting new processing pass
** (generate:27016): DEBUG: 09:12:23.854: Processing input file /etc/netplan/20-wlan0.yaml..
** (generate:27016): DEBUG: 09:12:23.854: starting new processing pass
** (generate:27016): DEBUG: 09:12:23.854: Processing input file /etc/netplan/30-eth0.yaml..
** (generate:27016): DEBUG: 09:12:23.854: starting new processing pass
** (generate:27016): DEBUG: 09:12:23.854: wlan0: setting default backend to 2
** (generate:27016): DEBUG: 09:12:23.854: Configuration is valid
** (generate:27016): DEBUG: 09:12:23.854: eth0: setting default backend to 2
** (generate:27016): DEBUG: 09:12:23.854: Configuration is valid
** (generate:27016): DEBUG: 09:12:23.855: Generating output files..
** (generate:27016): DEBUG: 09:12:23.855: networkd: definition wlan0 is not for us (backend 2)
** (generate:27016): DEBUG: 09:12:23.855: networkd: definition eth0 is not for us (backend 2)

Note the last 2 lines, which is “networkd” saying it won’t be managing these network connections.

Next, apply this configuration

mclement@clements6:~$ sudo netplan apply

Now, disable the system networking service

sudo service networking stop

At this point, my WiFi networking started working again, and was not slow anymore.

Update 3

Sigh… this again was only a temporary fix. Even after all of the above, network was still slow! Then I remembered that I was using 5 GHz WiFi, which has different frequencies/channels in different regions, so requires the device to know what country it is in. So I changed one more thing. Edit the file /etc/default/crda and set my country code. That is, the file originally looked like this:

# Set REGDOMAIN to a ISO/IEC 3166-1 alpha2 country code so that iw(8) may set
# the initial regulatory domain setting for IEEE 802.11 devices which operate
# on this system.
#
# Governments assert the right to regulate usage of radio spectrum within
# their respective territories so make sure you select a ISO/IEC 3166-1 alpha2
# country code suitable for your location or you may infringe on local
# legislature. See `/usr/share/zoneinfo/zone.tab' for a table of timezone
# descriptions containing ISO/IEC 3166-1 alpha2 country codes.

REGDOMAIN=

Note that REGDOMAIN was not set. I changed that last line to this:

REGDOMAIN=US

Since “US” is the code for my country.

What is interesting, is that I tested and 2.4 GHz WiFi worked fine all along. It was only 5 GHz WiFi that was intermittently broken. This would be consistent with not having the region set.

Given all this, I reverted the netplan changes above, so the desktop NetworkManager controls my networking.

Update 4

Again, this still didn’t fix the problem. However, the problem may have been the channel I was using on 5 GHz. This thread was helpful. Channel 149 was listed by “iw list”, but not by “iwlist chan”. I changed the router to use channel 48, which appears in both lists, and it is now working.

Conclusion

There’s a lot here, some of which wasn’t necessary. In summary, the fix was:

Configure /etc/netplan to tell the desktop Network Manager GUI to manage network connections. This is the default Ubuntu desktop setup.

Set /etc/default/crda to set the system country code (needed for 5 GHz).

Run iw list and iwlist chan to see which 5 GHz channels the WiFi card supports.

Configure my router to use one of these 5 GHz channels.

BANG! It’s still working overnight, fast and reliable. Problem solved.