Tutorial: Set a Static IP Address on an Ubuntu System
Overview
This guide provides instructions for setting a static IP address on an Ubuntu server in your local or private network.
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.
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.
Connect to your console and get root privileges.
Open the file /etc/netplan/01-netcfg.yaml with the nano text editor:
nano /etc/netplan/01-netcfg.yamlLocate the dhcp4 item for your network interface and set it to false.
Use the following example to add the static IP address information:
network: ethernets: eth0: dhcp4: false addresses: [192.0.2.2/24] routes: - to: default via: 192.0.2.254 nameservers: addresses: [192.0.2.254]This is the configuration file's root element and signifies the network configuration's start.
This section specifies the start of the ethernet interfaces configuration.
This identifies the specific ethernet interface being configured. You can use the command
ip linkto list your interface names.This indicates whether the interface obtains an IP address automatically from a DHCP server.
Here, you specify the static IP address and subnet mask.
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 .
This key specifies the DNS servers to be used by the system.
Press ctrl+x, y, and enter to save and exit the file.
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
If you've lost network access to your instance, use the (virtual) console to restore the altered file 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 the Access Server web services aren't responding, you may need to restart the Access Server service with systemctl restart openvpnas. 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 address that no longer exists. Resetting the web services and daemon ports to their default settings will enable them to work with the new IP address configuration.