Configure static ip address on Debian/Ubuntu/Centos/Rocky Linux

Thu, May 26, 2022 5-minute read

Introduction

When installing linux usually the installation defaults to DHCP for networking, which is perfectly fine if you are running a desktop machine. But if you install on a server you expect to have consistent addressing in place so it is possible to set up a name server and point a hostname towards your servers ip-address.

This can be solved in several ways:

  • Static IP
  • DHCP with dynamic update of DNS Server
  • DHCP assigned address that is fixed to the MAC Address of the network card

This post will only be about setting up a static ip address, since that is the easiest solution and probably the solution that most people would want to use.

The how

To configure a static ip address on a machine you need to first find a vacant ip address in the ip range you want to use - this is to prevent conflicts on the network, which will result in disruption of traffic for the affected hosts. So make sure that nothing else is using the ip address you want to use. This is why many people like DHCP, since a DHCP server maintains control of what IP Address is in use where and makes sure no conflicts exists.

Files

Like everything else that can be configured in Linux - also the network is configured by editing some files that usually resides in /etc. There are many systems that can be used to configure networking in Linux - but usually a distribution have a default and alternative ways to configure has to be installed manually.

Guides

Below are simple guides for Rocky Linux, Centos, Debian and Ubuntu. The exact installation you are using might be using a slightly different way to configure the networking. To find a guide for your specific system, you first need to find out how the networking is configured - then you can change the configuration files for that particular process.

Rocky Linux 8/8.5, CentOS 8/8.5

Per default if you don’t install other packages Rocky and CentOS is using /etc/sysconfig/network-scripts/ifcfg-[nic]. So if your network card is named ens192, then the corresponding file would be named ifcfg-ens192.

A configuration file could like a lot like the one below:

TYPE=Ethernet
DEVICE=ens192
NAME=ens192
UUID=bb5e5b01-946d-493e-b595-1891bb0bab19
ONBOOT=yes
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
BOOTPROTO=dhcp

As you can see the BOOTPROTO is set to DHCP which is the “magic sauce” that just makes everything work - but also have the consequence that the ip address might change.

So to change to static address you need to change the contents of the file to be similar to:

TYPE=Ethernet
DEVICE=ens192
NAME=ens192
UUID=bb5e5b01-946d-493e-b595-1891bb0bab19
ONBOOT=yes
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
BOOTPROTO=none
IPADDR="192.168.0.4"
PREFIX="16"
GATEWAY="192.168.0.1"
DNS1="192.168.0.2"
DNS2="192.168.0.3"
DOMAIN="root.dom"

All the options until BOOTPROTO are the same - so to change from DHCP to a static ip address you simply change BOOTPROTO from DHCP to none - and then enter the options that is required yourself.

If all you care about is a static ip address - all you need to set is: IPADDR, PREFIX, GATEWAY. This will allow the machine to respond properly to packages from the network. But since no DNS has been configured - you cannot resolve ip-addresses. This is done by simply adding a couple of DNS Servers and a domain.

When all the required changes are done - you have several options to apply the configuration.

The easiest and most brute force - is to simply restart the machine.

Alternatives are:

  • sudo systemctl NetworkManager restart
  • sudo nmcli networking off && sudo nmcli networking on

Both should restart networking and apply the new settings.

Debian 11/12

Debians network settings is configured in /etc/network/interfaces

An example could look like:

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

To change to a static ip adrress - you change it so it looks like:

# The primary network interface
allow-hotplug ens192
iface ens192 inet static
    address 192.168.0.4
    netmask 255.255.0.0
    gateway 192.168.0.1
    dns-nameserver 192.168.0.2 
    dns-nameserver 192.168.0.3
    dns-search root.dom

As you can see the settings are similar to the CentOS just named slightly different and organized differently.

Restart networking and apply changes by doing a sudo /etc/init.d/networking restart - or restarting the machine.

Ubuntu 20+

Ubuntu is using yet another different system called netplan - this system uses yaml to configure settings.

Netplan uses configuration files from /etc/netplan and uses all files of with extension yaml. You can either have one big file with all interfaces configured - or have one per interface.

I suggest you create a yaml file per interface - that makes it much easier to not mess up another interface by mistake if you need to change something. So if your interface is called ens192 - you create a file /etc/netplan/ens192-config.yaml or something similar - where the name of the interface is part of the file name.

A simple DHCP file could look like

network:
    version: 2
    renderer: networkd
    ethernets:
        ens192:
            dhcp4: true

As you can see its very well structured, and since it uses yaml it makes it easier to know how to add “multiple” items of the same thing, since it uses the yaml syntax.

So to change to a static ip setup simply change to something similar to:

network:
    version: 2
    renderer: networkd
    ethernets:
        ens192:
            addresses:
                - 192.168.0.4/16
            nameservers:
                search:
                    - root.dom
                addresses:
                    - 192.168.0.2
                    - 192.168.0.3
            routes:
                - to: default
                  via: 192.168.0.1

I really like the netplan file format, since its very easy to see how to add an extra ip address, name server etc.

Restart networking and apply changes by doing a sudo netplan apply - or restarting the machine.