Set a static IP address on an Ubuntu system

Introduction

This guide provides instructions for setting a static IP address on an Ubuntu server in your local or private network.

Notes about this guide:

  • This guide is for Access Servers deployed in a local or private network.
  • For cloud environments, reference the cloud provider's documentation for static IP address assignment.
  • If you use NetworkManager, you should use that instead of this guide.
  • Ensure that the static IP for Access Server is available for assignment, not part of a DHCP range or already in use otherwise.
  • These instructions apply to Ubuntu 20 and newer. 
  • We recommend using a fully qualified domain name (FQDN) as the address to which VPN clients connect, even if you use a static IP address. 

Before you begin

You need the following to get started:

  • Console or SSH access to your server with root privileges.
  • The static IP address for your server — in our example, we use 192.0.2.2.
  • The network subnet for your server — in our example, we use 192.0.2.0/24.
  • The internet gateway and DNS server IP address — in our example, we use 192.0.2.254.

Ensure your IP address and values match your network. These steps use IPv4 addresses. We use example IPv4 addresses reserved for documentation.

Important: When you change the network interface settings, you could lose network access to the system if you make a mistake. We recommend backing up your Access Server settings, taking a snapshot of your VM, or ensuring access to the (virtual) console to have a way to fix errors.

DHCP server tips

If Access Server receives an IP address from a DHCP server, use the DHCP reservation or static lease option there (if available) to always assign the same DHCP IP address to the server. This way, you don't have to configure a static IP in the operating system.

Not all DHCP servers have this option. In such a case, you can follow these steps to set the static IP address in the Linux server operating system. If you set the static IP address directly on the server, ensure you configure the static IP to one outside of the DHCP range. Otherwise, the DHCP server may consider the IP address you assign to your server suitable for assignment to a DHCP client, which may lead to an IP address conflict.

Some networks don't have a DHCP server; you must assign a static IP address. In that case, pick any available IP address in your network.

Edit network configuration file

To assign a static IP address, the first step is editing the text file containing your network interfaces' configuration.

Note: The filename storing the netplan configuration may have a different name than 01-netcfg.yaml, such as 50-cloud-init.yaml. You may also have multiple files in the directory. With multiple files, pick the one with the highest number. If you don't have the /etc/netplan directory, you may be on an older Ubuntu system, and we recommend upgrading.

  1. Connect to your console with root privileges.
  2. Open the file /etc/netplan/01-netcfg.yaml with the nano text editor:
    nano /etc/netplan/01-netcfg.yaml
  3. Locate the dhcp4 item for your network interface and set it to false.
  4. Use the following example to add the static IP address information:
    networks:
      ethernets:
        eth0:
         dhcp4: false
          addresses: [192.0.2.2/24]
         routes:
    - to: default
           via: 192.0.2.254
         nameservers:
           addresses: [192.0.2.254]
  5. Press ctrl+x, y, and enter to save and exit the file.
  6. Apply the setting with this command:
    netplan apply

In the example network configuration file above, adjust for your network based on our example settings:

  • IP address: 192.0.2.2
  • Network: 192.0.2.0/24
  • Internet gateway and DNS server: 192.0.2.254

Note: In our example above, we only have one network interface, eth0. If it is called something else in your system, make allowances for this and adjust things as necessary.

Troubleshooting

If you've lost network access to your instance, use the (virtual) console to restore the altered file back to DHCP mode, undo the changes, or correct any errors reported.

If you have network access but can't ping an address like 8.8.8.8, then your gateway (routes to default via:) may be incorrect. Try correcting this and applying the changes to ensure traffic goes through the correct gateway.

If you can ping 8.8.8.8 but can't ping a domain name like google.com, then make corrections to the nameservers setting; it may have an incorrect DNS server specified there. Try correcting this and applying the changes to ensure DNS resolution goes through the correct DNS server.

If your network is working correctly, but now the Access Server web services don't respond, you may need to restart the Access Server service with service openvpnas restart. If the web services still don't work on the new IP address configuration, you may have configured Access Server to listen only to a specific IP that doesn’t exist anymore. You can reset the web services and daemon ports to their default settings to make it work with the new IP address configuration.