Skip to content

Using Network Interfaces

By default, the DHCP server (typically located on your router) will provide you with an IP address and you need not worry about setting it manually. It also provides other information such as your subnet, default route, and DNS server.

Usually your router will have two Network Interface Cards (NICs). One NIC points outwards towards your Internet Service Provider (ISP) and is configured with a public facing IP provided by your ISP's DHCP server (existing "somewhere" on their network). The other NIC points inwards towards your home network. On a home network the DHCP server is located in your router and distributes IPs to all the devices within your home.

Bring an interface up/down

sh
ip link set dev eth0 up
ip link set dev eth0 down
ip link set dev eth0 up
ip link set dev eth0 down

Network Manager CLI

A great way to manage network connections is with the nmcli tool (see nmcli help). Alternatively, you can use nmtui, the terminal UI for managing network interfaces.

Set a connection to get its IP from a DHCP server (need root privileges)

sh
$ nmcli dev[ice]
DEVICE       TYPE      STATE          CONNECTION # <-- note these connections  
wlp9s0       wifi      connected      Home wifi
enp11s0      ethernet  connected      Wired connection 1

$ nmcli con[nection] edit "Wired connection 1"

===| nmcli interactive connection editor |===

nmcli> print ipv4 # <-- view ipv4 settings; use 'print all' to see everything

ipv4.method:                            manual # <-- note this is 'manual'
ipv4.dns:                               --
ipv4.dns-search:                        --
ipv4.dns-options:                       --
ipv4.dns-priority:                      0
ipv4.addresses:                         192.168.1.98/24
ipv4.gateway:                           --
ipv4.routes:                            --
...
ipv4.dad-timeout:                       -1 (default)

nmcli> set ipv4.method auto
Do you also want to clear 'ipv4.addresses'? [yes]: yes

nmcli> print ipv4

ipv4.method:                            auto # <-- now this is 'auto'
ipv4.dns:                               --
ipv4.dns-search:                        --
ipv4.dns-options:                       --
ipv4.dns-priority:                      0
ipv4.addresses:                         --
ipv4.gateway:                           --
...
ipv4.dad-timeout:                       -1 (default)

nmcli> save
Connection 'Wired connection 1' (4g4..2c) successfully updated.

nmcli> quit
$ nmcli dev reapply enp11s0
Connection successfully reapplied to device 'enp11s0'.

$ ip -br a
lo        UNKNOWN   127.0.0.1/8 ::1/128 
enp11s0   UP        10.0.1.112/24 fe80::793b:5cce:bf23:69bd/64 # <-- uses DHCP
$ nmcli dev[ice]
DEVICE       TYPE      STATE          CONNECTION # <-- note these connections  
wlp9s0       wifi      connected      Home wifi
enp11s0      ethernet  connected      Wired connection 1

$ nmcli con[nection] edit "Wired connection 1"

===| nmcli interactive connection editor |===

nmcli> print ipv4 # <-- view ipv4 settings; use 'print all' to see everything

ipv4.method:                            manual # <-- note this is 'manual'
ipv4.dns:                               --
ipv4.dns-search:                        --
ipv4.dns-options:                       --
ipv4.dns-priority:                      0
ipv4.addresses:                         192.168.1.98/24
ipv4.gateway:                           --
ipv4.routes:                            --
...
ipv4.dad-timeout:                       -1 (default)

nmcli> set ipv4.method auto
Do you also want to clear 'ipv4.addresses'? [yes]: yes

nmcli> print ipv4

ipv4.method:                            auto # <-- now this is 'auto'
ipv4.dns:                               --
ipv4.dns-search:                        --
ipv4.dns-options:                       --
ipv4.dns-priority:                      0
ipv4.addresses:                         --
ipv4.gateway:                           --
...
ipv4.dad-timeout:                       -1 (default)

nmcli> save
Connection 'Wired connection 1' (4g4..2c) successfully updated.

nmcli> quit
$ nmcli dev reapply enp11s0
Connection successfully reapplied to device 'enp11s0'.

$ ip -br a
lo        UNKNOWN   127.0.0.1/8 ::1/128 
enp11s0   UP        10.0.1.112/24 fe80::793b:5cce:bf23:69bd/64 # <-- uses DHCP

Set a connection to use a static IP

sh
$ nmcli dev[ice]
DEVICE       TYPE      STATE          CONNECTION # <-- note these connections  
wlp9s0       wifi      connected      Home wifi
enp11s0      ethernet  connected      Wired connection 1

$ nmcli con[nection] edit "Wired connection 1"

===| nmcli interactive connection editor |===

nmcli> print ipv4 # <-- view ipv4 settings; use 'print all' to see everything

ipv4.method:                            auto # <-- note this is 'auto'
ipv4.dns:                               --
ipv4.dns-search:                        --
ipv4.dns-options:                       --
ipv4.dns-priority:                      0
ipv4.addresses:                         --
ipv4.gateway:                           --
ipv4.routes:                            --

nmcli> set ipv4.method manual
nmcli> set ipv4.addresses 192.168.1.98/24
nmcli> save
nmcli> quit
$ nmcli dev reapply enp11s0
Connection successfully reapplied to device 'enp11s0'.

$ ip -br a
lo        UNKNOWN   127.0.0.1/8 ::1/128 
enp11s0   UP        192.168.1.98/24 fe80::793b:5cce:bf23:69bd/64 # <-- static IP
$ nmcli dev[ice]
DEVICE       TYPE      STATE          CONNECTION # <-- note these connections  
wlp9s0       wifi      connected      Home wifi
enp11s0      ethernet  connected      Wired connection 1

$ nmcli con[nection] edit "Wired connection 1"

===| nmcli interactive connection editor |===

nmcli> print ipv4 # <-- view ipv4 settings; use 'print all' to see everything

ipv4.method:                            auto # <-- note this is 'auto'
ipv4.dns:                               --
ipv4.dns-search:                        --
ipv4.dns-options:                       --
ipv4.dns-priority:                      0
ipv4.addresses:                         --
ipv4.gateway:                           --
ipv4.routes:                            --

nmcli> set ipv4.method manual
nmcli> set ipv4.addresses 192.168.1.98/24
nmcli> save
nmcli> quit
$ nmcli dev reapply enp11s0
Connection successfully reapplied to device 'enp11s0'.

$ ip -br a
lo        UNKNOWN   127.0.0.1/8 ::1/128 
enp11s0   UP        192.168.1.98/24 fe80::793b:5cce:bf23:69bd/64 # <-- static IP

Setting static IP on a headless (Linux) machine can also be done by directly adjusting files in system's root.

  • Network interface configuration files are located at /etc/network/interfaces and within the /etc/network/interfaces.d directory.

Backup your interfaces file before changing anything in case of error.

When creating a static IP ensure that you set the address, netmask, gateway

diff
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

auto enp5s0
- iface enp5s0 inet dhcp
+ iface enp5s0 inet static
+   address 10.0.6.9
+   netmask 255.255.0.0
+   gateway 10.0.6.1
+   dns-domain your.computer
+   dns-namesevers 10.0.6.1
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

auto enp5s0
- iface enp5s0 inet dhcp
+ iface enp5s0 inet static
+   address 10.0.6.9
+   netmask 255.255.0.0
+   gateway 10.0.6.1
+   dns-domain your.computer
+   dns-namesevers 10.0.6.1

Then restart your network service with systemctl restart networking.service

Useful tools

dhclient: Request an IP from the DHCP server. Best best to use with a specific network interface, otherwise an attempt to reconfigure all interfaces will be made.

sh
dhclient eth0
dhclient eth0

ethtool

See your available network interfaces

sh
lshw -class network -short
lshw -class network -short

DNS

On modern Linux systems that use systemd, the Domain Name Service (DNS) resolver is likely systemd-resolved.

sh
systemctl status systemd-resolved
systemctl status systemd-resolved

If you've been messing with calls to dhclient and you find yourself with an error like: DNS_PROBE_FINISHED_BAD_CONFIG. Then simply restarting the DNS resolver may be your solution.

sh
systemctl restart systemd-resolved
systemctl restart systemd-resolved

You can set DNS servers with the resolvectl command

Bridging

Add a network bridge

ip link add br0 type bridge
ip link add br0 type bridge

Virtual Networks

ifconfig eth0:1 192.168.1.2 netmask 255.255.255.0 up
ifconfig eth0:1 192.168.1.2 netmask 255.255.255.0 up

Network Interface Prioritization

Sometimes you want to have multiple network interfaces on the same device, e.g., a cellular and WiFi interface. Interfaces such as these can be prioritized by setting their metric value.

View metric values for all your interfaces with route -n.

sh
$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    600    0        0 wlp9s0
0.0.0.0         172.30.0.1      0.0.0.0         UG    200    0        0 eth0
172.30.0.0      0.0.0.0         255.255.255.0   U     200    0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     600    0        0 wlp9s0
$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    600    0        0 wlp9s0
0.0.0.0         172.30.0.1      0.0.0.0         UG    200    0        0 eth0
172.30.0.0      0.0.0.0         255.255.255.0   U     200    0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     600    0        0 wlp9s0

The metric is like a resistance, so lower metrics will have priority over higher metrics. In the above example, the eth0 interface has priority. To priorize the wlp9s0 interface, change its metric to anything below 200 using the ifmetric tool.

sh
$ sudo apt install ifmetric
$ sudo ifmetric wlp9s0 199
$ sudo apt install ifmetric
$ sudo ifmetric wlp9s0 199

Alternatively, the nmcli tool can also be used.

nmcli connection modify <connection-name> ipv4.route-metric 99
nmcli connection modify <connection-name> ipv4.route-metric 99

interface bring up and down details (TEMPORARY)

#!/bin/bash

# $1 interface name
set_nic_info() {
  sudo ifconfig $1 169.254.55.1 netmask 255.255.0.0 broadcast 169.254.255.255 mtu 9000
}

sudo ifconfig eth0 down
sudo ifconfig eth0 mtu 9000
sudo ifconfig eth0 up

set_nic_info eth0
# NB: Below is commented since already present in conf. Uncomment if removed
#sudo sh -c "echo 'net.core.rmem_default=33554432' >> /etc/sysctl.conf"
#sudo sh -c "echo 'net.core.rmem_max= 33554432' >> /etc/sysctl.conf"
sudo sysctl -p
#!/bin/bash

# $1 interface name
set_nic_info() {
  sudo ifconfig $1 169.254.55.1 netmask 255.255.0.0 broadcast 169.254.255.255 mtu 9000
}

sudo ifconfig eth0 down
sudo ifconfig eth0 mtu 9000
sudo ifconfig eth0 up

set_nic_info eth0
# NB: Below is commented since already present in conf. Uncomment if removed
#sudo sh -c "echo 'net.core.rmem_default=33554432' >> /etc/sysctl.conf"
#sudo sh -c "echo 'net.core.rmem_max= 33554432' >> /etc/sysctl.conf"
sudo sysctl -p

Tips and Tricks

Here's a collection of haphazard tips and

Use Jumbo Frames

sh
# Option 1: ip
ip link set eth0 mtu 9000

# Option 2: ifconfig
sudo ifconfig eth0 down; sudo ifconfig eth0 mtu 9000; sudo ifconfig eth0 up
# Option 1: ip
ip link set eth0 mtu 9000

# Option 2: ifconfig
sudo ifconfig eth0 down; sudo ifconfig eth0 mtu 9000; sudo ifconfig eth0 up

If you receive Error: mtu greater than device maximum, then you can set the interface to use its maximum allowable MTU. Find this with ip -d link list and look for the maxmtu field.

Change the Network Kernel Settings

The following commands will set the default and maximum size of the socket receive buffer within the /etc/sysctl.conf file. In effect, this changes the kernel's settings for the allowable size of incoming network packets.

sh
sudo sh -c "echo 'net.core.rmem_default=33554432' >> /etc/sysctl.conf"
sudo sh -c "echo 'net.core.rmem_max= 33554432' >> /etc/sysctl.conf"
sudo sysctl -p
sudo sh -c "echo 'net.core.rmem_default=33554432' >> /etc/sysctl.conf"
sudo sh -c "echo 'net.core.rmem_max= 33554432' >> /etc/sysctl.conf"
sudo sysctl -p

Similar setting can be applied to the send buffer with wmem_default and wmem_max.

ADDITIONAL RESOURCES