Sling Academy
Home/DevOps/Ubuntu: How to assign a static IP address to a network interface (2 ways)

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

Last updated: January 28, 2024

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.

Next Article: How to change OpenSSH config in Ubuntu

Previous Article: Managing volumes in Ubuntu: The ultimate guide

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide