Linux - Fix ethernet after suspend or hibernate

Description

There is a bug for the r8169 ethernet driver in linux where it will not work after suspending or hibernating the system. It fails to work even when networking services are restarted. More info here. Even though it is fixed in Ubuntu it seems that in Arch it is not in my case.

The driver being used can be checked with the command:

$ sudo lshw -C network
  *-network
       description: Ethernet interface
       product: RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
       vendor: Realtek Semiconductor Co., Ltd.
       physical id: 0
       bus info: pci@0000:05:00.0
       logical name: enp5s0
       version: 26
       serial:
       size: 1Gbit/s
       capacity: 1Gbit/s
       width: 64 bits
       clock: 33MHz
       capabilities: pm msi pciexpress msix bus_master cap_list ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autonegotiation
       configuration: autonegotiation=on broadcast=yes driver=r8169 driverversion=6.3.6-zen1-1-zen duplex=full firmware=rtl8168h-2_0.0.2 02/26/15 ip=192.168.0.126 latency=0 link=yes multicast=yes port=twisted pair speed=1Gbit/s
       resources: irq:36 ioport:f000(size=256) memory:fc604000-fc604fff memory:fc600000-fc603fff

On the configuration line there is driver=r8169, this will confirm that the problem is most likely this bug.

Fix

Create a file: /etc/systemd/system/fix-r8169.service

With the contents:

[Unit]
Description=Fix RTL-8169 Ethernet Driver on resume
After=suspend.target hibernate.target

[Service]
User=root
Type=oneshot
ExecStartPre=/sbin/modprobe -r r8169
ExecStart=/sbin/modprobe r8169
TimeoutSec=0
StandardOutput=journal

[Install]
WantedBy=suspend.target hibernate.target

And enable it: systemctl enable fix-r8169.service

This systemd service will unload and then reload the r8169 kernel driver.