Introduction / Why This Matters
Proper network configuration is the foundation of any Linux server or workstation. Without a correctly assigned IP address, gateway, and DNS servers, the system will be unable to communicate with other devices on the local network or the internet. This guide covers the practical aspects of configuration for both temporary changes (for testing) and permanent setup on major modern distributions.
After completing this guide, you will be able to:
- Manually assign IP addresses and routes.
- Understand which tool (
ip,nmcli,netplan) to use in your specific case. - Diagnose common network connectivity issues.
Prerequisites / Preparation
- Terminal access with
sudo(administrator) privileges. - Basic understanding of networking concepts: IP address, subnet mask, gateway, DNS.
- Knowledge of your network interface name. You can find it with the command:
Look for an entry withoutip link showLOOPBACK(usuallylo). Example names:eth0,ens33,enp0s3,wlan0. - Network information from your administrator or router:
- Desired IP address (or use DHCP).
- Subnet mask (e.g.,
255.255.255.0or/24). - Gateway address.
- DNS server addresses (e.g.,
8.8.8.8,1.1.1.1).
Step 1: Temporary Network Configuration with the ip Command
This method works instantly, but all changes will disappear after a reboot. Ideal for testing.
- Clear any existing address from the interface (e.g.,
eth0):sudo ip addr flush dev eth0 - Assign a new IP address and subnet mask:
Here,sudo ip addr add 192.168.1.100/24 dev eth0/24is the mask, equivalent to255.255.255.0. - Bring the interface up:
sudo ip link set eth0 up - Add the default route (gateway):
sudo ip route add default via 192.168.1.1 - Configure DNS temporarily by writing the servers to
/etc/resolv.conf(the file may be overwritten):echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf echo "nameserver 1.1.1.1" | sudo tee -a /etc/resolv.conf
⚠️ Important: For permanent configuration, you must use your distribution's configuration files (see the following steps).
Step 2: Permanent Configuration in Ubuntu/Debian (Netplan)
Starting with Ubuntu 17.10, Netplan is the default declarative configurator, which generates settings for systemd-networkd or NetworkManager.
- Locate the configuration file in
/etc/netplan/. It typically has a name like00-installer-config.yamlor01-netcfg.yaml. - Edit the file (e.g.,
sudo nano /etc/netplan/00-installer-config.yaml). Example configuration for a static IP:network: version: 2 renderer: networkd # or 'NetworkManager' for desktops ethernets: eth0: addresses: - 192.168.1.100/24 gateway4: 192.168.1.1 nameservers: addresses: - 8.8.8.8 - 1.1.1.1ethernets— the section for wired interfaces. For Wi-Fi, usewifis.- Ensure YAML indentation uses spaces, not tabs.
- Apply the configuration:
If you have a syntax error, the command will report it.sudo netplan apply
Step 3: Permanent Configuration via NetworkManager (nmcli)
NetworkManager is a popular daemon often used in desktop systems (Ubuntu, Fedora, RHEL). You can manage it via the nmcli CLI utility.
- Check the name of an existing connection (if any):
nmcli connection show - Create a new connection (or modify an existing one). To create:
sudo nmcli connection add type ethernet con-name static-eth ifname eth0 ip4 192.168.1.100/24 gw4 192.168.1.1 - Add DNS servers to this connection:
sudo nmcli connection modify static-eth ipv4.dns "8.8.8.8 1.1.1.1" - Apply the settings and activate the connection:
sudo nmcli connection up static-eth - To make the connection auto-connect at boot, ensure the
connection.autoconnectflag is set toyes(this is the default).
💡 Tip: For Wi-Fi, use
type wifiand add parameterswifi-sec.key-mgmt wpa-pskandwifi-sec.psk "password".
Step 4: Configuration in RHEL/CentOS 8+ (systemd-networkd)
Minimal installations of the RHEL family often use systemd-networkd.
- Create or edit the interface configuration file in
/etc/systemd/network/. The filename must end with.network(e.g.,20-wired.network).[Match] Name=eth0 [Network] Address=192.168.1.100/24 Gateway=192.168.1.1 DNS=8.8.8.8 DNS=1.1.1.1 - Restart the service and reload the network:
sudo systemctl restart systemd-networkd sudo systemctl restart systemd-resolved # for DNS - Enable the services to start at boot (usually already enabled):
sudo systemctl enable systemd-networkd
Verifying the Result
- Ensure the interface has the correct IP:
The output should include a line with your addressip addr show eth0inet 192.168.1.100/24. - Check the routing table:
There should be an entryip route showdefault via 192.168.1.1 dev eth0. - Test internet connectivity:
If pings succeed, network connectivity is working.ping -c 4 8.8.8.8 - Test DNS name resolution:
ornslookup google.com
An IP address should be returned.dig google.com
Common Issues
Operation not permittedorPermission denied: You forgot to usesudo.RTNETLINK answers: File exists: Attempting to add a duplicate IP address. Flush the interface (ip addr flush) or use a different address.- Interface does not come up after configuration (
state DOWN):- Check if the cable is physically connected (for wired) or if Wi-Fi is enabled (laptop button/switch,
rfkill list). - Verify YAML syntax in netplan (
sudo netplan try). - Check logs:
journalctl -u systemd-networkdorjournalctl -u NetworkManager.
- Check if the cable is physically connected (for wired) or if Wi-Fi is enabled (laptop button/switch,
- No internet access, but
ping 8.8.8.8works: DNS issue. Check/etc/resolv.confcontents and DNS settings in your configuration file (netplan/nmcli). - IP address conflict: Ensure the chosen address is not already in use by another device on the network. Use
arp-scanor check the DHCP lease table on your router.