Linux Configure Network Step by Step Guide
In this article
Configuring a Linux network allows you to connect to the internet, set up static IPs, or manage local networks. This guide shows how to configure network settings using command-line tools and configuration files, suitable for most Linux distributions like Ubuntu, Debian, or CentOS.
What You'll Need
- A running Linux system with sudo access
- Network interface to configure (wired or wireless)
- Terminal access (e.g., GNOME Terminal, Konsole, or SSH)
- Internet connection (if setting up dynamic IP)
- Optional: Static IP details (IP address, subnet mask, gateway, DNS servers)
Step-by-Step
- Open your terminal application. Use
Ctrl + Alt + Ton most desktop environments or search for 'Terminal' in your app menu. - Check network interfaces with the command:
Look for the name of your network interface (e.g.,ip link showeth0for Ethernet orwlan0for wireless). This command will also show if the interface is up or down. If an interface is down, bring it up with:
Replacesudo ip link set eth0 upeth0with your interface name. - For dynamic IP (DHCP), use
dhclientto request an IP from a DHCP server:
Replacesudo dhclient eth0eth0with your interface name. This command requests an IP address from the network's DHCP server. If the command fails, it could be due to an incorrect interface name, a firewall block, or a lack of DHCP server on the network. - To set a static IP, open the configuration file with a text editor. For
netplan(used in Ubuntu 18.04+, Debian 11+), edit:
Add or modify the YAML configuration, for example:sudo nano /etc/netplan/01-netcfg.yaml
Replace the IP addresses and interface name as needed. YAML syntax is strict: incorrect indentation or formatting can prevent the file from working. Always validate with:network: version: : 2 ethernets: eth0: addresses: - 192.168.1.100/24 gateway4: 192.168.1.1 nameservers: addresses: - 8.8.8.8 - 8.8.4.4
This command checks the syntax before applying changes, preventing boot issues.netplan apply - Apply your changes with:
sudo netplan apply - For older systems using
interfaces(Debian/Ubuntu older versions), edit:
Add a static IP configuration like:sudo nano /etc/network/interfaces
Then restart the interface:auto eth0 iface eth0 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1 nameserver 8.8.8.8sudo systemctl restart networking - Verify your configuration with:
andip addr show
to ensure connectivity. If ping fails, check if the gateway is reachable with:ping -c 4 google.com
and test DNS resolution with:ping 192.168.1.1nslookup google.com
Common Mistakes
- Typing the interface name incorrectly (e.g.,
eth0vs.enp0s3). Useip linkto verify the exact name. - Using the wrong configuration file for the system (e.g., using
netplanwhen the system usesinterfaces). Check your distribution's documentation or usefind /etc/ -name "interfaces"orfind /etc/ -name "*.yaml"to locate the right file. - Forgetting to run
sudo netplan applyafter making changes. Netplan does not apply changes automatically unless you use thenetplan applycommand. - Incorrectly formatting YAML files (netplan), such as missing spaces, tabs, or invalid syntax. Use a YAML validator or run
netplan applyto catch errors before rebooting. - Not restarting the network service after updating configuration files. Run
sudo systemctl restart networkingorsudo systemctl restart NetworkManagerto activate changes.
Tips
- Use
ip routeto check routing table and verify gateway settings. Look for the default route withip route show. - Use
nmclifor NetworkManager-based setups (e.g., GNOME desktops) with:
This avoids editing configuration files directly and is useful for quick configuration changes.nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns 8.8.8.8 - Back up configuration files before editing them. Use
cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bakto create a backup. - Use
pingandtracerouteto test connectivity to internal and external IPs. For example,ping 192.168.1.1checks local network connectivity, whileping google.comchecks internet access.
Troubleshooting
When your network configuration doesn't work, follow these steps:
- Check for typos in your configuration file using
sudo nano /etc/netplan/01-netcfg.yamlorsudo nano /etc/network/interfaces. - Validate your YAML syntax in Netplan with:
If there's a syntax error, it will display the line and column. Fix the error before applying the changes.netplan apply - Ensure the interface is up:
sudo ip link set eth0 up - Check DHCP status with:
dhclient -v eth0 - Restart the network service:
orsudo systemctl restart NetworkManagersudo systemctl restart networking - If using NetworkManager, use:
nmcli device disconnect eth0 nmcli device connect eth0 - Check logs with:
orjournalctl -u networkingjournalctl -u NetworkManager - If all else fails, reboot the system:
sudo reboot
Wireless Configuration Example
For wireless interfaces using Netplan, you can configure them like this:network:
version: 2
wifis:
wlan0:
dhcp4: false
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
access-points:
'MyWiFi':
password: 'mypassword'
Apply the configuration with netplan apply, and confirm the interface is up with ip link show wlan0 and ip addr show wlan0.
Using NetworkManager from the Command Line
If your system uses NetworkManager, you can manage connections vianmcli. For example:
nmcli device status
This shows the current state of network interfaces. To connect to a wireless network:
nmcli device wifi connect 'MyWiFi' password 'mypassword' ifname wlan0
This is useful for scripting or when working in headless environments without a GUI.
Common Configuration Files and Locations
/etc/netplan/– Netplan YAML configuration files (Ubuntu 18.04+, Debian 11+)/etc/network/interfaces– Static IP configuration for older Debian/Ubuntu systems/etc/NetworkManager/– NetworkManager configuration files
Frequently Asked Questions
How do I find my current IP address on Linux?
Use ip addr show or hostname -I in the terminal.
Can I use a GUI to configure the network?
Yes, desktop environments like GNOME or KDE offer network settings in the system settings. Look for Network or Wi-Fi settings.
Is Netplan the same on all Linux distributions?
No. Netplan is used in Ubuntu and some Debian derivatives. Older systems may use interfaces or NetworkManager.
I changed my IP and now I can't connect. How do I revert?
Boot into a live USB, edit the configuration file with nano, or use a fallback IP if one was configured.
How do I set up a wireless connection?
Use nmcli for NetworkManager or configure netplan for wireless with SSID and password.
What if my system doesn't use Netplan?
Check your /etc/network/interfaces file for Debian-based systems or use nmcli for NetworkManager-based systems.