Ubuntu: How to assign a static IP address to a network interface (2 ways)

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction to Static IP Configuration

Assigning a static IP address to a network interface is an essential task for any network administrator or user who needs a constant IP address for their system. On Ubuntu, this can be achieved using various methods, ranging from editing network configuration files to using network management tools. In this tutorial, we’ll explore different ways to assign a static IP address on an Ubuntu system.

Prerequisites

Before getting started, make sure you have the following:

  • An Ubuntu system
  • Root privileges or the ability to use ‘sudo’
  • A working knowledge of networking concepts
  • An understanding of your network’s IP address structure

Method 1: Using Netplan

Since Ubuntu 17.10, Netplan has been introduced as the new method to configure network interfaces. Let’s start with the most basic way to assign a static IP using Netplan.

$ sudo nano /etc/netplan/*.yaml

Edit the Netplan YAML configuration file according to your needs. Below is a simple example:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
          addresses: [8.8.8.8, 8.8.4.4]

After editing, apply the changes:

$ sudo netplan apply

Check the new IP address:

$ ip addr show enp0s3

Method 2: Using /etc/network/interfaces

For older Ubuntu systems, or if you’ve uninstalled Netplan, you can configure your IP by editing the /etc/network/interfaces file.

$ sudo nano /etc/network/interfaces

Here is a basic static IP configuration:

auto enp0s3
iface enp0s3 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

Restart the networking service to apply changes:

$ sudo /etc/init.d/networking restart

Advanced Configuration

For a more advanced configuration, you can set multiple static IPs or configure VLANs. For instance, configuring a secondary IP address:

auto enp0s3
iface enp0s3 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1

iface enp0s3:1 inet static
    address 192.168.1.101
    netmask 255.255.255.0

To configure a VLAN:

auto enp0s3.10
iface enp0s3.10 inet static
    address 192.168.2.100
    netmask 255.255.255.0
    vlan-raw-device enp0s3

Troubleshooting

If your network is not functioning as expected after assigning a static IP, ensure your configuration files are correct, your cables are plugged in, and your network services are restarted. Checking the log files can provide useful information:

$ journalctl -u networkd

Conclusion

Assigning a static IP on Ubuntu can be straightforward once you’re familiar with Netplan or the traditional /etc/network/interfaces method. Utilize the one that is most suitable for your version of Ubuntu and specific networking requirements.