Static IP address on Ubuntu

Static IP address on Ubuntu

Whenever I install server operating systems, I set them up with a static IP Address. My DNS servers are virtualised, so I can access every host server and VM using an IP even if the DNS servers aren’t up and running. I don’t have many servers, so this works well for me.

Whenever Ubuntu is being installed, if I remember, I set them up with a static IP. However, occasionally, I forget to do this when installing the software and have to do it on a running system. To do that, I have to edit the configuration.

The recommended way of assigning static IP addresses

The easiest and recommended way to assign a static IP address to a device on your LAN is to configure a Static DHCP on your router. Static DHCP or DHCP reservation is a feature found on most routers which makes the DHCP server automatically assign the same IP address to a specific network device each time the device requests an address from the DHCP server. Assigning a static IP to the device’s unique MAC address works.

The steps for configuring a DHCP reservation vary from router to router. Consult the vendor’s documentation for more information.

Assigning static IP addresses on the server

Netplan

Ubuntu 17.10 and later uses Netplan as the default network management tool. The previous Ubuntu versions were using ifconfig and its configuration file /etc/network/interfaces to configure the network.

Netplan configuration files are written in YAML syntax with a .yaml file extension. To configure a network interface with Netplan, you need to create a YAML description for the interface, and Netplan will generate the required configuration files for the chosen renderer tool.

Netplan supports two renderers, NetworkManager and Systemd-networkd. NetworkManager is mostly used on Desktop machines, while the Systemd-networkd is used on servers without a GUI.

Configuring Static IP address on Ubuntu Server

On Ubuntu 20.04, the system identifies network interfaces using ‘predictable network interface names’.

The first step toward setting up a static IP address is identifying the name of the ethernet interface you want to configure. To do so, use the ip link command, as shown below: –

ip link

This outputs a few lines on my virtual machines. The important line is line 3 as this shows the name of the Proxmox network device and the state is ‘UP’: –

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp6s18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 82:e6:2d:52:11:7f brd ff:ff:ff:ff:ff:ff
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default
    link/ether 02:42:be:f8:c4:be brd ff:ff:ff:ff:ff:ff

You can then issue the following command to see the DHCP IP address assigned to this device with: –

ip addr

This shows the following details: –

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
       valid_lft forever preferred_lft forever
2: enp6s18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 82:e6:2d:52:11:7f brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.130/24 metric 100 brd 192.168.1.255 scope global dynamic enp6s18
       valid_lft 65088sec preferred_lft 65088sec
    inet6 fe80::80e6:2dff:fe52:117f/64 scope link
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:be:f8:c4:be brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

Line 9 shows that the device has an IP address of 192.168.1.130.

Netplan configuration files are stored in the /etc/netplan directory. You’ll probably find one or more YAML files in this directory. The name of the file may differ from setup to setup. Usually, the file is named either 01-netcfg.yaml, 50-cloud-init.yaml, or NN_interfaceName.yaml, but in your system, it may be different.

If your Ubuntu cloud instance is provisioned with cloud-init, you’ll need to disable it. To do so, create the following file:

sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

This should look like this: –

network: {config: disabled}

In my Ubuntu netplan folder /etc/netplan, there is normally a file called 00-installer-config.yaml with the following inside: –

network:
  ethernets:
    enp6s18:
      dhcp4: true
  version: 2

As you can see, the default is set up as DHCP.

The first thing we must do is to change the dhcp4 setting to false. Then, we must add the configuration we need: –

network:
  renderer: networkd
  ethernets:
    enp6s18:
      addresses:
        - 192.168.1.41/24
      nameservers:
        addresses: [192.168.1.13,1.1.1.1]
      routes:
        - to: default
          via: 192.168.1.1
  version: 2

In this example, I set the static IP address to 192.168.1.41, the gateway to my router IP address to 192.168.1.1, and the DNS servers to 192.168.1.13 and 1.1.1.1.

After making these changes, you can either reboot the VM or issue the following command: –

sudo netplan apply

After which, you can issue the ‘ip addr’ command again, and you should see the new static IP address.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.