How to Set Up a Static IP for Ubuntu: A Step-by-Step Guide

How to Set Up a Static IP for Ubuntu: A Step-by-Step Guide
Photo by Jainath Ponnala / Unsplash

Setting up a static IP address on an Ubuntu server is crucial for maintaining consistent network performance and ensuring reliable access. In this blog post, we'll walk you through the process of configuring a static IP on your Ubuntu machine using Netplan, and we'll cover some common troubleshooting steps.

Step 1: Editing the Netplan Configuration File

The first step in setting up a static IP is to edit the Netplan configuration file. Netplan is the utility that manages network configuration on Ubuntu. You can find the configuration files in the /etc/netplan/ directory.

To edit the Netplan configuration file, open a terminal and use the following command:

sudo nano /etc/netplan/01-netcfg.yaml

In the editor, you’ll need to modify the configuration to set your static IP address. Here’s an example configuration:

network:
  version: 2
  ethernets:
    en999: # Replace with your network interface name
      dhcp4: no
      addresses:
        - 192.168.0.999/24 # Replace with your desired static IP
      routes:
        - to: default
          via: 192.168.0.1 # Replace with your router's IP address
      nameservers:
        addresses:
          - 1.1.1.1 # Primary DNS server
          - 8.8.8.8 # Secondary DNS server

Make sure to replace en999 with your actual network interface name, and adjust the IP addresses to fit your network configuration.

Step 2: Applying the Configuration

After saving the changes to the Netplan configuration file, you’ll need to apply the new settings. Run the following command:

sudo netplan apply

Step 3: Troubleshooting Common Errors

While applying the configuration, you might encounter an error similar to this:

WARNING:root:Cannot call Open vSwitch: ovsdb-server.service is not running.

This error indicates that the Open vSwitch service is not running, which is required for Netplan to apply the network configuration.

Step 4: Installing Open vSwitch

To resolve this issue, you need to install the openvswitch-switch package. Open vSwitch is a production-quality, multilayer virtual switch commonly used in virtualized environments.

Install the package using the following command:

sudo apt install openvswitch-switch

Step 5: Starting and Enabling Open vSwitch

Once the package is installed, make sure the Open vSwitch service is started and enabled to run on boot:

sudo systemctl start openvswitch-switch
sudo systemctl enable openvswitch-switch

Step 6: Reapplying the Configuration

With the Open vSwitch service running, you can now reapply the Netplan configuration:

sudo netplan apply

This time, the configuration should apply successfully, and your Ubuntu server will be configured with a static IP address.

Conclusion

Setting up a static IP on Ubuntu using Netplan is a straightforward process, but it can involve some troubleshooting. By following these steps, you can ensure your server maintains a consistent network identity, which is essential for reliable access and performance. Whether you're managing a home server or deploying a large-scale network, having a static IP address helps maintain stability and predictability in your network setup. Happy networking!

By Yamen Atassi - Modified by AI