Setting up a static IP address is a crucial task for any system administrator managing Ubuntu servers. Whether you’re running a home lab or managing production servers, having a consistent IP address ensures reliable network connectivity and easier server management. In this guide, I’ll walk you through the process of configuring a static IP address in Ubuntu 24.04 using the command line.
requirements
Before we begin, make sure you have:
- Ubuntu 24.04 installed
- Sudo or root access to your system
- Basic knowledge of command-line operations
- Backup of your current network configuration
Understanding the Problem
By default, Ubuntu systems are configured to use DHCP (Dynamic Host Configuration Protocol) to obtain an IP address automatically. While this works well for desktop systems, servers typically need static IP addresses for:
- Consistent network access
- Proper DNS resolution
- Reliable service hosting
- Simplified firewall configuration
Step-by-Step Solution
1. Identify Your Network Interface
First, let’s find out which network interface we need to configure:
nmcli d
This command will show all your network interfaces. You’ll see output similar to this:
DEVICE TYPE STATE CONNECTION
enp0s3 ethernet connected Wired connection 1
lo loopback unmanaged --
Note down your interface name (in this example, it’s enp0s3
).
2. Create/Edit Netplan Configuration
Ubuntu 24.04 uses Netplan for network configuration. Let’s create or edit the configuration file:
sudo nano /etc/netplan/01-netcfg.yaml
Add the following configuration (adjust the values according to your network):
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: no
addresses:
- 192.168.1.10/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
Key configuration elements:
enp0s3
: Your network interface name192.168.1.10/24
: Your desired static IP and subnet mask192.168.1.1
: Your network gateway8.8.8.8, 8.8.4.4
: DNS servers (using Google’s DNS in this example)
3. Secure the Configuration File
It’s crucial to set proper permissions on your Netplan configuration:
sudo chmod 600 /etc/netplan/01-netcfg.yaml
4. Apply the Configuration
Now, let’s apply the new network settings:
sudo netplan apply
5. Verify the Configuration
Check if your static IP has been properly configured:
ip a
You should see your new static IP address assigned to your network interface.
Common Issues and Solutions
- Configuration Not Applied
- Error: Changes don’t take effect after
netplan apply
- Solution: Check the syntax in your YAML file
sudo netplan --debug apply
- Network Connectivity Lost
- Error: Can’t connect to network after applying changes
- Solution: Verify gateway and DNS settings are correct
ping 8.8.8.8
ping google.com
- Permission Issues
- Error: Unable to modify netplan configuration
- Solution: Ensure you’re using sudo and check file permissions
sudo chown root:root /etc/netplan/01-netcfg.yaml
sudo chmod 600 /etc/netplan/01-netcfg.yaml
Best Practices
- Always backup your current network configuration before making changes:
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.backup
- Use meaningful IP addresses that follow your network scheme
- Document your network configuration changes
- Test connectivity after applying changes
Conclusion
Setting up a static IP address in Ubuntu 24.04 is straightforward when you follow these steps carefully. Remember to always backup your configuration before making changes and double-check your network settings to avoid connectivity issues.
This setup ensures your Ubuntu server maintains a consistent network presence, which is essential for reliable service hosting and network management.