Networking in Linux: Configure Static IP, DNS, and Gateways
In modern Linux systems, precise network configuration is essential for servers, desktops, and embedded devices alike.
This article provides a serious, detailed, and extensive guide on assigning static IP addresses, setting
up DNS resolution, and defining default gateways. Additionally, we’ll touch on advanced routing and integrating VPNs like
OpenVPN,
WireGuard, and
StrongSwan.
1. Understanding Network Interfaces
Network interfaces are represented by device names like eth0, enp3s0, or wlan0.
ip link show– list all interfacesip addr show ltinterfacegt– display IP detailsethtool ltinterfacegt– query link status and capabilities
2. Configuring a Static IP Address
2.1 Debian/Ubuntu (lt18.04) via /etc/network/interfaces
Edit /etc/network/interfaces:
auto enp3s0
iface enp3s0 inet static
address 192.168.1.50
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
2.2 Ubuntu 18.04 (Netplan)
Edit a file under /etc/netplan/ (e.g., 01-netcfg.yaml):
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: no
addresses: [192.168.1.50/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
Apply with sudo netplan apply.
2.3 Red Hat/CentOS (nmcli or ifcfg)
Using nmcli:
sudo nmcli con modify System eth0 ipv4.addresses 192.168.1.50/24
sudo nmcli con modify System eth0 ipv4.gateway 192.168.1.1
sudo nmcli con modify System eth0 ipv4.dns 8.8.8.8 8.8.4.4
sudo nmcli con modify System eth0 ipv4.method manual
sudo nmcli con up System eth0
3. Setting DNS Servers
Linux handles DNS via:
- /etc/resolv.conf – traditional resolver configuration
- systemd-resolved – caching split DNS (Ubuntu, Fedora)
3.1 /etc/resolv.conf
search example.com
nameserver 8.8.8.8
nameserver 8.8.4.4
Note: Prevent overwrites by NetworkManager or DHCP client with chattr i /etc/resolv.conf, but use carefully.
3.2 systemd-resolved
Edit /etc/systemd/resolved.conf:
[Resolve]
DNS=8.8.8.8 1.1.1.1
FallbackDNS=9.9.9.9
Domains=example.com
Enable and restart:
sudo systemctl enable systemd-resolved
sudo systemctl restart systemd-resolved
4. Configuring Gateways and Static Routes
Default gateway directs outbound traffic when no specific route exists.
ip route add default via 192.168.1.1 dev enp3s0
4.1 Persistent Static Routes
Debian/Ubuntu:
# /etc/network/interfaces
up ip route add 10.10.10.0/24 via 192.168.1.254 dev enp3s0
down ip route del 10.10.10.0/24 via 192.168.1.254
5. Best Practices Troubleshooting
- Backup config files before editing.
- Validate syntax (YAML indentation, nmcli query).
- Use logs:
journalctl -u systemd-networkd,dmesg,nmcli general status. - Connectivity tests:
ping,traceroute,dig,nslookup. - Avoid conflicts: ensure DHCP is disabled for static assignments.
6. Integrating VPN Solutions
Secure remote access often requires a Virtual Private Network. Popular solutions include:
- OpenVPN – robust SSL/TLS-based VPN.
- WireGuard – modern, high-performance VPN.
- StrongSwan – IPsec-based, enterprise-grade.
Each VPN can be configured to push custom DNS settings, route all or split-tunnel traffic, and integrate with system network managers.
Refer to the vendors’ documentation for step-by-step installation and advanced tuning.
Leave a Reply