Networking
Networkmanager is the default in most Linux distros. Systemd has built in networking functions, but they are terrible to configure. This is why everyone uses networkmanager package for Networking in Linux. It has two absolutely amazing tools nmcli and nmtui.
nmcliis a command line tool that allows you to manage your network connections. You can use it to connect to Wi-Fi networks, configure Ethernet connections, and manage VPNs.nmtuiis a text-based user interface that allows you to manage your network connections. It is a bit easier to use thannmcli, but it is not as powerful.
Get Full List of Network Interfaces
To get basic ip information:
ip address
To get DNS, Routing, Gateway, and other information:
nmcli device show <interface>
For information on all interfaces managed by NetworkManager:
nmcli device status
Manage a network connections with nmcli
Below you find all the basic commands to manage your network connections with nmcli. The order of operations is important. You must create a connection before you can modify it, and you must modify it before you can activate it.
Important to note: conflicting network managers can cause issues. If you have systemd-networkd, netplan, dhcpcd, dnsmasq, dhclient, or wicd running, disable them. These are other network management services that might interfere with NetworkManager if they are running simultaneously.
To see a list of all network connections:
nmcli con show
To see a list of all available Wi-Fi networks:
nmcli device wifi list
To connect to a Wi-Fi network:
nmcli device wifi connect <SSID> password <PASSWORD>
To disconnect from a Wi-Fi network:
nmcli device disconnect <INTERFACE>
To delete a network connection:
nmcli con delete <CONNECTION_NAME>
To Add a new Ethernet connection:
nmcli con add type ethernet ifname <INTERFACE> con-name <CONNECTION_NAME>
To modify an existing connection:
nmcli con mod <CONNECTION_NAME> <SETTING> <VALUE>
To activate a connection:
nmcli con up <CONNECTION_NAME>
To deactivate a connection:
nmcli con down <CONNECTION_NAME>
You can make connections persistent across reboots by adding connection.autoconnect yes to the connection settings:
nmcli con mod <CONNECTION_NAME> connection.autoconnect yes
DHCP
DHCP Client to automatically get IP address and other network configuration settings from a DHCP server. Most Linux distributions come with a DHCP client pre-installed, so you don't need to install anything extra. To use DHCP, simply configure your network interface to use DHCP in the network settings. However, there are several DHCP clients available for Linux, including:
dhclient: This is the most common DHCP client and is included in most Linux distributions. Its rather slow, but it works.dhcpcd: This is a lightweight DHCP client that is full featured with many options. Its fast and works well.systemd-networkd: This is a systemd component that can be used to manage network interfaces and DHCP. Its fast and works well, but its also very difficult to configure.- NetworkManager internal DHCP client: This is the DHCP client that is built into NetworkManager. Its fast and works well, but simpler than dhcpcd.
Static IP Configuration
To configure a static IP address on a Linux system, you can use the nmcli or nmtui tools provided by NetworkManager. Here are the steps to configure a static IP address using nmcli:
- Open a terminal.
- List your network interfaces to identify the one you want to configure:
nmcli device status - Use the following command to set a static IP address, replacing
eth0with your interface name, and<IP_ADDRESS>with the desired static IP address:nmcli con mod eth0 ipv4.addresses <IP_ADDRESS>/24 nmcli con mod eth0 ipv4.gateway <GATEWAY_IP> nmcli con mod eth0 ipv4.dns <DNS_IP> nmcli con mod eth0 ipv4.method manual - Finally, restart the network connection to apply the changes:
nmcli con down eth0 && nmcli con up eth0 - For persistent configuration through reboots, ensure that the connection profile is set to autoconnect:
nmcli con mod eth0 connection.autoconnect yes
DNS Configuration
DNS (Domain Name System) is used to resolve domain names to IP addresses. By default, most Linux distributions use systemd-resolved for DNS resolution. You can configure DNS settings using nmcli or nmtui. Here are the steps to configure DNS settings using nmcli:
- Open a terminal.
- List your network connections to identify the one you want to configure:
nmcli con show - Use the following command to set the DNS server, replacing
eth0with your connection name, and<DNS_IP>with the desired DNS server IP address:nmcli con mod eth0 ipv4.dns <DNS_IP> - Restart the network connection to apply the changes:
nmcli con down eth0 && nmcli con up eth0
Managing Connections with nmtui
nmtui is a text-based user interface for managing network connections. It provides a simple way to configure network settings without using the command line. Here are the steps to manage connections using nmtui:
- Open a terminal.
- Type
nmtuiand press Enter to launch the interface. - Use the arrow keys to navigate the menu and select "Edit a connection".
- Select the connection you want to edit and press Enter.
- Use the arrow keys to navigate the fields and make changes as needed.
- Press Tab to navigate to the "OK" button and press Enter to save your changes.
- Press Tab to navigate to the "Back" button and press Enter to return to the main menu.
- Select "Activate a connection" to activate the modified connection.
- Press Tab to navigate to the "Quit" button and press Enter to exit
nmtui.
Note:Persistent configuration through reboots is automatic with nmtui, as it modifies the NetworkManager profiles directly.
Creating a Bridge with nmcli
A network bridge is a virtual network device that allows you to connect multiple network interfaces together. This is useful for creating a virtual network or for connecting multiple physical networks together. Here are the steps to create a bridge using nmcli:
- Open a terminal.
- Create a new bridge connection:
nmcli con add type bridge ifname br0 - Add a network interface to the bridge, replacing
eth0with the name of your network interface:nmcli con add type bridge-slave ifname eth0 master br0 - Configure the bridge with a static IP address, replacing
<IP_ADDRESS>,<GATEWAY_IP>, and<DNS_IP>with the desired values:
or to use DHCP:nmcli con mod br0 ipv4.addresses <IP_ADDRESS>/24 nmcli con mod br0 ipv4.gateway <GATEWAY_IP> nmcli con mod br0 ipv4.dns <DNS_IP> nmcli con mod br0 ipv4.method manualnmcli con mod br0 ipv4.method auto - Activate the bridge connection:
nmcli con up br0
Adding bridge to virtual machines with virsh
To add the bridge to a virtual machine using virsh globally, you can edit the default network configuration file. Here are the steps:
create a new XML file for the bridge network configuration, for example, bridge-network.xml:
<network>
<name>br0net</name>
<forward mode='bridge'/>
<bridge name='br0'/>
</network>
Note: the XML file can be anywhere and when you use net-define it will copy it to your libvirt network directory.
Define the new network using the XML file:
sudo virsh net-define bridge-network.xml
Start the new network:
sudo virsh net-start br0net
Set the network to start automatically on boot:
sudo virsh net-autostart br0net
Troubleshooting Network Issues
There is a ton of fragmentation in Linux networking, so troubleshooting can be difficult. Here are some common tools and commands that can help you troubleshoot network issues:
ping: This command is used to test connectivity to a remote host. You can use it to check if a host is reachable and to measure the round-trip time for packets.traceroute: This command is used to trace the route that packets take to reach a remote host. It can help you identify where packets are being dropped or delayed.ifconfigorip addr: These commands are used to display the network interfaces and their configuration. You can use them to check the IP address, subnet mask, and other settings for your network interfaces.netstatorss: These commands are used to display network connections, routing tables, and interface statistics. You can use them to check for open ports, active connections, and other network-related information.nmcli: This command is used to manage NetworkManager connections. You can use it to check the status of your network connections, modify settings, and troubleshoot issues.journalctl -u NetworkManager: This command is used to view the logs for NetworkManager. You can use it to check for errors and other messages related to your network connections.systemctl status NetworkManager: This command is used to check the status of the NetworkManager service. You can use it to see if the service is running and to check for any errors.resolvectl: This command is used to manage DNS settings and view DNS resolution status. You can use it to check the current DNS servers and troubleshoot DNS-related issues.ethtool <interface>: This command is used to display and modify Ethernet device settings. You can use it to check the link status, speed, and other settings for your network interfaces.
Disabling other network managers like systemd-networkd, netplan, dhcpcd, dnsmasq, dhclient, or wicd can also help resolve conflicts and ensure that NetworkManager functions correctly. These are other network management services that might interfere with NetworkManager if they are running simultaneously. DISABLE THEM.