Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Network Interfaces File Cheatsheet

This is just a cheatsheet for the /etc/network/interfaces file that I can quickly refer to when I forget things like how to specify the mac address against an interface etc.

Related Posts

Cheats

Basic DHCP Example

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug ens18
iface ens18 inet dhcp

It is only the last two lines that are really for the setting of DHCP on an interface, but it is sometimes useful to have a full file example in case "stuff happens".

Basic Static IP Example

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

allow-hotplug ens19
iface ens19 inet static
        address 192.168.0.2/24
        gateway 192.168.0.1
        dns-nameserver 8.8.8.8
        dns-nameserver 8.8.4.4
        dns-search programster.org

The dns-search is the domain to append to a hostname by default if only the hostname is provided. E.g if given a name kube1 in tools like ping, then it would use kube1.programster.org since search is set to programster.org.

Matching MAC Address

When you have two network interfaces attached to a server, it is usually important to specify the mac address against the name of the interface. That way you can be sure you are applying the settings to the physical hardware you intend. E.g. one of the network pipes might need to be configured to use DHCP, whilst the other might need a static IP. It would be bad if the server booted up with these configurations assigned the wrong way around.

This is easily achieved by adding hwaddress ether XX:XX:XX:XX:XX:XX to the bottom of your configuration block like so:

allow-hotplug enp0s19
iface enp0s19 inet static
        address 192.168.0.2
        netmask 255.255.255.0
        hwaddress ether BC:24:11:A9:6E:EB

Names And Alt Names

When you look at the output of ip addr, you may see two names for an interface. In the example below, it has the name ens19 and enp0s19.

3: ens19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether bc:24:11:a9:6e:eb brd ff:ff:ff:ff:ff:ff
    altname enp0s19
    inet 192.168.0.2/24 brd 192.168.0.255 scope global enp0s19
       valid_lft forever preferred_lft forever
    inet6 fe80::be24:11ff:fea9:6eeb/64 scope link 
       valid_lft forever preferred_lft forever

Whichever name you use for configuring the interface in the */etc/network/interfacesfile, that is the name you will need to use when performing operations likeifupandifdown`.

Last updated: 14th January 2025
First published: 14th January 2025

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch