How to Configure a Static IP Address on Ubuntu Linux with Proper Permissions
In this article, we’ll walk through the simple steps to configure a static IP address on an Ubuntu Linux system using Netplan, and ensure the configuration file has the correct permissions for security. We will also cover updating the /etc/hosts
file for hostname resolution.
Step 1: Edit the Netplan Configuration File
Netplan is the default network configuration tool in Ubuntu 18.04 and later. The configuration files are located in /etc/netplan/
.
Open the Netplan configuration file for editing with your preferred text editor:
sudo nano /etc/netplan/01-netcfg.yaml
Step 2: Configure Static IP Address, Gateway, and DNS
Replace the content (or add if empty) with the following example configuration. Adjust the IP addresses and interface name (ens160
) to fit your environment.
network:
version: 2
ethernets:
ens160:
addresses:
- 192.168.0.61/24 # Static IP address with subnet mask
routes:
- to: 0.0.0.0/0 # Default route (gateway)
via: 192.168.0.1 # Your gateway/router IP
nameservers:
addresses:
- 192.168.0.10 # Your Windows DNS server IP
- 8.8.8.8 # Optional fallback DNS (Google DNS)
Save and exit the editor (Ctrl + O
, Enter
, then Ctrl + X
in nano).
Step 3: Secure the Netplan Configuration File
To prevent unauthorized modifications, restrict the permissions of the Netplan config file so only root can read/write it:
sudo chmod 600 /etc/netplan/01-netcfg.yaml
Step 4: Apply the Network Configuration
Run the following command to apply your changes:
sudo netplan apply
Step 5: Update the /etc/hosts
File for Hostname Resolution
Edit the /etc/hosts
file to map your static IP to a hostname. This helps the system resolve hostnames locally.
sudo nano /etc/hosts
Add a line similar to this, adjusting to your IP and hostname:
192.168.0.61 Kubenode01.abcxyz.net Kubenode01
Save and exit the file.
Conclusion
You have successfully configured a static IP address on your Ubuntu system with appropriate file permissions and updated your local hostname resolution. This setup is essential for servers, Kubernetes nodes, and other network-critical machines that require a stable IP configuration.