Skip to main content

Managing Network Connections

1. Bringing Interfaces Up and Down

The first step in managing network connections is ensuring that network interfaces are active. You can bring interfaces up and down using several commands, including ifup, ifdown, and the more modern ip command.

1.1 Using the ip Command

The ip command is the modern utility for network management on Linux systems. It allows you to activate or deactivate network interfaces.

Examples:

1. Bring an Interface Up (Activate it)

ip link set eth0 up

This command brings the interface eth0 up, activating the network connection.

2. Bring an Interface Down (Deactivate it)

ip link set eth0 down

This command disables the eth0 interface, disconnecting it from the network.


1.2 Using the ifup and ifdown Commands

On older systems, you can use the ifup and ifdown commands to manage network interfaces. These commands are used to bring interfaces up or down based on the configuration file in /etc/network/interfaces.

Examples:

1. Bring an Interface Up

ifup eth0
  • Activates the eth0 interface according to the configuration in /etc/network/interfaces.

2. Bring an Interface Down

ifdown eth0
  • Deactivates the eth0 interface, disconnecting it from the network.

Note: The ifup and ifdown commands are legacy tools and are generally replaced by ip or nmcli in modern Linux distributions.


2. Managing Network Connections with nmcli

The nmcli tool is part of NetworkManager, a utility used for managing network connections, including Ethernet, Wi-Fi, and VPN. It is widely used on systems with a graphical user interface (GUI) but can also be used on headless systems (without a GUI).

2.1 Viewing Available Network Connections

To list all available network connections on your system, you can use the nmcli connection command.

Example: List All Connections

nmcli connection show
  • Displays all available network connections, including Ethernet and Wi-Fi interfaces.

2.2 Connecting to a Wired Network

To connect to a wired network (Ethernet), use the following command:

Example: Connect to a Wired Network

nmcli connection up eth0
  • Activates the Ethernet interface eth0 and establishes a connection.

2.3 Connecting to a Wi-Fi Network

To connect to a Wi-Fi network, use the nmcli command with the wifi option.

Examples:

1. Scan for Available Wi-Fi Networks

nmcli device wifi list
  • Lists all available Wi-Fi networks that your system can detect.

2. Connect to a Wi-Fi Network

nmcli device wifi connect "MyWiFiNetwork" password "MyWiFiPassword"
  • Connects to the Wi-Fi network MyWiFiNetwork using the password MyWiFiPassword.

Note: Make sure the Wi-Fi interface (wlan0 or similar) is enabled before attempting to connect to Wi-Fi.

2.4 Disconnecting from a Network

To disconnect from a network (either Ethernet or Wi-Fi), you can use the following command:

Examples:

1. Disconnect from a Network

nmcli connection down eth0
  • Disconnects the interface eth0 from the network.

2. Disconnect from a Wi-Fi Network

nmcli device disconnect wlan0
  • Disconnects the Wi-Fi interface wlan0.

3. Managing Network Interfaces Using ifconfig

ifconfig is an older, but still commonly used, command for configuring network interfaces. While it is deprecated in many distributions, it remains useful for basic tasks like checking network interface status and assigning IP addresses.

3.1 Viewing Network Interface Details with ifconfig

Example: Display All Network Interfaces

ifconfig
  • Displays all network interfaces, their IP addresses, and status information.

3.2 Enabling or Disabling an Interface with ifconfig

Examples:

1. Enable an Interface

ifconfig eth0 up
  • Brings the eth0 interface up, activating it.

2. Disable an Interface

ifconfig eth0 down
  • Brings the eth0 interface down, deactivating it.

Note: As mentioned earlier, ifconfig is an older tool, and it is recommended to use the ip command for newer systems.


4. Troubleshooting Network Connections

Network issues are common, and sometimes manual troubleshooting is necessary. Below are a few common troubleshooting tips and commands.

4.1 Check IP Address Assignment

If a system is not getting an IP address, run the following command to check the current assignment.

Example: Check IP Address

ip addr show eth0
  • Displays the current IP address assigned to eth0. If there’s no IP address, you might need to request one from the DHCP server.

4.2 Renew DHCP Lease

If the IP address was not assigned or has expired, you can use the following command to request a new one.

Example: Renew DHCP Lease

dhclient eth0
  • Requests a new IP address for the eth0 interface via DHCP.

4.3 Check Default Gateway

If you can't access the internet, it may be an issue with your default gateway. Check your default route with:

Example: Display Routing Table

ip route show
  • Verifies if the default gateway is correctly set. If not, add it manually.

4.4 Check DNS Resolution

If DNS is not working, you may not be able to access websites by domain name. Verify DNS resolution by testing with ping:

Example: Check DNS Resolution

ping google.com
  • If this command returns an error, you may need to configure DNS settings properly in /etc/resolv.conf.

5. Quick Recap: Key Commands

CommandPurpose
ip link set eth0 upBring the eth0 interface up.
ip link set eth0 downBring the eth0 interface down.
nmcli connection up eth0Activate the eth0 interface using NetworkManager.
nmcli device wifi listList available Wi-Fi networks.
nmcli device wifi connect <SSID> password <password>Connect to a Wi-Fi network.
nmcli connection down eth0Disconnect from the network for interface eth0.
ifconfigDisplay all network interfaces and their configurations.
dhclient eth0Request a new IP address via DHCP for the eth0 interface.

Key Takeaways:

  • Use ip for network interface management and routing.
  • Use nmcli for advanced network connection management, especially on systems with NetworkManager.
  • Basic tools like ifconfig and dhclient are still useful in many environments but are being replaced by more modern commands.