How to Configure and Manage Networks with Linux

Share This Post:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
linux-networking-meme

Unless you are a total security freak and air gap all of your computers, knowing how to configure and manage network connections with Linux is pretty much a necessity of your job as a system administrator or hacker, sorry penetration tester

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.

DistributionNetwork Configuration Locations
openSUSE/etc/sysconfig/network file
Debian/etc/network/interfaces file
Red Hat/etc/sysconfig/network-scripts directory
Since version 17.04, the Ubuntu distribution has deviated from the standard Debian method and utilizes the Netplan tool to manage network settings. Netplan uses simple YAML text files in the /etc/netplan folder to define the network settings for each network interface installed on the system. By default, Netplan just passes the network settings off to the NetworkManager tool, so you don’t need to worry about how the Netplan configuration files are set.

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.
wifi network connection linux
Screenshot of available wireless networks
wifi network settings configuration
Configuration screen for Linux wi fi network 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.
nmtui network manager text tool
nmui: Text based network management tool.
nmcli network manager
nmcli: Command line based network management tool

Legacy Network Management Tools

If your Linux distribution include one of the Network Manager tools mentioned above, there are a number of legacy command-line tools that available to you:
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.

ethtools linux
Output of the ethtool command.
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
The “ip” command  displays or sets the IP address, netmask, and router values for a network interface; it is becoming the more popular method for defining network settings from the command line. The table below shows a number of the more popular options associated with the command.
OptionDescription
addressDisplay or set the IPv4/IPv6 address for the device.
addrlabelDefine configuration labels.
l2tpTunnel ethernet over IP
linkDefine a network device.
maddressDefines a mulicas address for the system to listen to.
monitorWatch for netlink messages.
mrouteDefine an entry in the multicastt routing cache
mruleDefine a rule in the multicast routing policy database.
neighborManage ARP or NDISC cache entries.
netnsManage network namespaces.
ntableManage the neighbor cache operation.
routteManage routing tables.
ruleManages entries in the routing policy database.
tcpmetricsMange TCP metrics on the interface.
tokenManage tokenized interface identifiers.
tunnelTunnel over IP.
tuntapManage TUN/TAP devices
xfrmManage IPSec policies for secure connections.

The screenshot below shows the use of the “ip” command to display current network settings.

Ip address show command linux
The "ip address show" command displaying three network interfaces on the Linux system: lo, the local loopback interface, eth0, a wired network interface and wlan0, the wireless interface.

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.

iwlist scan command linux
Wireless network scan with the "iwlist" command.
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.

route command linux
Visualizing the default router configuration with the "route" command.

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.
 
 
Initialize network interface bonding, by loading the bonding module in the Linux kernel with the following command:
modprobe bonding
Which 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 4
Once 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 bond0
Linux 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.
ModeNameDescription
0balance-rrUses a round robin methodology to provide load balancing and fault tolerance.
1active-backupFault tolerance provided by one interface as the primary and the other as a back up.
2balance-xorLoad balancing and fault tolerance via transmitting on one interface and receiving on the second.
3broadcastUses both interfaces for transmitting
4802.3adAggregates the interfaces to create one connection combining interface bandwidths
5balance-tlbProvides load balancing and fault tolerance based on the current transmit load on each interface.
6balance-albProvides 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.

Ping command linux
Using the "ping" command to test network connectivity.

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.

host command linux
Using the "host" command to resolve hosts and IP addresses.

As seen in the screenshot below, the “dig” command displays all of the DNS data records associated with a specific host or network.

dig command linux

The image below shows how to use the “dig” command to investigate network services, in this case, Google’s mail servers.

Dig command MX
Using the "dig" command with the MX option.
The “nslookup” command looks up DNS information for multiple servers or domains and provides an interactive interface. The screenshot below shows the use of the command with www.microsoft.com and whitehouse.gov domains.
nslookup linux
Using the "nslookup" command in Linux.

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.”
netstat command linux
Using the "netstat" command to show open network connections.
netstat -l
Using the "netstat" command with the "-l" option to show which applications are listening on which ports.
netstat command-u -t
Use of the "-u" and "-t" options with the "netstat" command.
netstat -s command Linux
Use of the network command with the "-s" option to show network statistics.

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. 

Share This Post:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents

You May Like

Related Posts