
Fixing the Address: Configuring Static IPs and Interfaces
Stop relying on DHCP. Learn to configure permanent network settings on Linux. Master the modern Netplan (YAML) configuration for Ubuntu and the powerful NetworkManager (nmcli) for enterprise distros.
Configuring Static IPs: Giving Your Server a Permanent Home
Most home computers use DHCP, where the router "assigns" an address that might change every few days. This is fine for a laptop, but disastrous for a server. If your database's IP address changes, your web app will stop working.
To build reliable infrastructure, you must know how to configure a Static IP. In the modern Linux world, this is done differently depending on your "Family" of distribution. Ubuntu uses Netplan, while Fedora and RHEL use NetworkManager.
In this lesson, we will learn both.
1. The Ubuntu Way: Netplan (YAML)
Ubuntu 18.04+ introduced Netplan. Instead of editing confusing config files, you write simple, structured YAML files.
Where it lives:
Files are found in /etc/netplan/. Usually named 01-netcfg.yaml.
A Static IP Example:
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses:
- 192.168.1.50/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
Applying changes:
Danger: If you make a typo in the YAML, you will lose connection to the server!
sudo netplan try: This applies the change for 120 seconds. If you don't confirm it, it "Rolls back" to the old settings. Use this for remote servers!sudo netplan apply: Applies changes permanently.
2. The Enterprise Way: NetworkManager (nmcli)
NetworkManager is the standard for Desktop Linux and Red Hat-based servers. It is usually controlled via the nmcli command line tool.
Useful nmcli Commands:
nmcli device status: See all physical cards.nmcli connection show: See all configured profiles.
Setting a Static IP with nmcli:
# 1. Set the IPv4 address
sudo nmcli con mod "eth0" ipv4.addresses 192.168.1.50/24
# 2. Set the Gateway
sudo nmcli con mod "eth0" ipv4.gateway 192.168.1.1
# 3. Change selection from 'auto' to 'manual'
sudo nmcli con mod "eth0" ipv4.method manual
# 4. Restart the connection
sudo nmcli con up "eth0"
3. Bringing Interfaces Up and Down
Sometimes a network card gets "glitched." You can restart the software link without rebooting the whole machine.
# The modern way
sudo ip link set eth0 down
sudo ip link set eth0 up
4. Virtual Interfaces (IP Aliasing)
Did you know one network card can have multiple IP addresses? This is very common for web servers hosting multiple sites.
# Add a second 'Temporary' IP to eth0
sudo ip addr add 192.168.1.51/24 dev eth0
5. Practical: The "Locked Out" Recovery
If you are editing network configs on a cloud server and you lose connection, don't panic.
- Use the Cloud Console (the web-terminal).
- Look at the logs:
journalctl -u systemd-networkd. - Revert your
/etc/netplanfile to the backup you (hopefully) made and runnetplan apply.
6. Example: A Network Config YAML Generator (Python)
If you are deploying 50 servers, you don't want to write YAML by hand. Here is a Python script that generates a valid Netplan YAML based on a set of variables.
import yaml
def generate_netplan_config(interface, ip, gateway, dns_list):
"""
Generates a Netplan-compatible YAML configuration.
"""
config = {
'network': {
'version': 2,
'renderer': 'networkd',
'ethernets': {
interface: {
'addresses': [f"{ip}/24"],
'routes': [{'to': 'default', 'via': gateway}],
'nameservers': {'addresses': dns_list}
}
}
}
}
# Dump to YAML format
yaml_output = yaml.dump(config, default_flow_style=False)
return yaml_output
if __name__ == "__main__":
# Settings for a new server
itf = "enp0s3"
addr = "10.0.0.150"
gw = "10.0.0.1"
dns = ["8.8.8.8", "1.1.1.1"]
print("--- Generated Netplan Config ---")
print(generate_netplan_config(itf, addr, gw, dns))
7. Professional Tip: Check 'Routing Tables'
If you have a server with two network cards (e.g., one for the internet and one for a private database), you need to manage the Routing Table. This tells Linux which "Exit Door" to use for which traffic.
# See the full routing table
ip route
8. Summary
Configuring a static environment is the bridge between "Playing with Linux" and "Building Infrastructure."
- Netplan is the YAML-based standard for Ubuntu.
nmcliis the command-line interface for NetworkManager.- Use
netplan tryto safety-test changes on remote servers. ip linkmanages the physical state of your cards.
In the final lesson of this module, we will learn how to connect to these static servers securely from anywhere: Remote Access with SSH.
Quiz Questions
- Why is
netplan trysafer thannetplan applywhen working on a remote AWS server? - In a Netplan YAML file, what does the
renderer: networkdline signify? - How can you find the exact name of your Ethernet interface (e.g.,
eth0vsenp3s0)?
Continue to Lesson 6: Remote Access—Master of SSH.