Skip to content

How to setup static IP on Ubuntu 24

To set up static IP configuration on VPS'es that runs Ubuntu 24, we have to login into Fleio.
After connecting to Fleio we can use the console to run commands.

After we have logged into the console, verify that we are a superuser (sudo) and then we proceed with these steps:

  1. First, we need to identify the network interface we want to configure using the ip a, it might be named enp3s0 or something similar:

    root@:~# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host noprefixroute
           valid_lft forever preferred_lft forever
    2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether fa:16:3e:de:4b:6e brd ff:ff:ff:ff:ff:ff
        inet 188.241.119.200/21 metric 100 brd 188.241.119.255 scope global dynamic enp3s0
           valid_lft 86370sec preferred_lft 86370sec
        inet6 2a06:cd40:100:2::169/64 scope global
           valid_lft forever preferred_lft forever
        inet6 fe80::f816:3eff:fede:4b6e/64 scope link
           valid_lft forever preferred_lft forever
    

  2. The second step is to modify the netplan configuration file in /etc/netplan, the default file should be named 50-cloud-init.yaml, here is an example:

    Note The file should automatically set the subnet and gateway but if they are wrong or non-existent, you can find the subnet using ip a and the hostmin or gateway using ip r or ip route show

    network:
        version: 2
        ethernets:
            enp3s0:
                dhcp4: no
                addresses:
                - <your IPv4 address>/subnet
                - <your IPv6 address>/subnet
                nameservers:
                    addresses:
                    - <nameserver1>
                    - <nameserver2>
                    - <nameserver3>
                    - <nameserver4>
                routes:
                -   to: default
                    via: <your IPv4 gateway>
                -   to: ::/0
                    via: <your IPv6 gateway>
                set-name: enp3s0
    
    Note Here is an example of ip r output, we will choose the value on the first line, specifically 188.241.112.1:
    root@ubuntu24:~# ip r
    default via 188.241.112.1 dev enp3s0 proto dhcp src 188.241.119.200 metric 100
    

  3. In order to make the changes persistent (meaning they won't reset if the instance is rebooted) follow the steps included at the top of the file 50-cloud-init.yaml and create the file:

    # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
    # network: {config: disabled}
    

  4. In order to secure the file we just edited we must change its permissions to only be readable an writeable by the root user:

    chmod 600 /etc/netplan/50-cloud-init.yaml
    

  5. The next step is to apply the new network configuration using netplan. You can also test the configuration before apply, with netplan try. In order to apply this we will run the following commands:

    netplan apply
    

  6. Check if the configuration is up by running either ip a or netplan status and at the end do a final check by pinging the IP.

Configuration for multiple IPs.

If you want to configure multiple IPs in your instance, all you need to do is add another IP to /etc/netplan/50-cloud-init.yaml

addresses:  
  - <your first IPv4 address>/subnet  
  - <your second IPv4 address>/subnet  

After you have completed all these steps and the IP is responding to ping, you have successfully configured the static IP.