Tools for configuring and troubleshooting network connectivity

Lead Image © Dietmar Hoepfl, 123RF.com

Connections

,

The Linux command line provides a powerful collection of utilities for configuring and troubleshooting network connections. This article rounds up some new and old networking commands.

Linux and other Unix-based systems often offer several alternatives for solving a single problem. Although this approach can be confusing to some novice administrators, it is one of the defining features of the open source environment.

The networking tool collection is no exception to this practice. You'll find an array of useful tools – some overlapping and some unique – for configuring, managing, and troubleshooting network connections.

In this article, we highlight some favorite tools in the networking collection. Of course, a full description of the complete TCP/IP networking environment could fill a very long book. Here, we assume you have some basic knowledge of TCP/IP networking concepts such as routing, addressing, and name resolution.

Interfaces

The ifconfig command was, and still is on many systems, the default tool for configuring network interfaces. However, ifconfig is often considered obsolete, in that newer tools are provided for systems running kernels newer than 2.0. The ifconfig command is still available as part of the net-tools package, however, and in all likelihood, it is automatically installed on your system.

On newer Linux systems, you also get the ip command. More than just a newer version of ifconfig, ip is the workhorse of the new generation of network tools. Not only does it integrate the functionality of several older tools, but ip also provides a unified syntax across all the various functions. In contrast, the utilities provided by the net-tools packages are a patchwork collection of tools that were developed individually over many years. (See also the "ip and ifconfig" box.)

ip and ifconfig

The ip commands are typically more complex than their ifconfig equivalents. For example, to display the configuration of the eth0 interface, the ifconfig command is simply ifconfig eth0.

The ip command is part of the iproute package. The similarity between the tools in this package helps you master the configuration of your network more quickly because you do not need to learn different syntax options for different functions. Furthermore, you don't need to remember which utility does what because, for the most part, ip integrates the capabilities of ifconfig, route, and arp into a single tool.

The generic usage is

ip [OPTIONS] <OBJECT> [COMMAND]

where OBJECT is something like ip for your IP configuration, link for a network interface, addr for your IP address, route for routes, and so forth. (The ip command also supports several other objects – see the ip man page for more details).

In the context of the ip command, a "link" is a network device, real or virtual, and to display the details of a specific interface, you might enter the following:

ip addr show dev eth0

This command might give you something like the output shown in Figure 1.

Figure 1: Output of the ip addr command.

In most cases, the default argument is show, which displays the basic parameters of the given object. The default behavior is to display the information for all objects if none is specified. For example, ip addr will show (i.e., display) the address information about all network interfaces. If you want, you can use list instead of show. (Most users think about "listing" devices rather than "showing" them.)

This form of the ip addr command is composed of three parts: show dev eth0. You could say that the command-within-a-command is show with dev eth0 acting as arguments.

If you want to add a virtual interface called eth0:1, the command would look like:

ip addr add 192.168.1.42 dev eth0:1

In this case, you can think of 192.168.1.42 dev eth0:1 as arguments to the add command. This example adds the IP address 192.168.1.42 to the device eth0:1.

With the ip command, you can also enable and disable interfaces (i.e., bring them up or down):

ip link set up dev eth1

In this example, the command is set; set and view are the two options the link object accepts.

The ifup command is another option for starting up a network interface. This command is actually a shell script that is used by the /etc/init.d/network file to bring up the interface when the system first boots, although you can also run it manually. As you would expect, there is also an ifdown command, which is a symbolic link to ifup.

If you call the ifup man page, you will find several other links to ifup, one of which is ifrenew. This command is used to renew the DHCP lease on the specified interface. However, it does not actually shut down and restart the interface. Another useful command is ifstatus, which, as you might guess, provides status information on the given interface.

The ifstatus command provides significantly more information than ip addr show. As you can see in Figure 2, ifstatus outputs information on routes configured on the device, as well as other important details.

Figure 2: Viewing status information with ifstatus.

Routing

If you were to imagine network traffic as something like a letter that you're sending to a friend, you would know their name (hostname) and address (IP address), and all you would need to do to get the letter to your friend is address it, put a stamp on it, and stick it in the mailbox (gateway). However, between your gateway and the friend's gateway, the letter has to be routed between you and your friend.

The post office maintains a set of routes to deliver letters, and hosts on the Internet maintain routing tables to send packets to and fro. Usually, the only route you have to worry about is the default gateway for your system. On your home LAN, this is the modem or DSL router that connects you to the Internet. For example, on my home network, the default route is 192.168.1.254.

The need to define network routes manually has decreased through the years. Most home and office networks today provide dynamic IP address assignment through DHCP, which includes information on the default gateway for the network.

Even if you explicitly define your network configuration – at installation or through the resident configuration utility for your Linux distribution – the tool typically asks you to specify a default gateway, and the rest of the routing happens automatically.

However, for complex network configurations, such as computers with multiple network interfaces or routed networks with multiple paths, you might occasionally need to add information directly to the routing table – for configuration, optimization, or troubleshooting purposes. If your system loses or doesn't have a default route and it isn't a gateway, then you're not going to be delivering any packets.

The traditional method for adding and managing routes is the route command. The ip route command is a more recent alternative with similar functionality. Adding a route with the route command will look something like this:

route add -net 192.168.42.0/24 gw 192.168.1.99

Doing the same thing with the ip command would look like:

ip route add 192.168.42.0/24 via 192.168.1.99

As you can see, the format is basically the same as when you added IP addresses. In this case, the object is a route and the command is add.

Note that both commands add the route for a range of IP addresses (192.168.42.0/24 – in CIDR format), and this route is assigned to a router address – with the gw ("gateway") argument in the route command and the more intuitive via with ip route.

If you enter the ip route command without any modifying arguments, you are shown the list of configured routes. Although this is not any simpler than running route, I think the output is a little more useful. For example, the output for the default route shown by ip is:

default via 192.168.2.1 dev eth0

whereas the output for route is more elaborate (Figure 3).

Figure 3: The route command displays routing table entries.

With the use of route and ip, you can hand-configure routes aside from the gateway. Suppose, for example, that you have two interfaces on your machine and want to ensure that the eth1 interface is used for the 192.168.42.0/24 network:

route add -net 192.168.42.0 netmask 255.255.255.0 gw 192.168.1.254 dev eth1

Now traffic headed to 192.168.42.0 will go through eth1. One caveat – this all goes away when you reboot. Static routes set by hand are not persistent by default. The kernel will "forget" everything unless you make it permanent. How do you make the routes permanent? This is, unfortunately, a bit more complicated. Different distributions set their network configurations differently – and have different tools for configuring the network configuration.

For example, if you're using Red Hat or Fedora, you will find networking scripts under /etc/sysconfig/network-scripts, whereas Debian-based systems keep their information under /etc/network/interfaces. If you're using a desktop system, you might want to use Network Manager to make changes rather than using text files.

Users on openSUSE or SUSE Linux Enterprise Systems should use YaST2 to make changes. Bottom line: If you need to set up persistent routes, you'll probably want to consult your distribution's documentation.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content