Post

Setting up a Static IP on Linux

For this tutorial, all you need is Super User access to your Linux machine and a text editor.

Step 1 - Find your network interface

Access your server and start by finding out the name of your NIC (Network Interface Card).

1
ip a

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 22:fd:0f:5d:5e:f4 brd ff:ff:ff:ff:ff:ff
    altname enp0s18
    inet 172.16.255.253/24 brd inet 172.16.255.255 scope global dynamic ens18
       valid_lft 229738sec preferred_lft 229738sec
    inet6 fe80::20fd:fff:fe5d:5ef4/64 scope link
       valid_lft forever preferred_lft forever

In this case, the output shows us two interfaces:

lo: Stands for loopback is the interface used for communication within the device itself. Typically, this interface will have assigned the IP 127.0.0.1 and usually gets represented by the hostname localhost.

ens18: This is the interface we are looking for. en indicates that it is an Ethernet connection.

Note: The name of the interface connection can be different from machine to machine. Other common examples are: eth0, enp0s3m, ens33, eno1 and for wireless you can find something like wlx74da38f468d2 or wlan0.

Step 2 - Find which service you are using.

Debian-based systems mainly have two configurations. Ifupdown and Netplan.io.

Check for ifupdown:

If your system is using ifupdown, which is more common on older systems, the following command should return enabled.

1
sudo systemctl is-enabled networking.service

Check for Netplan:

Netplan is the new standard for the recent versions of Ubuntu and some other Debian-based distributions. The following command should return enabled.

1
sudo systemctl is-enabled systemd-networkd.service

Step 3 - Edit the Network Configuration

The configuration is different for each service.

Ifupdown:

Here, you will need the name we saw earlier ens18. In this example, you must edit the file /etc/network/interfaces as root.

Initially, the content of the file should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

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

We have to edit the file to look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto ens18
iface ens18 inet static
address 172.16.255.4
netmask 255.255.255.0
gateway 172.16.255.1
dns-nameservers 1.1.1.1,9.9.9.9

The last thing is to restart the service:

1
sudo service networking restart

Well done! If you run the command ip a, again you should see your new IP, in this case, inet 172.16.255.4/24.

Netplan

For Netplan, the configuration is written in YAML. Edit the file present in /etc/netplan/. This file has an arbitrary name, typically something like XX-cloud-init.yaml.

The content of the file should look something like this:

1
2
3
4
5
6
# This is the network config written by 'console-conf'
network:
  ethernets:
    ens18:
      dhcp4: true
  version: 2

To setup the static IP modify the file to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
network:
  version: 2
  ethernets:
    ens18:
      addresses:
        - 172.16.255.4/24
      routes:
        - to: default
          via: 172.16.255.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 9.9.9.9

When you finish editing the file, you can use the command netplan generate to compile the file. netplan apply to aply the new configuration.

Well done! If you run the command ip a, again you should see your new IP, in this case, inet 172.16.255.4/24.

Conclusion

Congratulations! You’ve learned how to configure a static IP address on your Debian-based Linux system using two popular methods: Netplan and ifupdown. Whether you prefer the simplicity of Netplan's YAML configuration or the traditional approach of ifupdown, you now have the knowledge and confidence to tailor your network settings according to your needs.

By mastering these techniques, you’ve empowered yourself to take control of your system’s network configuration. Whether setting up a home server, deploying network services, or ensuring a consistent connection, having a static IP address provides stability and predictability to your network environment.

Remember, technology is all about flexibility. Now, you have the tools to adapt your Linux system’s networking to your specific requirements. Keep exploring, learning, and experimenting - you’re on your way to becoming a Linux networking expert!

Happy networking! 🚀🌐

This post is licensed under CC BY 4.0 by the author.