Configuring Network Interfaces on Linux
This guide shows concrete commands and configuration examples for common Linux network setups. It focuses on modern tools: NetworkManager, systemd-networkd, and Ubuntu’s netplan frontend. Keep in mind distributions differ in defaults; adapt file paths accordingly.
Inspecting current state
Section titled “Inspecting current state”Check interfaces and addresses:
ip addr showip route showss -tulnCheck kernel messages and network service logs for device and driver issues:
sudo journalctl -k | tail -n 200sudo journalctl -u NetworkManager -n 200sudo dmesg | grep -i ethernetNetworkManager (desktop, dynamic setups)
Section titled “NetworkManager (desktop, dynamic setups)”Bring an interface up and request DHCP:
sudo nmcli device statussudo nmcli device connect eth0sudo nmcli device show eth0Create a static IPv4 connection using nmcli:
sudo nmcli connection add type ethernet ifname eth0 con-name static-eth0 ipv4.method manual ipv4.addresses 192.0.2.10/24 ipv4.gateway 192.0.2.1 ipv4.dns "8.8.8.8 1.1.1.1"sudo nmcli connection up static-eth0Use nmtui for an interactive TUI on systems with NetworkManager:
sudo nmtuisystemd-networkd (server, minimal setups)
Section titled “systemd-networkd (server, minimal setups)”Drop a .network file in /etc/systemd/network/ for DHCP:
[Match]Name=eth0
[Network]DHCP=ipv4For a static address:
[Match]Name=eth0
[Network]Address=192.0.2.20/24Gateway=192.0.2.1DNS=8.8.8.8Then restart the service:
sudo systemctl restart systemd-networkdsudo networkctl status eth0netplan (Ubuntu and derivatives)
Section titled “netplan (Ubuntu and derivatives)”Netplan YAML lives in /etc/netplan/ and translates to NetworkManager or systemd-networkd backends. Example static config:
network: version: 2 renderer: networkd ethernets: eth0: addresses: [192.0.2.30/24] gateway4: 192.0.2.1 nameservers: addresses: [8.8.8.8,1.1.1.1]Apply with:
sudo netplan applyDNS and resolvability
Section titled “DNS and resolvability”On systems using systemd-resolved, check status with:
systemd-resolve --statusresolvectl statusTemporary DNS test:
dig @8.8.8.8 example.com +shortping -c2 8.8.8.8Troubleshooting steps (practical checklist)
Section titled “Troubleshooting steps (practical checklist)”- Verify link:
cat /sys/class/net/eth0/operstateandethtool eth0. - Check driver/kernel messages:
dmesg | grep -i eth0andsudo journalctl -k. - Confirm IP and routes:
ip addr,ip route. - Test connectivity:
ping,traceroute, andcurl --interface eth0 https://ifconfig.co. - Capture packets for analysis:
sudo tcpdump -i eth0 -n -s 0 -w /tmp/cap.pcap. - Check for NetworkManager interfering when using
systemd-networkd(or vice versa) — ensure only one manager controls the interface.
Quick recovery recipes
Section titled “Quick recovery recipes”- If an interface does not come up after a configuration change, run
sudo ip link set dev eth0 downthensudo ip link set dev eth0 up, andsudo systemctl restart NetworkManagerorsystemctl restart systemd-networkdas relevant. - For flaky DHCP, clear leases (
/var/lib/NetworkManager/or DHCP client state) and request a fresh lease.
This guide is intentionally concrete — tell me which distribution(s) you want exact commands for (Debian/Ubuntu, Fedora, Arch) and我会补齐更精确的步骤与示例。