In this guide, we are going to set a static IP address on an Ubuntu Server (specifically version 22.04 Jammy Jellyfish). By default, the server is often configured to receive an IP via DHCP, but for server operations, a static IP is usually preferred. We will achieve this by editing the Netplan configuration YAML file.
First, log into your machine. You can verify your current IP address and ensuring you have connectivity (e.g., to a Cloudflare DNS) before making changes.
Navigate to the Netplan configuration directory to see the current settings. Usually, the file is located in /etc/netplan/. If you check the file content, you will likely see dhcp4: true, indicating the server is currently configured for DHCP.
We will use the nano text editor to modify the configuration file. It is crucial to maintain correct indentation when editing YAML files, as they are space-sensitive.
You will need to replace the DHCP configuration with specific static details:
ens33).192.168.74.10/24).1.1.1.1 and Google 8.8.8.8).192.168.74.2).Your configuration should look similar to this:
network:
ethernets:
ens33:
addresses:
- 192.168.74.10/24
nameservers:
addresses:
- 1.1.1.1
- 8.8.8.8
routes:
- to: default
via: 192.168.74.2
version: 2
After editing, save the file and exit Nano.
Once the file is saved, apply the new configuration using the following command:
sudo netplan apply
You may see a warning regarding Open vSwitch, which is generally okay to ignore for this virtual machine setup.
Verify that the IP address has changed by running the ip address command:
ip a
You should see your new static IP (e.g., 192.168.74.10) listed under your interface. Finally, test connectivity by pinging your default gateway and an external address:
ping 192.168.74.2
ping 1.1.1.1
If the pings are successful, your static IP is correctly configured and operational.