How to set up a static IP on Debian 11 Linux?

This is a step-by-step guide on how to set up a static IP on Linux servers running Debian 11.

How to set up a static IP on Debian 11 Linux?
Photo by Lukas / Unsplash

Important Note

I am aware that Debian 11 is not a very recent distribution. Unfortunately, on some of my mini-laboratory equipment, such as the Pine A64, an alternative to Raspberry Pi, which is less recent, this is the only version of Linux I have managed to run stably. I make this observation because, in more modern distributions, this configuration can be done more easily and differently from what is shown in this tutorial.

To set up a static IP on Debian 11 or previous Linux distributions, we need to follow these steps:

  1. Log in to our remote Server. For this example, we will connect to the Matrix server with IP 192.168.1.45 through port 22 (default port):

    localhost:~$ ssh shadow@192.168.1.45
    
  2. Before modifying any configuration, we proceed to backup the file to be changed:‌‌‌‌

    shadow@matrix:~$ sudo cp /etc/network/interfaces ~/interfaces.20230406.bk
    # I like to use the date in the backup file name to know how old the backup is.
    
  3. We need to identify the network adapter we want to modify. For this, we can use the ip application.

    shadow@matrix:~$ ip -c link show
    

    The link option will help us identify the available network devices on the equipment. The -c option enables the terminal to return the results in color.

    From the list returned, we must identify the interface we want to modify to assign the IP. If we want more information about the interface, we can change the link option to the addr option. This will return the details of the IP protocol of the interface.

    shadow@matrix:~$ ip -c addr show eth0
    
  4. We proceed to modify the network file that manages the interfaces. We will use vim as our preferred editor:

    shadow@matrix:~$ vim /etc/network/interfaces
    
  5. The default file will show several lines of configuration of each of the network interfaces on the equipment. We will only update the data related to the interface we want to modify, in this case, eth0:

    ...
    # Let's find the line where our interface is located
    allow-hotplug eth0
    iface eth0 int dhcp
    

    We replace the previous two lines with the following configuration::

    auto eth0
    iface eth0 inet static
    	address 192.168.1.25   # IP address we want to assign
    	netmask 255.255.255.0  # 24-bit mask
    	gateway 192.168.1.1.   # IP address of the main router
    	dns-nameservers 192.168.1.1 1.1.1.1 8.8.8.8
    

    We save our changes. In vim, it would be: esc then :x.

  6. We proceed to restart the network services to reflect the changes.

    shadow@matrix:~$ sudo systemctl restart networking.service
    
  7. We proceed to validate that the IP has been assigned using the following command shown above:

    shadow@matrix:~$ ip -c addr show eth0
    

    If the result reflects the new IP, we have successfully completed our update.

Fun Facts

My Server does not have the reboot and shutdown applications.

Something that caught me a little off guard on this server was realizing that it did not have the reboot and shutdown applications to restart or power off my machine.

Well, it's interesting and important to know that we can achieve the same goal using systemctl. To restart the machine, we use the following command:

shadow@matrix:~$ systemctl reboot

If we only want to power off the server, we use the following:

shadow@matrix:~$ systemctl poweroff

I hope this tutorial is useful to you.

Happy Hacking!

External resources: