How to Change a Static IP Address in Ubuntu Server
Notes on configuring static IP in Linux
This guide will walk you through the process of changing the static IP address on an Ubuntu Server.
Configuring a static IP address is a crucial step when setting up an Ubuntu server. Unlike a dynamic IP (which changes whenever the system reboots or the DHCP lease expires), a static IP ensures the server maintains the same network address. This is especially important for servers running services like web hosting, DNS, or file sharing.
1. Prerequisites
Before proceeding, make sure you have:
-
Access to the Ubuntu server (physical or SSH).
-
Sudo or root privileges.
-
Network details such as:
- The new IP address you want to assign
- Subnet mask (or CIDR notation, e.g.,
/24
) - Gateway IP address
- DNS server addresses
2. Identify Your Network Interface
Run the following command to list your active network interfaces:
ip a
Look for interface names like eth0
, ens33
, ens160
, or enp0s3
. Make a note of the interface you plan to configure.
3. Configure Netplan (Ubuntu 18.04 and Later)
Modern Ubuntu Server versions use Netplan for network configuration. The YAML configuration files are typically stored in:
/etc/netplan/
To edit the configuration:
sudo nano /etc/netplan/01-netcfg.yaml
(Your filename may differ, e.g., 50-cloud-init.yaml
.)
4. Example Netplan Configuration
Below is an example of a static IP configuration:
network:
version: 2
ethernets:
ens33:
dhcp4: no
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
Key points:
dhcp4: no
disables DHCP for IPv4.addresses
defines the static IP with subnet.gateway4
sets the default gateway.nameservers
specifies DNS servers.
Replace ens33
with your network interface name, and adjust the IP, gateway, and DNS settings as needed.
5. Apply the Changes
After saving the file, apply the new settings with:
sudo netplan apply
If you are connected via SSH, be cautious—an incorrect configuration may disconnect you. For troubleshooting, you can run:
sudo netplan try
This allows you to test the configuration and automatically roll back if you don’t confirm within 120 seconds.
6. Verify the Configuration
To confirm your new static IP:
ip a
You should see the updated static IP address assigned to your interface.
You can also test connectivity:
ping -c 4 8.8.8.8
ping -c 4 google.com
7. Configuring Static IP on Older Ubuntu Versions (Pre-18.04)
For Ubuntu 16.04 and earlier, network configuration is managed in:
/etc/network/interfaces
Example:
auto ens33
iface ens33 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
Save and restart networking:
sudo systemctl restart networking
8. Common Troubleshooting Tips
- YAML indentation matters: Always use spaces, not tabs.
- If DNS is not working, double-check the
nameservers
section. - If SSH connection drops, ensure the new IP is within your accessible subnet.
Changing a static IP address in Ubuntu Server is straightforward once you know where to configure it.
On Ubuntu 18.04 and later, Netplan is the default tool, while older versions rely on the interfaces
file.
By following the steps outlined above, you can ensure your server always has a fixed IP address, making it more reliable for hosting and networking tasks.