Skip to main content

Command Palette

Search for a command to run...

How to Configure Static IP on Ubuntu and Rocky Linux

Updated
4 min read

In real production environments, servers do not rely on DHCP.
They use static IP addresses so services like SSH, Ansible, Node Exporter, and databases remain consistently reachable.

In this article, we will configure static IP addresses on:

  • Ubuntu Server 22.04

  • Rocky Linux 9

Network Architecture

In the last article, we created an internal network in the range 172.29.10.0/24.
Now, we will configure the VMs with the following IP addresses:

  • Ubuntu: 172.29.10.10

  • Rocky: 172.29.10.20
    Both machines will use 172.29.10.254 as the gateway IP. We will introduce a dedicated router VM later in the series. For now, we reserve .254 as the gateway address

⚠️Tip: Never change the gateway or primary IP remotely without console access.

Configure Static IP on Ubuntu Server (Netplan)

Modern Ubuntu Server (including 22.04 and 24.04) uses Netplan for network configuration.

  1. First, we need to identify the network interface. Run ip a and look for something like ens33

  2. The netplan files are located in /etc/netplan.
    In a fresh Ubuntu Server installation, you will see 50-cloud-init.yaml.
    When you open it, you will see that it configures ens33 using DHCP. To prevent cloud-init from overwriting our static configuration, we need to disable its network management.

  3. We can disable cloud-init by creating a file /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with network: {config: disabled}
    You can use the below one-liner to do it

    echo 'network: {config: disabled}' | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
    

    After the reboot, the cloud-init will be disabled. But don't reboot yet.

  4. Now to configure static IP, we have to create a file inside /etc/netplan similar to the 50-cloud-init.yaml
    Open a new YAML file:

    sudo nano /etc/netplan/01-static-ip.yaml
    

    Enter the following content into this file and save it:

    network:
    version: 2
    renderer: networkd
    ethernets:
     ens33:
       dhcp4: no
       addresses:
         - 172.29.10.10/24
       routes:
         - to: default
           via: 172.29.10.254
       nameservers:
         addresses:
           - 172.29.10.254
           - 1.1.1.1
    
  5. Now, first we should check the changes. Run sudo netplan try.
    This step is significant, especially when configuring networking over SSH.
    This gives 120 seconds to try the new changes.
    If new configurations are not accepted within the 120-second window, the changes are automatically reverted.

  6. In the previous step, you may have seen a file permissions too open warning. Let's fix it. Run the following commands.

    sudo chmod 600 /etc/netplan/01-static-ip.yaml
    sudo chown root:root /etc/netplan/01-static-ip.yaml
    sudo netplan try
    

    The warning should now be gone

  7. To verify the configuration, let's try to SSH on the new IP using our host machine.

  8. SSH connection was successful; that means our configuration is working. Now, to save the configuration, just run sudo netplan apply. You can do it from within the SSH.
    After running ip a, you should see the new IP address 172.29.10.10.

    The DHCP-assigned IP may still appear temporarily.

Configuring Static IP on Rocky Linux

Rocky Linux uses NetworkManager, which can be managed via the nmcli command-line tool. Using nmcli is straightforward and efficient.

  1. First, let's identify the interface name using sudo nmcli con show and check the current IP with ip a

    As you can see above, the interface name is ens160. Do not confuse NAME with DEVICE in nmcli output. NAME can be Wired Connection x or sankalp too, whereas DEVICE will match what's shown in the output of ip a.

  2. Run the below command to set a static IP for the interface name you obtained above

    sudo nmcli con mod '<NAME>' ipv4.method manual ipv4.addresses '172.29.10.20/24' ipv4.gateway '172.29.10.254' ipv4.dns '172.29.10.254,1.1.1.1' connection.autoconnect yes
    
  3. Now, run sudo nmcli dev reapply '<DEVICE>', use the DEVICE name here. This will apply the new changes instantly.

    nmcli does not provide an automatic rollback mechanism like netplan try

  4. Check with ip a, and you should see a new IP in your interface.

What's Next?

Now that our Ubuntu and Rocky Linux VMs have static IPs, the network is stable and predictable.
In the next article, we will build a dedicated gateway VM at 172.29.10.254, configure IP forwarding, and provide internet access to our internal machines.
This will evolve our setup into a structured multi-VM lab architecture, similar to real production environments.

Series Roadmap

  1. Download VMware Workstation Pro ✅

  2. Installation Guide ✅

  3. Creating Your First VM (Ubuntu Server 22.04)

  4. Installing Rocky Linux on VMware

  5. Setting up Internal Networks

  6. Configuring Static IP on Ubuntu and Rocky ✅

  7. Building a Gateway VM for Multi-VM Lab Architecture

  8. Multi-VM DevOps Lab Architecture