Skip to main content

Tutorial: Set a Static IP Address on an Ubuntu System

Abstract

Follow these steps to set up a static IP address on an Ubuntu system. This is important to run Access Server.

Overview

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

Important

  • This guide is for Access Servers deployed in a local or private network.

  • For cloud environments, reference the 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, and not already in use.

  • 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.

You need the following to get started:

  1. Console or SSH access to your server with root privileges.

  2. The static IP address for your server — in our example, we use 192.0.2.2.

  3. The network subnet for your server — in our example, we use 192.0.2.0/24.

  4. 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.

Caution

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 configuration, creating a snapshot of your VM, or ensuring access to the (virtual) console to have a way to fix errors.

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 it 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.

To assign a static IP address, edit the text file containing your network interfaces' configuration.

Note

The file 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. If you have 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 and get 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:

    network:1
      ethernets:2
        eth0:3
         dhcp4:4 false
         addresses:5 [192.0.2.2/24]
         routes:6
         - to: default
           via: 192.0.2.254
         nameservers:7
           addresses: [192.0.2.254]

    1

    This is the configuration file's root element and signifies the network configuration's start.

    2

    This section specifies the start of the ethernet interfaces configuration.

    3

    This identifies the specific ethernet interface being configured. You can use the command ip link to list your interface names.

    4

    This indicates whether the interface obtains an IP address automatically from a DHCP server.

    5

    Here, you specify the static IP address and subnet mask.

    6

    This section defines static routes where to: default specifies a default route through the gateway, 192.0.2.254, with via: 192.0.2.254 .

    7

    This key specifies the DNS servers to be used by the system.

    Important

    Ensure you use correct spacing to avoid errors. The correct spacing is found in /usr/share/doc/netplan/examples/static.yaml.

  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

Tip

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.

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, 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 nameserver 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.