Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux Networking Configuration: Step-by-Step Setup Guide

· 5 min read · Home Networking

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Advanced Network Configuration
  7. Security and Best Practices
  8. Frequently Asked Questions

Networking configuration in Linux enables your system to connect to local networks, the internet, or other devices. This guide provides a hands-on approach to configuring network interfaces, ensuring your Linux machine can communicate effectively. You'll need root access and a basic understanding of command-line tools.

What You'll Need

  • Root access or sudo privileges
  • A running Linux system (e.g., Ubuntu, Debian, CentOS, RHEL)
  • Internet access or access to network devices like routers
  • Basic familiarity with command-line tools like ip, nmcli, or netplan

Step-by-Step

  1. Identify the network interface: Run ip link or ls /sys/class/net to list available interfaces. Common names include eth0, enpXsY, or wlan0. For example, on a system with a single Ethernet and Wi-Fi interface, you might see eth0 and wlan0. Make a note of the interface you want to configure.
  2. Check current configuration: Use ip addr to see current IP settings and ip route to show routing tables. This helps you verify if the system is already connected or if changes are needed. For instance, ip addr show eth0 will display the IP address, subnet mask, and MAC address of the selected interface.
  3. Edit configuration files:
    • Debian/Ubuntu (netplan): Edit /etc/netplan/01-netcfg.yaml with a text editor like nano. For example, open the file with sudo nano /etc/netplan/01-netcfg.yaml and modify the YAML structure accordingly.
    • RHEL/CentOS (NetworkManager): Use nmcli or nmtui for GUI-based setup. For example, nmcli dev list provides detailed device information that can be used to configure settings.
    • Legacy systems (ifcfg files): Edit /etc/sysconfig/network-scripts/ifcfg-eth0 or similar files. Open the file with a text editor and set options like BOOTPROTO=static, IPADDR=192.168.1.50, and NETMASK=255.255.255.0.
  4. Set static IP (example for netplan):
        network:
          version: 2
          ethernets:
            eth0:
              addresses:
                - 192.168.1.50/24
              gateway4: 192.168.1.1
              nameservers:
                addresses:
                  - 8.8.8.8
                  - 8.8.4.4
        
    This example sets a static IP of 192.168.1.50/24, a default gateway at 192.168.1.1, and DNS servers at Google’s public DNS servers.
  5. Apply configuration:
    • Netplan: sudo netplan apply
    • NetworkManager: sudo nmcli connection reload and sudo nmcli connection up id "Your Connection"
    • Legacy ifcfg files: sudo systemctl restart NetworkManager or sudo systemctl restart networking
  6. Verify connectivity: Use ping 8.8.8.8 to test internet access and ip route to confirm routing. If the ping returns data, the system has internet connectivity and the configuration is successful.

Common Mistakes

1. Incorrect syntax in config files: Netplan and ifcfg files are YAML or shell scripts; a single indentation or missing dash can break the setup. Always validate YAML with netplan try before applying changes.

2. Using the wrong interface name: Confirm the interface name with ip link before applying changes. A mismatched interface name can lead to failed configurations.

3. Not restarting services: After editing files, always restart NetworkManager or apply netplan changes. If the configuration is not applied, the system may still use old settings.

4. Forgetting DNS settings: Even with a correct IP and gateway, DNS misconfiguration can prevent domain resolution. Always test DNS with nslookup example.com or dig example.com.

Tips

1. Always test your configuration before committing it to production systems. Use netplan try to preview changes and ensure the system does not lose connectivity.

2. Use nmcli dev list for NetworkManager to get detailed device info. This helps identify the correct interface and device properties.

3. For dynamic IP (DHCP), set dhcp4: yes in netplan or BOOTPROTO=dhcp in ifcfg files. This ensures the system can automatically obtain an IP from a DHCP server.

Troubleshooting

If configuration fails:

  1. Check for typos in the YAML or ifcfg file using netplan try or nmcli dev status. Syntax errors are the most common cause of configuration issues.
  2. Run systemctl status NetworkManager or journalctl -u NetworkManager to view logs. These logs can provide details about failed connections or misconfigurations.
  3. Use ip link and ip addr to confirm interface is up and has the correct settings. If the interface is down, bring it up with ip link set eth0 up.
  4. If using DHCP, confirm the DHCP server is reachable and working with dhclient eth0 (or similar). This tests if the system can obtain an IP from a DHCP server.

Advanced Network Configuration

For advanced scenarios like VLANs, bonding, or bridging, additional configuration is required. For example, to set up a VLAN interface:

network:
  version: 2
  ethernets:
    eth0:
      addresses: []
      dhcp4: no
  vlans:
    eth0.10:
      id: 10
      link: eth0
      addresses:
        - 192.168.10.50/24
      gateway4: 192.168.10.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

This example creates a VLAN interface eth0.10 with a specific IP and gateway. After editing the netplan file, apply it with sudo netplan apply.

Security and Best Practices

1. Always back up configuration files before modifying them. For example, run sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak to create a backup.

2. Avoid using hardcoded DNS servers in production environments. Instead, use DHCP to automatically assign DNS servers from the router or network.

3. Use firewall rules (like ufw or iptables) to control traffic and protect the system from unauthorized access. For example, use sudo ufw allow 22/tcp to allow SSH traffic.

Frequently Asked Questions

How do I configure a static IP on Ubuntu?

Edit the netplan YAML file with the desired IP, gateway, and DNS settings, then run sudo netplan apply.

Can I use NetworkManager on RHEL?

Yes, RHEL 8+ uses NetworkManager by default. Use nmcli or nmtui to configure interfaces.

Why is my interface not getting an IP?

Check if the interface is up with ip link show and verify DHCP is enabled in your configuration file.

How do I test DNS resolution?

Use nslookup google.com or dig google.com to confirm DNS is working as expected.

What if my YAML file has an error?

Run netplan try to validate the file before applying it. Any syntax issues will be reported.