How to Configure and Manage Networks Connections with Linux
There are five main configuration settings you need to manage in a Linux system in order to connect it to a network:
- The host address
- The network subnet address
- The default router/ gateway
- The system hostname
- A DNS server address that is used to resolve hostnames
How Do Network Configuration Files Work
Linux distribution uses network configuration files to define the network settings in order to communicate on the network. There are three different ways to configure this information in Linux systems:
- Manually editing network configuration files.
- Using a graphical tool included with your Linux distribution.
- Using command-line tools.
But just like everything else in Linux-world, there’s not a single standard configuration file that all distributions use. While they all have similar features, such as defining each of the required network settings as separate values in the configuration file, each Linux distributions uses different configuration files and method for defining the network settings. The configuration file locations for different distributions can be seen in the table below.
Distribution | Network Configuration Locations |
---|---|
openSUSE | /etc/sysconfig/network file |
Debian | /etc/network/interfaces file |
Red Hat | /etc/sysconfig/network-scripts directory |
You will also need to define a DNS server so that the system can use DNS hostnames. Fortunately, this is a standard that all Linux systems follow and is handled in the /etc/resolv.conf configuration file.
Graphical Tools for Managing Networks with Linux
Many Linux distributions use the Network Manager tool to provide a graphical interface for showing and managing network connections; it starts automatically at boot time and appears in the system tray area of the desktop as an icon.
- If your system detects a wired network connection, the icon appears as a mini-network with blocks connected together.
- If your system detects a wireless network connection, the icon appears as an empty radio signal.
- Clicking the icon shows a list of the wireless networks detected by the network card.
- If an access point is encrypted, you get prompted to enter the password to access the network.
Once connected to a wireless access point, the icon appears as a radio signal; click the icon, and select Wi-Fi setting to modify the network connection settings for the system as shown in the two screenshots below. You can select the network connection to configure (either wireless or wired) and then click the Edit button to change the current configuration. The Network Manager tool:
- Allows you to specify all four of the network configuration values by using the manual configuration option or to set the configuration to use DHCP to determine the settings.
- Automatically updates the appropriate network configuration files with the updated settings.
Command Line Tools for Managing Networks with Linux
Linux Network Manager tool also provides two different types of command-line tools to guide you through the process of setting the required network information for your Linux system. :
- nmtui: Provides a simple text-based menu tool where you can select a network interface and assign network properties to it. You can see it in the first screen shot below.
- nmcli: Provides a text based command-line tool you can see in the second screen shot below. The nmcli command uses command-line options to allow you to set the network settings.
Legacy Network Management Tools
ethtool
Displays ethernet settings for a network interface and allows you to view/modify the network interface card Ethernet settings/properties that you may need to communicate with network devices. As seen in the screenshot below, the ethtool command displays the current configuration settings for the network interface by default. You can change features such as speed, duplex, and whether or not the network interfac. eattempts to auto-negotiate features with the switch.
ifconfig
ifconfig displays or sets the IP address and netmask values for a network interface and allows you to set the network address and subnet mask for a network interface. You can read more about using the command with
ip
Option | Description |
---|---|
address | Display or set the IPv4/IPv6 address for the device. |
addrlabel | Define configuration labels. |
l2tp | Tunnel ethernet over IP |
link | Define a network device. |
maddress | Defines a mulicas address for the system to listen to. |
monitor | Watch for netlink messages. |
mroute | Define an entry in the multicastt routing cache |
mrule | Define a rule in the multicast routing policy database. |
neighbor | Manage ARP or NDISC cache entries. |
netns | Manage network namespaces. |
ntable | Manage the neighbor cache operation. |
routte | Manage routing tables. |
rule | Manages entries in the routing policy database. |
tcpmetrics | Mange TCP metrics on the interface. |
token | Manage tokenized interface identifiers. |
tunnel | Tunnel over IP. |
tuntap | Manage TUN/TAP devices |
xfrm | Manage IPSec policies for secure connections. |
The screenshot below shows the use of the “ip” command to display current network settings.
Looking at the image above, you see a number of items worth knowing about
- The local loopback interface is a special virtual network interface. Any local program can use it to communicate with other programs just as if they were across a network. That can simplify transferring data between programs.
- The eth0 network interface is the wired network connection for the Linux system. The ip command shows the IP address assigned to the interface (there’s both an IP and an IPv6 link local address assigned) and some basic statistics about the packets on the interface.
- The wlan0 network interface is the wired network connection for the Linux system. The ip command shows the IP address assigned to the interface (there’s both an IP and an IPv6 link local address assigned) and some basic statistics about the packets on the interface.
If the output doesn’t show a network address assigned to the interface, you can use the ip command to specify the host address and netmask values for the interface:
ip address add 10.0.2.2/24 dev eth0
Then use the ip command to set the default router for the network interface:
ip route add default via 192.168.1.1 dev eth0
Then finally, make the network interface active by using the link option:
ip link set eth0 up
iwconfig
Before you can use the ip command to assign an address to a wireless interface use the iwconfig command to assign the wireless SSID and encryption key values with the following command:
iwconfig wlp6s0 essid “MyNetwork” key s:mypassword
- The essid parameter specifies the access point SSID name,
- the key parameter specifies the encryption key required to connect to it. Notice that the encryption key is preceded by an s:. That allows you to specify the encryption key in ASCII text characters; otherwise you’ll need to specify the key using hexadecimal values.
You can use the “iwlist” command to display all of the wireless signals your wireless card detects if you don’t know the name of a local wireless connection; simply specify the name of the wireless device, and use the scan option
iwlist wlano scan
The partial output of this command can be seen in the screenshot below.
route
The route command sets the default router address and is alternative to the “ip” command for changing network settings and its use can be seen in the image below.
If you want to change the routing settings for your network by say, modifying the default gateway, use the “route” command, with the following syntax:
route add default gw <IP Address>
The default router defined for the Linux system (running on a virtual machine) is 0.0.0.0 and is available from the eth0 and wlan0 network interfaces. The output also shows that to get to either 192.168.0.0 or 10.0.2.0 networks, you don’t need a gateway because these are the local networks to which the Linux system is connected. While these entries were created automatically, you could manually create the routing table in the system by using the add or del command-line option for the route command with the following command where <target> is the target host or network and <gateway> is the router address:
route [add] [del] <target> gw <gateway>
Additionally, you can fine-tune network interface parameters in the “/etc/sysctl.conf” configuration file, which defines kernel parameters Linux uses interacting with network interfaces. This has become a popular method to use for setting advanced security features like disabling:
- Responding to ICMP messages by setting the “icmp_echo_ignore_broadcasts” value to 1.
- Packet forwarding by setting “the ip_forward” value to 0.
If you are on a network which uses DHCP, ensure your Linux system has a proper DHCP client running to communicate with the DHCP server in the background and assigns the necessary IP address settings. as directed by the DHCP server. There are three main DHCP programs available for Linux systems:
- dhcpcd: becoming the most popular of the three, but you’ll still see the other two used in some Linux distributions.
- dhclient
- pump
If your Linux server that acts as a DHCP server, the “/etc/dhcpd.conf” file contains the IP address settings that the server offers to DHCP clients. The file contains a section for each subnet the DHCP server services
Bonding is also a network configuration you may need to manage as it has to do with network interface bonding, which allows the aggregation of multiple interfaces into a single virtual network device you can manage using three different bonding types:
- Load balancing: Network traffic is shared between two or more network interfaces. Aggregation: Two or more network interfaces are combined to create one larger net-
- work pipe.
- Active/passive: One network interface is live while the other is used as a backup for fault tolerance.
modprobe bondingWhich creates the bond0 network interface, which you then use with the “ip” utility to define the bond type:
ip link add bond0 type bond mode 4Once defined, use the “ip” utility to add the appropriate network interfaces to the bond:
ip link set eth0 master bond0 $ sudo ip link set eth1 master bond0Linux then treats the bond0 device as a single network interface and utilizes the load balancing or aggregation method you defined. The table below shows the 7 bonding modes available on Linux systems.
Mode | Name | Description |
---|---|---|
0 | balance-rr | Uses a round robin methodology to provide load balancing and fault tolerance. |
1 | active-backup | Fault tolerance provided by one interface as the primary and the other as a back up. |
2 | balance-xor | Load balancing and fault tolerance via transmitting on one interface and receiving on the second. |
3 | broadcast | Uses both interfaces for transmitting |
4 | 802.3ad | Aggregates the interfaces to create one connection combining interface bandwidths |
5 | balance-tlb | Provides load balancing and fault tolerance based on the current transmit load on each interface. |
6 | balance-alb | Provides load balancing and fault tolerances based on the current receive load on each interface. |
How To Troubleshoot Networks with Linux
As anyone who has ever turned on a computer knows, from time to time they may not work as expected an you may need to troubleshoot problems with both the computer and the network. When this happens you should:
- Follow a troubleshooting methodology.
- Execute the network activity monitoring commands we discuss below so you know:
- What processes are listening on the network
- Which connections are active from your system.
Send Some Test Packets Across the Wire
The most basic way to test network connectivity is to send test packets to known hosts. Linux provides the “ping/ping6” commands to do that. The “ping/ping6” commands send Internet Control Message Protocol (ICMP) packets, normally used to track connectivity and control messaging, to remote hosts using either the IP (ping) or IPv6 (ping6) protocol. If the remote host supports ICMP, it will send a reply packet back when it receives a ping packet. Be aware some systems disable ping responses as a security measure.
Finding Host Information On Linux
Sometimes your connectivity issues are due to problems with the DNS hostname system; test a hostname using the “host” command which queries the DNS server, determining the IP address(es) assigned to a specific hostname; as seen in the first two examples in the screen shot below where we use the command with www.google.com/google.com, it returns all IP addresses associated with the hostname and you will see that some hosts are supported by multiple servers in a load balancing configuration as well as mail and IPv6 servers.
The third, fourth and fifth examples in the image below demonstrate that you can specify an IP address for the “host” command, and it will attempt to find the hostname associated with it. Keep in mind that often an IP address resolves to a generic server hostname that hosts the website, but not the website alias.
As seen in the screenshot below, the “dig” command displays all of the DNS data records associated with a specific host or network.
The image below shows how to use the “dig” command to investigate network services, in this case, Google’s mail servers.
The “netstat” command, part of the net-tools package, generates a lot of output because there are a lot of programs using network services on Linux systems. The screenshots below show:
- That by default, it lists all of the open network connections on the system.
- The “–l” option gives you list of applications listening on the various network ports.
- Limit the output to just TCP or UDP connections by using:
- The “–t” command-line option for TCP connections
- The “–u ” for UDP connections.
- The “–s” option displays statistics for the different types of packets the system has used on the network, giving you an idea of your Linux system’s network activity or if there’s a specific issue with one of the protocols installed.”
The “ss” command determine which program is listening on which open port. A program connection to a port is called a socket and the “ss” command links which system processes are using which active network sockets. When the “ss” command is used with the “-anpt” option, it displays:
- Listening and established TCP connections
- The process they’re associated with.