# Basic network troubleshooting

> Read and change addresses and routes with ip, find listening ports with ss, test paths with ping, traceroute, tracepath and netcat, and know the old net-tools.

Source: https://aminbytes.dev/bytes/networking-fundamentals/network-troubleshooting/


"The network is down" can mean a hundred things: a cable, a wrong address, a missing route, a dead DNS server, a firewall, a service that crashed. Good troubleshooting is about asking the right question in the right order, and each question has a command.

This byte covers the **iproute2** tools (`ip`, `ss`) that every current distribution ships, the path testers (`ping`, `traceroute`, `tracepath`, `netcat`) and the **legacy net-tools** (`ifconfig`, `route`, `netstat`) the exam still expects you to recognize. Everything you change here with `ip` is **temporary**: it's gone after a reboot. To make it stick, use the tools from [persistent network configuration](../network-configuration/).

You need root (or `sudo`) to change anything, not to look. If a command is missing, install `iproute2`, `iputils-ping`, `traceroute`, `iputils-tracepath` and `netcat-openbsd` on Debian or Ubuntu, or `iproute`, `iputils`, `traceroute` and `nmap-ncat` on RHEL or Fedora.

## Interfaces with ip

`ip` takes an **object** and a **command**: `ip <object> <command>`. Objects can be shortened: `ip a` is `ip addr`, `ip r` is `ip route`, `ip l` is `ip link`, `ip n` is `ip neigh`.

### Look

```bash
ip addr show
```

```text
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:4e:66:a1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.50/24 brd 192.168.1.255 scope global dynamic noprefixroute enp0s3
       valid_lft 85912sec preferred_lft 85912sec
    inet6 fe80::a00:27ff:fe4e:66a1/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
```

How to read it:

- `<...UP,LOWER_UP>`: `UP` means the interface is switched on, `LOWER_UP` means a cable or radio link is actually there. `NO-CARRIER` means "unplugged".
- `link/ether`: the MAC address.
- `inet` is IPv4, `inet6` is IPv6, each with its prefix. `dynamic` means it came from DHCP.
- `mtu 1500`: the largest packet the interface sends.

Useful variations:

```bash
ip -br addr              # one line per interface
ip -4 addr               # only IPv4
ip addr show dev enp0s3  # one interface
ip -s link show enp0s3   # with packet, error and drop counters
```

### Change

```bash
sudo ip link set dev enp0s3 down          # switch the interface off
sudo ip link set dev enp0s3 up            # and on again
sudo ip addr add 192.168.1.60/24 dev enp0s3
sudo ip addr del 192.168.1.60/24 dev enp0s3
sudo ip addr flush dev enp0s3             # remove every address
sudo ip link set dev enp0s3 mtu 9000      # change the MTU
```

An interface can hold several addresses at once, so `ip addr add` adds one rather than replacing the old one. Always give the prefix: without `/24`, `ip` assumes `/32`, a network of one, and nothing else on the LAN is reachable.

`ip neigh` shows the neighbor table: which MAC address belongs to which IP on your local network (ARP for IPv4, NDP for IPv6):

```text
192.168.1.1 dev enp0s3 lladdr a4:2b:b0:11:22:33 REACHABLE
192.168.1.10 dev enp0s3 lladdr 08:00:27:9c:41:0e STALE
```

## Routing with ip

The **routing table** tells the kernel where to send each packet. Every packet goes to the most specific matching route. If nothing else matches, it goes to the **default route**, also called the default gateway.

```bash
ip route
```

```text
default via 192.168.1.1 dev enp0s3 proto dhcp src 192.168.1.50 metric 100
192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.50 metric 100
```

Line one: anything without a better match goes **via** the router `192.168.1.1`. Line two: `192.168.1.0/24` is directly attached, so packets there go straight out of `enp0s3` (`proto kernel` means the kernel added it when the address was set).

To ask "which route would a packet to this address take?":

```bash
ip route get 1.1.1.1
```

```text
1.1.1.1 via 192.168.1.1 dev enp0s3 src 192.168.1.50 uid 1000
    cache
```

Change routes like this:

```bash
sudo ip route add 10.10.0.0/16 via 192.168.1.254         # a network behind another router
sudo ip route del 10.10.0.0/16
sudo ip route add default via 192.168.1.1                # set the default gateway
sudo ip route replace default via 192.168.1.254          # change it in one step
sudo ip route del default
ip -6 route                                              # the IPv6 table
```

`ip route add default` fails with `RTNETLINK answers: File exists` if a default route is already there. Use `replace`, or delete the old one first.

To make a static route persistent with NetworkManager, add it to the profile:

```bash
sudo nmcli connection modify office +ipv4.routes "10.10.0.0/16 192.168.1.254"
sudo nmcli connection up office
```

> [!NOTE]
> A Linux box only passes packets between its interfaces (acts as a router) when forwarding is on: `sysctl net.ipv4.ip_forward` shows `0` or `1`. Turn it on with `sudo sysctl -w net.ipv4.ip_forward=1`, and make it permanent in a file under `/etc/sysctl.d/`.

## hostname

`hostname` prints or sets the machine's name. Setting it this way lasts only until reboot (use `hostnamectl` for a permanent change).

| Command | Shows |
| ------- | ----- |
| `hostname` | the name, like `web01` |
| `hostname -s` | the short name, without the domain |
| `hostname -f` | the fully qualified name, like `web01.example.lan` |
| `hostname -d` | only the domain, `example.lan` |
| `hostname -i` | the address(es) the hostname resolves to |
| `hostname -I` | every address on every interface (no name lookup) |
| `sudo hostname web02` | sets the name until reboot |

`hostname -i` printing `127.0.1.1` is normal on Debian: it comes from `/etc/hosts`.

## Sockets with ss

`ss` (socket statistics) shows open connections and listening ports. It replaces `netstat`.

```bash
sudo ss -tulpn
```

```text
Netid State  Recv-Q Send-Q Local Address:Port  Peer Address:Port Process
udp   UNCONN 0      0      127.0.0.53%lo:53         0.0.0.0:*     users:(("systemd-resolve",pid=612,fd=13))
tcp   LISTEN 0      128          0.0.0.0:22         0.0.0.0:*     users:(("sshd",pid=801,fd=3))
tcp   LISTEN 0      511                *:80               *:*     users:(("nginx",pid=1022,fd=6))
tcp   LISTEN 0      4096       127.0.0.1:5432       0.0.0.0:*     users:(("postgres",pid=944,fd=7))
```

| Option | Meaning |
| ------ | ------- |
| `-t` / `-u` | TCP / UDP sockets |
| `-l` | only listening sockets (without it: only established ones) |
| `-a` | all sockets, listening and connected |
| `-n` | numbers, not names (`22`, not `ssh`) |
| `-p` | the process using the socket (needs root for other users' processes) |
| `-s` | summary counts |
| `-4` / `-6` | only IPv4 / IPv6 |

The local address tells you **who can reach** the service. `0.0.0.0:22` or `*:80` listens on every interface. `127.0.0.1:5432` only accepts connections from the machine itself, a classic reason for "works locally, refused from outside".

`ss` can filter, too:

```bash
ss -tn state established '( dport = :443 )'
```

## Testing reachability with ping and ping6

`ping` sends ICMP echo requests and waits for echo replies. It answers the most basic question: can I reach that address at all?

```bash
ping -c 4 192.168.1.1
```

```text
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.512 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.431 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.540 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.509 ms

--- 192.168.1.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 0.431/0.498/0.540/0.041 ms
```

Without `-c`, Linux `ping` runs until you press Ctrl+C. Other options: `-i 0.5` (interval in seconds), `-W 2` (seconds to wait for a reply), `-s 1472` (payload size), `-I enp0s3` (send from this interface).

For IPv6, use **`ping6`** or `ping -6`. On current systems `ping6`, where it still exists, is just another name for `ping`. Link-local addresses need the interface:

```bash
ping6 -c 2 ::1
ping -6 -c 2 fe80::1%enp0s3
```

Read failures carefully:

- `Destination Host Unreachable`: nobody on the local network answered ARP. Wrong address, or the host is off.
- `Network is unreachable`: your machine has no route for that address. Check `ip route`.
- No output, 100% packet loss: packets go out, but nothing comes back. Often a firewall dropping ICMP.

> [!TIP]
> A failing `ping` doesn't prove a host is down. Many servers and cloud firewalls block ICMP. Test the real service port with `nc` before you conclude anything.

## Following the path: traceroute and tracepath

When the gateway answers but a far host doesn't, find **where** the packets stop. `traceroute` sends probes with a TTL (time to live) of 1, then 2, then 3. Each router lowers the TTL by one, and the router where it hits zero sends back an ICMP "time exceeded". That reveals every hop:

```bash
traceroute example.com
```

```text
traceroute to example.com (203.0.113.10), 30 hops max, 60 byte packets
 1  router.lan (192.168.1.1)  0.612 ms  0.571 ms  0.549 ms
 2  10.20.0.1 (10.20.0.1)  8.921 ms  9.004 ms  8.877 ms
 3  * * *
 4  ae-1.core1.ams.example.net (198.51.100.17)  12.402 ms  12.388 ms  12.511 ms
 5  203.0.113.10 (203.0.113.10)  14.230 ms  14.118 ms  14.207 ms
```

Each line is a hop with three timing probes. `* * *` means that router didn't answer in time, which is common and harmless if later hops still answer. If every hop after a certain point is `* * *`, that is where traffic dies.

Linux `traceroute` sends **UDP** probes by default (ports from 33434 up). `-I` uses ICMP echo instead, `-T` uses TCP SYN (needs root), `-n` skips name lookups. **`traceroute6`** or `traceroute -6` does the same over IPv6.

**`tracepath`** is the simpler cousin from iputils. It needs **no root** and also discovers the **path MTU**, the largest packet that fits through every hop:

```bash
tracepath -n example.com
```

```text
 1?: [LOCALHOST]                      pmtu 1500
 1:  192.168.1.1                                           0.598ms
 1:  192.168.1.1                                           0.512ms
 2:  10.20.0.1                                             9.112ms
 3:  no reply
 4:  198.51.100.17                                        12.733ms asymm  5
 5:  203.0.113.10                                         14.302ms reached
     Resume: pmtu 1500 hops 5 back 5
```

For IPv6 use `tracepath6`, or `tracepath -6` on systems where the separate `tracepath6` command is gone.

 (Side note: `mtr` (from the `mtr` package) combines `ping` and `traceroute` in one live, updating screen. It isn't on the exam, but it's the tool many admins reach for first.)

## Talking to ports: netcat

**netcat** (`nc`) opens a raw TCP or UDP connection and connects it to your terminal. It's the Swiss army knife for "is that port really open?"

```bash
nc -zv example.com 443
nc -zv example.com 8443
```

```text
Connection to example.com (203.0.113.10) 443 port [tcp/https] succeeded!
nc: connect to example.com (203.0.113.10) port 8443 (tcp) failed: Connection refused
```

`-z` only checks the connection without sending data, `-v` prints what happens, `-u` switches to UDP, `-w 3` gives up after 3 seconds. "Connection refused" means the host answered but nothing listens there. A long wait ending in a timeout usually means a firewall drops the packets.

You can also speak a text protocol by hand. Here is a raw HTTP request (see [the HTTP topic](../../http/basics/what-is-http/) for what the lines mean):

```bash
printf 'HEAD / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n' | nc example.com 80
```

Or open a tiny chat between two terminals. One listens, the other connects, and whatever you type appears on the other side:

```bash
nc -l 5000                  # terminal 1: listen on port 5000
nc 192.168.1.50 5000        # terminal 2: connect to it
```

There are several netcats. Debian and Ubuntu use `netcat-openbsd` (shown above). RHEL and Fedora ship **ncat** from the Nmap project as `nc`, which prints messages like `Ncat: Connected to 203.0.113.10:443.` The old `netcat-traditional` wants `nc -l -p 5000` to listen.

## Legacy net-tools: ifconfig, route, netstat

Before iproute2, Linux used the **net-tools** package: `ifconfig`, `route`, `netstat` and `arp`. They are deprecated, not installed by default on Debian 12 or RHEL 9 (install the `net-tools` package), and they don't show everything (for example extra addresses added with `ip addr add`). But old scripts, old documentation and the exam still use them.

```bash
ifconfig enp0s3
```

```text
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.50  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a00:27ff:fe4e:66a1  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:4e:66:a1  txqueuelen 1000  (Ethernet)
        RX packets 10234  bytes 9876543 (9.4 MiB)
        TX packets 5120  bytes 612345 (598.0 KiB)
```

```bash
route -n
```

```text
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 enp0s3
192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
```

The `G` flag marks a route through a gateway. Destination `0.0.0.0` with mask `0.0.0.0` is the default route.

| Task | Legacy net-tools | iproute2 |
| ---- | ---------------- | -------- |
| Show all interfaces | `ifconfig -a` | `ip addr` |
| Set an address | `ifconfig enp0s3 192.168.1.60 netmask 255.255.255.0` | `ip addr add 192.168.1.60/24 dev enp0s3` |
| Interface up / down | `ifconfig enp0s3 up` / `down` | `ip link set enp0s3 up` / `down` |
| Show routes | `route -n` or `netstat -rn` | `ip route` |
| Set default gateway | `route add default gw 192.168.1.1` | `ip route add default via 192.168.1.1` |
| Add a network route | `route add -net 10.10.0.0 netmask 255.255.0.0 gw 192.168.1.254` | `ip route add 10.10.0.0/16 via 192.168.1.254` |
| Delete default route | `route del default gw 192.168.1.1` | `ip route del default` |
| Listening ports | `netstat -tulpn` | `ss -tulpn` |
| All connections | `netstat -an` | `ss -an` |
| Interface counters | `netstat -i` | `ip -s link` |
| ARP / neighbor table | `arp -n` | `ip neigh` |

## A troubleshooting order

Work from the bottom up. Stop at the first step that fails, fix it, and start again:

1. **Link**: is the interface up with a carrier? `ip -br link` (look for `UP` and no `NO-CARRIER`).
2. **Address**: does it have the right address and prefix? `ip -br addr`. A `169.254.x.x` address means DHCP failed.
3. **Local network**: does the gateway answer? `ip route` to find it, then `ping -c 3 <gateway>`.
4. **Beyond**: can you reach an outside address by number? `ping -c 3 1.1.1.1`. If not, `tracepath -n 1.1.1.1` shows where it stops.
5. **Names**: does DNS work? `ping example.com` fails but `ping 1.1.1.1` works means a DNS problem. See [client side DNS](../client-side-dns/).
6. **Service**: is the port open? On the server `ss -tlnp`, from the client `nc -zv host port`. Check whether it listens on `127.0.0.1` only, and whether a firewall is in the way.

The logs help too: `journalctl -u NetworkManager` (or `-u systemd-networkd`) and `dmesg | grep -i enp0s3` show link changes and DHCP results.

## Exam checklist

- `ip addr`, `ip link`, `ip route`, `ip neigh` to view. `ip addr add|del IP/prefix dev IF`, `ip link set IF up|down`. All changes are lost at reboot.
- Default route: `ip route add default via GW`. Network route: `ip route add NET/prefix via GW`. `ip route get IP` shows which route is used.
- Legacy equivalents: `ifconfig`, `route -n`, `route add default gw GW`, `netstat -tulpn`, `netstat -rn`, `arp`. They come from net-tools and are deprecated.
- `ss -tulpn`: TCP, UDP, listening, process, numeric. `0.0.0.0` or `*` listens everywhere, `127.0.0.1` only locally.
- `ping` uses ICMP echo; `-c` count. `ping6` (or `ping -6`) for IPv6; link-local needs `%interface`.
- `traceroute` uses rising TTLs and UDP by default (`-I` ICMP, `-T` TCP). `tracepath` needs no root and reports path MTU. Both have `6` versions.
- `nc -zv host port` tests a TCP port, `nc -l port` listens, `-u` for UDP.
- `hostname` shows or temporarily sets the name. `-f` FQDN, `-i` resolved address, `-I` all local addresses.
- Troubleshoot bottom up: link, address, gateway, routing, DNS, service and firewall.


## Byte check

1. Which command sets the default gateway to `192.168.1.1` with iproute2?
   - **`ip route add default via 192.168.1.1`** (correct)
   - `ip route add default gw 192.168.1.1`
   - `ip addr add default 192.168.1.1`
   iproute2 uses `via` for the next hop. `gw` belongs to the legacy `route add default gw` command.

2. You want to see which programs listen on TCP and UDP ports, with numbers instead of service names. Which command fits?
   - **`ss -tulpn`** (correct)
   - `ip -s link`
   - `ping -c 4 localhost`
   `-t` TCP, `-u` UDP, `-l` listening, `-p` process, `-n` numeric. `netstat -tulpn` is the legacy equivalent.

3. What is a key advantage of `tracepath` over `traceroute`?
   - It only uses TCP
   - **It needs no root and also reports the path MTU** (correct)
   - It works only on IPv6
   `tracepath` runs as a normal user and shows `pmtu` along the way. `traceroute` has more options, and some of them (like TCP probes with `-T`) need root.


