Network Configuration
Configuring network interfaces on Linux means assigning IP addresses, setting
up routes, choosing between static and DHCP addressing, and making those
settings persist across reboots. Modern distributions offer several tools for
the job -- the ip command for one-shot runtime changes, NetworkManager for
desktop and server use, netplan for Ubuntu, and the classic
/etc/network/interfaces file on Debian. This guide covers all of them.
Back to the Networking hub. Related guides: DNS Guide | Firewall Guide.
The ip command (iproute2)
The ip command replaces the older ifconfig and route utilities. Changes
made with ip take effect immediately but do not survive a reboot.
Addresses
# Show all addresses
ip addr show
# Add an IPv4 address to eth0
ip addr add 192.168.1.10/24 dev eth0
# Add a secondary address
ip addr add 192.168.1.11/24 dev eth0 label eth0:1
# Remove an address
ip addr del 192.168.1.11/24 dev eth0
# Flush every address from an interface
ip addr flush dev eth0
Links
# Bring an interface up or down
ip link set eth0 up
ip link set eth0 down
# Change the MTU
ip link set eth0 mtu 9000
# Rename an interface (must be down first)
ip link set eth0 down
ip link set eth0 name lan0
ip link set lan0 up
Routes
# Show the routing table
ip route show
# Add a default gateway
ip route add default via 192.168.1.1
# Add a static route to a remote network
ip route add 10.10.0.0/16 via 192.168.1.254 dev eth0
# Delete a route
ip route del 10.10.0.0/16
# Replace a route (add or update)
ip route replace 10.10.0.0/16 via 192.168.1.253
NetworkManager (nmcli)
NetworkManager is the default on RHEL, Fedora, CentOS Stream, and most
desktop distributions. The nmcli command-line tool is the fastest way to
interact with it.
# List all connections
nmcli connection show
# Show details for a specific connection
nmcli connection show "Wired connection 1"
# Create a new static connection
nmcli connection add con-name office type ethernet ifname eth0 ipv4.addresses 192.168.1.50/24 ipv4.gateway 192.168.1.1 ipv4.dns "8.8.8.8 8.8.4.4" ipv4.method manual
# Switch an existing connection to DHCP
nmcli connection modify office ipv4.method auto
nmcli connection up office
# Set the system hostname
nmcli general hostname server01.example.com
Connection files are stored under
/etc/NetworkManager/system-connections/ as key-file format INI files.
Netplan (Ubuntu)
Ubuntu 18.04 and later use netplan as a front-end that generates either NetworkManager or systemd-networkd configuration.
# /etc/netplan/01-static.yaml
network:
version: 2
renderer: networkd # or NetworkManager
ethernets:
eth0:
dhcp4: false
addresses:
- 192.168.1.50/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
# Apply the configuration
sudo netplan apply
# Debug: show what will be generated
sudo netplan generate
sudo netplan --debug apply
/etc/network/interfaces (Debian)
On Debian and older Ubuntu releases without netplan the classic ifupdown system is still available:
# /etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.50/24
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
auto eth1
iface eth1 inet dhcp
# Bring an interface up using the config above
sudo ifup eth0
# Bring it down
sudo ifdown eth0
Static vs DHCP
Use DHCP when the network has a DHCP server and you do not need a fixed address. Use static addressing for servers, routers, and any machine that other hosts need to reach by a predictable IP. You can combine both -- let DHCP assign an address on one interface while configuring another statically.
Managing multiple interfaces
Servers often have more than one NIC. Each interface can be configured independently:
# Two static interfaces with separate subnets
nmcli connection add con-name lan type ethernet ifname eth0 ipv4.addresses 10.0.0.5/24 ipv4.method manual
nmcli connection add con-name mgmt type ethernet ifname eth1 ipv4.addresses 172.16.0.5/24 ipv4.method manual
If both subnets have a default gateway you must choose one or use policy routing to avoid ambiguity:
# Add a rule: traffic from 172.16.0.0/24 uses table 100
ip rule add from 172.16.0.0/24 table 100
ip route add default via 172.16.0.1 table 100
Setting the hostname
# Transient (runtime only)
hostname server01
# Persistent (systemd)
hostnamectl set-hostname server01.example.com
# Verify
hostnamectl
Edit /etc/hosts to map the hostname to the loopback address so local
services resolve correctly:
127.0.1.1 server01.example.com server01