Troubleshooting network0related issues with Linux is a complex topic and could easily fill its own book. we will introduce some key troubleshooting tools and the basics of their use.
There is substantial overlap in the tools that we describe, so you may find learning about some tools (or tool features) redundant. Some are better suited to a given task than others (for example, multiple tools will catch TLS errors, but OpenSSL provides the richest debugging information).
Exact tool use may come down to preference, familiarity, and availability.
Case | Tools |
---|---|
Checking connectivity | traceroute , ping , telnet , netcat |
Port scanning | nmap |
Checking DNS records | dig , commands mentioned in “Checking Connectivity” |
Checking HTTP/1 | cURL , telnet , netcat |
Checking HTTPS | OpenSSL , cURL |
Checking listening programs | netstat |
Some networking tools that we describe likely won’t be preinstalled in your distro of choice, but all should be available through your distro’s package manager. We will sometimes use # Truncated
in command output where we have omitted text to avoid examples becoming repetitive or overly long.
Security Warning
Before we get into tooling details, we got to notice about some security warning. An attacker can utilize any tool listed here in order to explore and access additional systems. There are many strong opinions on this topic, but we consider it best practice to leave the fewest possible networking tools installed on a given machine.
An attacker may still be able to download tools themselves (e.g., by downloading a binary from the internet) or use the standard package manager (if they have sufficient permission). In most cases, you are simply introducing some additional friction prior to exploring and exploiting. However, in some cases you can reduce an attacker’s capabilities by not preinstalling networking tools.
Linux file permissions include something called the setuid bit that is sometimes used by networking tools. If a file has the setuid bit set, executing said file causes the file to be executed as the user who owns the file, rather than the current user. You can observe this by looking for an s
rather than an x
in the permission readout of a file:
This allows programs to expose limited, privileged capabilities (for example, passwd uses this ability to allow a user to update their password, without allowing arbitrary writes to the password file). A number of networking tools (ping, nmap, etc.) may use the setuid bit on some systems to send raw packets, sniff packets, etc.
If an attacker downloads their own copy of a tool and cannot gain root privileges, they will be able to do less with said tool than if it was installed by the system with the setuid bit set.
ping
ping is a simple program that sends ICMP ECHO_REQUEST
packets to networked devices. It is a common, simple way to test network connectivity from one host to another.
ICMP is a layer 4 protocol, like TCP and UDP. Kubernetes services support TCP and UDP, but not ICMP. This means that pings to a Kubernetes service will always fail. Instead, you will need to use telnet or a higher-level tool such as cURL to check connectivity to a service. Individual pods may still be reachable by ping, depending on your network configuration.
Firewalls and routing software are aware of ICMP packets and can be configured to filter or route specific ICMP packets. It is common, but not guaranteed (or necessarily advisable), to have permissive rules for ICMP packets. Some network administrators, network software, or cloud providers will allow ICMP packets by default.
By default, ping will send packets forever, and must be manually stopped (e.g., with Ctrl-C). -c
Common Options
Option | Description |
---|---|
-c <count> | Sends the specified number of packets. Exits after the final packet is received or times out. |
-i <seconds> | Sets the wait interval between sending packets. Defaults to 1 second. Extremely low values are not recommended, as ping can flood the network. |
-o | Exit after receiving 1 packet. Equivalent to -c 1. |
-S <source address> | Uses the specified source address for the packet. |
-W <milliseconds> | Sets the wait interval to receive a packet. If ping receives the packet later than the wait time, it will still count toward the final summary. |
traceroute
traceroute
shows the network route taken from one host to another. This allows users to easily validate and debug the route taken (or where routing fails) from one machine to another.
traceroute
sends packets with specific IP time-to-live values. When a host receives a packet and decrements the TTL to 0, it sends a TIME_EXCEEDED
packet and discards the original packet. The TIME_EXCEEDED
response packet contains the source address of the machine where the packet timed out. By starting with a TTL of 1 and raising the TTL by 1 for each packet, traceroute
is able to get a response from each host along the route to the destination address.
traceroute displays hosts line by line, starting with the first external machine. Each line contains the hostname (if available), IP address, and response time:
Common options
Option | Syntax | Description |
---|---|---|
First TTL | -f <TTL> , -M <TTL> | Set the starting IP TTL (default value: 1). Setting the TTL to n will cause traceroute to not report the first n-1 hosts en route to the destination. |
Max TTL | -m <TTL> | Set the maximum TTL, i.e., the maximum number of hosts that traceroute will attempt to route through. |
Protocol | -P <protocol> | Send packets of the specified protocol (TCP, UDP, ICMP, and sometimes other options). UDP is default. |
Source address | -s <address> | Specify the source IP address of outgoing packets. |
Wait | -w <seconds> | Set the time to wait for a probe response. |
dig
dig
is a DNS lookup tool. You can use it to make DNS queries from the command line and display the results.
The general form of a dig
command is dig [options] <domain>
. By default, dig
will display the CNAME, A, and AAAA records:
To display a particular type of DNS record, run dig
Common options
|Option|Syntax|Description|
|IPv4|-4
|Use IPv4 only.|
|IPv6|-6
|Use IPv6 only.|
|Address|-b <address>[#<port>]
|Specify the address to make a DNS query to. Port can optionally be included, preceded by #.|
|Port|-p <port>
|Specify the port to query, in case DNS is exposed on a nonstandard port. The default is 53, the DNS standard.|
|Domain|-q <domain>
|The domain name to query. The domain name is usually specified as a positional argument.|
|Record Type|-t <type>
|The DNS record type to query. The record type can alternatively be specified as a positional argument.|
telnet
telnet
is both a network protocol and a tool for using said protocol. telnet
was once used for remote login, in a manner similar to SSH. SSH has become dominant due to having better security, but telnet
is still extremely useful for debugging servers that use a text-based protocol. For example, with telnet
, you can connect to an HTTP/1 server and manually make requests against it.
The basic syntax of telnet
is telnet <address> <port>
. This establishes a connection and provides an interactive command-line interface. Pressing Enter twice will send a command, which easily allows multiline commands to be written. Press Ctrl-J to exit the session:
To make full use of telnet
, you will need to understand how the application protocol that you are using works. telnet
is a classic tool to debug servers running HTTP, HTTPS, POP3, IMAP, and so on.
nmap
nmap
is a port scanner, which allows you to explore and examine services on your network.
The general syntax of nmap
is nmap [options] <target>
, where target is a domain, IP address, or IP CIDR. nmap’s default options will give a fast and brief summary of open ports on a host:
In the previous example, nmap detects three open ports and guesses which service is running on each port.
Because nmap can quickly show you which services are accessible from a remote machine, it can be a quick and easy way to spot services that should not be exposed. nmap is a favorite tool for attackers for this reason.
nmap
has a dizzying number of options, which change the scan behavior and level of detail provided. As with other commands, we will summarize some key options, but we highly recommend reading nmap
’s help/man pages.
common options
Option | Syntax | Description |
---|---|---|
Additional detection | -A | Enable OS detection, version detection, and more. |
Decrease verbosity | -d | Decrease the command verbosity. Using multiple d’s (e.g., -dd) increases the effect. |
Increase verbosity | -v | Increase the command verbosity. Using multiple v’s (e.g., -vv) increases the effect. |
netstat
netstat can display a wide range of information about a machine’s network stack and connections:
Invoking netstat with no additional arguments will display all connected sockets on the machine. In our example, we see three TCP sockets, one UDP socket, and a multitude of UNIX sockets. The output includes the address (IP address and port) on both sides of a connection.
We can use the -a
flag to show all connections or -l
to show only listening connections:
A common use of netstat is to check which process is listening on a specific port. To do that, we run sudo netstat -lp - l
for “listening” and p for “program.” sudo may be necessary for netstat to view all program information. The output for -l
shows which address a service is listening on (e.g., 0.0.0.0
or 127.0.0.1
).
We can use simple tools like grep to get a clear output from netstat when we are looking for a specific result:
common options
Option | Syntax | Description |
---|---|---|
Show all sockets | netstat -a | Shows all sockets, not only open connections. |
Show statistics | netstat -s | Shows networking statistics. By default, netstat shows stats from all protocols. |
Show listening sockets | netstat -l | Shows sockets that are listening. This is an easy way to find running services. |
TCP | netstat -t | The -t flag shows only TCP data. It can be used with other flags, e.g., -lt (show sockets listening with TCP). |
UDP | netstat -u | The -u flag shows only UDP data. It can be used with other flags, e.g., -lu (show sockets listening with UDP). |
netcat
netcat is a multipurpose tool for making connections, sending data, or listening on a socket. It can be helpful as a way to “manually” run a server or client to inspect what happens in greater detail. netcat is arguably similar to telnet in this regard, though netcat is capable of many more things.
nc is an alias for netcat on most systems.
netcat can connect to a server when invoked as netcat
Openssl
The OpenSSL technology powers a substantial chunk of the world’s HTTPS connections. Most heavy lifting with OpenSSL is done with language bindings, but it also has a CLI for operational tasks and debugging. openssl can do things such as creating keys and certificates, signing certificates, and, most relevant to us, testing TLS/SSL connections. Many other tools, including ones outlined in this chapter, can test TLS/SSL connections. However, openssl stands out for its feature-richness and level of detail.
Commands usually take the form openssl [sub-command] [arguments] [options]. openssl has a vast number of subcommands (for example, openssl rand allows you to generate pseudo random data). The list subcommand allows you to list capabilities, with some search options (e.g., openssl list --commands
for commands). To learn more about individual sub commands, you can check openssl
openssl s_client -connect
will connect to a server and display detailed information about the server’s certificate. Here is the default invocation:
If you are using a self-signed CA, you can use -CAfile
cURL
cURL is a data transfer tool that supports multiple protocols, notably HTTP and HTTPS.
wget is a similar tool to the command curl. Some distros or administrators may install it instead of curl.
cURL commands are of the form curl [options]
By default, cURL does not follow redirects, such as HTTP 301s or protocol upgrades. The -L flag (or —location) will enable redirect following:
Use the -X option to perform a specific HTTP verb; e.g., use curl -X
You can supply data (for a POST, PUT, etc.) in a few ways:
As a file in either format: -d @data.txt
The -H option adds an explicit header, although basic headers such as Content-Type are added automatically:
Here are some examples:
Like many programs, cURL has a verbose flag, -v, which will print more information about the request and response. This is extremely valuable when debugging a layer 7 protocol such as HTTP:
cURL has many additional features that we have not covered, such as the ability to use timeouts, custom CA certs, custom DNS, and so on.