How to Configure a Static IP Address in Ubuntu Linux

By default, when you connect an Ubuntu Linux machine to a network, your router automatically assigns it a dynamic IP address using DHCP. For a standard desktop computer used for web browsing, this is perfectly fine. However, if you are setting up a home server, a network-attached storage (NAS) device, or a self-hosted Pi-hole, a dynamic IP is a massive liability. If your router reboots and hands your server a new IP address, all of your connected devices will instantly lose access to it.

To ensure your server remains permanently reachable at the exact same address, you must learn how to configure a static IP address in Ubuntu Linux.

Understanding Netplan

If you have used Linux for many years, you might instinctively try to edit the old /etc/network/interfaces file. Do not do this. Modern versions of Ubuntu (18.04 and newer) have completely replaced the old networking system with a new utility called Netplan.

Netplan uses YAML files to configure network interfaces. YAML is extremely strict about indentation; you must use spaces (never tabs) to indent your lines, or the configuration will completely fail to apply.

Step 1: Identify Your Network Interface

Before you can assign a static IP, you need to know the exact name of the network interface you are trying to configure (such as your Ethernet port or Wi-Fi adapter).

  1. Open your terminal or connect to your server via SSH.
  2. Run the following command: ip a
  3. Look for your primary active connection. It will usually start with “en” for Ethernet (e.g., enp3s0 or eth0) or “wl” for Wi-Fi (e.g., wlan0). Note this name down; you will need it later.

Step 2: Locate the Netplan Configuration File

Next, you need to find and edit the default Netplan configuration file.

  1. Navigate to the Netplan directory by running: cd /etc/netplan/
  2. List the contents of the directory by running: ls
  3. You should see a YAML file. Its name varies depending on your installation, but it is typically called something like 00-installer-config.yaml, 01-network-manager-all.yaml, or 50-cloud-init.yaml.

Step 3: Edit the YAML File

Now, open the file using the Nano text editor. (Replace “00-installer-config.yaml” with the actual name of your file).

sudo nano 00-installer-config.yaml

By default, the file will look something like this, indicating that DHCP is currently enabled:

network:
  ethernets:
    enp3s0:
      dhcp4: true
  version: 2

You need to completely rewrite this block to disable DHCP and manually specify your IP address, your router’s gateway IP, and your preferred DNS servers. Modify the file to look like the following example. (Remember: Use spaces for indentation, absolutely no tabs!).

network:
  ethernets:
    enp3s0:
      dhcp4: false
      addresses:
        - 192.168.1.50/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 1.1.1.1]
  version: 2

Explanation of the variables:

  • addresses: This is the static IP you want to assign to your server (e.g., 192.168.1.50). The /24 at the end represents the subnet mask (equivalent to 255.255.255.0), which is standard for almost all home networks.
  • via: This is the default gateway (the IP address of your home router).
  • nameservers: These are the DNS servers your computer will use to resolve websites. In this example, we used Google (8.8.8.8) and Cloudflare (1.1.1.1).

Once you have carefully typed your configuration, press Ctrl+O to save the file, hit Enter to confirm the name, and then press Ctrl+X to exit the Nano editor.

Step 4: Apply and Test the Changes

Before permanently applying the new IP address, you should instruct Netplan to test the configuration for syntax errors.

  1. Run the test command: sudo netplan try
  2. If you made a typo or used a tab instead of spaces, Netplan will throw an error and immediately revert to your old settings so you don’t lose your SSH connection.
  3. If the configuration is valid, Netplan will ask you to press Enter to accept the new settings.

If you prefer to skip the test and apply the settings immediately (which is slightly riskier), you can simply run:

sudo netplan apply

Your Ubuntu machine is now locked to its new static IP address. You can verify the change was successful by running the ip a command one final time.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.