DEV Community

Saksham Sharma
Saksham Sharma

Posted on

Essential Linux Networking Commands for Cybersecurity & Daily Troubleshooting

When something on a network breaks—a service refuses connections, DNS resolves to the wrong IP, a firewall drops packets, or a port is already in use—guessing slows you down. Whether you are working on a Linux server, a virtual machine, or a Termux environment on Android, knowing a handful of core networking commands lets you diagnose problems methodically instead of changing settings at random.

This guide covers practical Linux networking commands organized by what you are actually trying to verify: **interfaces and routes**, **sockets and listening ports**, **DNS resolution**, **HTTP/API reachability**, **open ports and services**, and **live packet inspection**.

---

## 1. Interfaces, IP Addresses, and Routing (`ip`)

The `ip` command (from `iproute2`) is the modern standard for inspecting network interfaces, IP addresses, and routing tables on Linux.

### Check Network Interfaces and IP Addresses

Enter fullscreen mode Exit fullscreen mode


bash
ip addr show

or shorthand:

ip a


Look for:

* **`lo` (loopback):** `127.0.0.1/8` — local host traffic. If loopback has issues, local services won't talk to each other properly.
* **Physical or virtual interfaces (`eth0`, `enp0s3`, `wlan0`):** Check whether the interface is `UP` or `DOWN` and confirm the assigned IPv4/IPv6 address and subnet mask (CIDR).

To bring an interface up or down (requires root or `sudo`):

Enter fullscreen mode Exit fullscreen mode


bash
sudo ip link set dev eth0 up
sudo ip link set dev eth0 down


### Inspect the Routing Table

If a host has an IP address but cannot reach the internet or another subnet, check the default gateway and routes:

Enter fullscreen mode Exit fullscreen mode


bash
ip route show


A typical output includes a line starting with `default via <gateway-ip> dev <interface>`. If that default route is missing or pointing to the wrong interface, outbound traffic to external networks will fail.

---

## 2. Basic Reachability (`ping`)

`ping` sends ICMP echo requests to test round-trip reachability and latency to a target IP or hostname.

Enter fullscreen mode Exit fullscreen mode


bash
ping -c 4 1.1.1.1
ping -c 4 example.com


* Use `-c 4` so it stops after 4 packets instead of running continuously.
* **Ping to `1.1.1.1` succeeds, ping to `example.com` fails:** Often points to a DNS resolution issue rather than lack of internet routing.
* **Ping fails completely:** Could mean routing is down, the target blocks ICMP (`ping`), or a firewall is dropping traffic. Note that some servers and cloud instances intentionally ignore ICMP echo requests even when web or SSH ports are open.

---

## 3. Sockets, Listening Ports, and Connections (`ss`)

When troubleshooting why a web server, SSH daemon, or local script isn't accepting connections—or which process is holding onto a port—`ss` (socket statistics) is faster and cleaner than legacy `netstat`.

### List Listening TCP and UDP Ports

Enter fullscreen mode Exit fullscreen mode


bash
ss -tulnp


What the flags mean:

* `-t`: TCP sockets
* `-u`: UDP sockets
* `-l`: Listening sockets only
* `-n`: Show numeric ports/addresses instead of resolving service names
* `-p`: Show the process name and PID (may require `sudo` to see processes owned by other users)

### Inspect Established Connections

Enter fullscreen mode Exit fullscreen mode


bash
ss -tup state established


This helps you spot active outbound or inbound connections, see remote peer addresses, and verify whether a client is actually connecting to the port you expect.

---

## 4. DNS Resolution (`dig` and `getent`)

Name resolution turns domains like `example.com` into IP addresses (`A` for IPv4, `AAAA` for IPv6). When browsers or scripts fail with name resolution errors, test DNS directly.

### Query DNS Records with `dig`

Enter fullscreen mode Exit fullscreen mode


bash
dig example.com A +short
dig example.com AAAA +short
dig example.com MX +short
dig example.com TXT +short


To query a specific DNS resolver (such as Cloudflare `1.1.1.1` or Google `8.8.8.8`) and compare results:

Enter fullscreen mode Exit fullscreen mode


bash
dig @1.1.1.1 example.com +noall +answer


Comparing `@1.1.1.1` against your local resolver helps isolate whether a problem is local/ISP DNS or an issue with the domain's authoritative DNS records.

### Check OS-Level Name Resolution (`getent`)

Enter fullscreen mode Exit fullscreen mode


bash
getent hosts example.com


`getent` uses the system's Name Service Switch (`/etc/nsswitch.conf`), so it reflects `/etc/hosts` entries as well as DNS—making it useful for checking local overrides.

---

## 5. HTTP, Headers, and TLS Checks (`curl`)

Before blaming application code, verify what the server actually returns over HTTP or HTTPS using `curl`.

### Inspect Response Headers and Status Code

Enter fullscreen mode Exit fullscreen mode


bash
curl -I https://example.com


`-I` fetches headers (`HEAD` request) so you can quickly check HTTP status codes (`200`, `301`/`302` redirects, `403`, `404`, `502`/`503`), `Location` redirect targets, and server headers.

### Follow Redirects and Measure Timing

Enter fullscreen mode Exit fullscreen mode


bash
curl -IL https://example.com
curl -s -o /dev/null -w "Status: %{http_code} | Connect: %{time_connect}s | Total: %{time_total}s\n" https://example.com


This is handy for checking slow endpoints, redirect chains, and whether HTTPS endpoints respond cleanly.

---

## 6. Port & Service Discovery (`nmap`)

`nmap` helps verify which ports are open or filtered on a host from an outside perspective. Only scan systems and networks you own or have explicit authorization to test.

### Scan Common TCP Ports on a Target

Enter fullscreen mode Exit fullscreen mode


bash
nmap -sT -F


* `-sT`: TCP connect scan (works reliably even without raw socket privileges in restricted or mobile environments).
* `-F`: Fast scan of common ports.

### Check Service Versions on Specific Ports

Enter fullscreen mode Exit fullscreen mode


bash
nmap -sV -p 22,80,443


Checking specific ports (`-p`) keeps scans focused, fast, and less noisy than broad sweeps.

---

## 7. Live Packet Inspection (`tcpdump`)

When `ip`, `ss`, and `curl` show symptoms without revealing why packets aren't arriving or leaving, `tcpdump` captures live network traffic on an interface.

### Capture Traffic on a Specific Interface and Port

Enter fullscreen mode Exit fullscreen mode


bash
sudo tcpdump -i eth0 -n port 80


* `-i eth0`: Listen on interface `eth0` (use `-i any` to watch all interfaces).
* `-n`: Don't resolve hostnames or port names (keeps output fast and clear).
* `port 80`: Filter only traffic involving TCP/UDP port `80`.

### Filter by Host or Save to a Capture File

Enter fullscreen mode Exit fullscreen mode


bash
sudo tcpdump -i any -n host 192.168.1.10
sudo tcpdump -i eth0 -n -w capture.pcap port 53


Saving to a `.pcap` file lets you inspect traffic later or share a minimal capture safely (after verifying no sensitive data is inside). Always stop `tcpdump` with `Ctrl+C` when finished so you don't leave captures running unnecessarily.

---

## 8. A Practical Troubleshooting Workflow

When debugging a network issue from scratch, moving step by step saves time:

1. **Check local state:** Run `ip a` and `ip route show` to confirm your interface is `UP`, has a valid IP, and has a working route.
2. **Test IP vs. DNS:** Ping `1.1.1.1` (`ping -c 4 1.1.1.1`), then test `dig example.com +short` and `getent hosts example.com`.
3. **Check ports and listeners:** Use `ss -tulnp` locally to verify the service is actually listening on the expected address and port.
4. **Test application traffic:** Use `curl -I` (or `curl -v`) to inspect HTTP headers, redirects, or TLS/connection errors.
5. **Inspect the wire if needed:** Use `tcpdump` with a tight filter (`host` or `port`) to see whether packets reach the interface and whether replies come back.

---

## About the Author

**Saksham Sharma** is a 16-year-old cybersecurity researcher and developer from Beni-4, Singa, Myagdi, currently living in Pokhara, Nepal. Studying in Class 11, he focuses on network security, cloud security, and open-source AI/LLMs, alongside Linux/Termux environments, Python scripting, and Android application development.

* **Portfolio:** https://sakshamsharmanepal.github.io/sakshamsharma/
* **GitHub:** https://github.com/sakshamsharmanepal
* **LinkedIn:** https://www.linkedin.com/in/sakshamsharmanepal
* **Email:** connect@sakshamsharma.work.gd

Enter fullscreen mode Exit fullscreen mode


plaintext




About the Author

Saksham Sharma (Known As Saksham Sharma Nepal) is a 16-year-old cybersecurity researcher and developer from Beni-4, Singa, Myagdi, currently living in Pokhara, Nepal. Studying in Class 11, he focuses on network security, cloud security, and open-source AI/LLMs, alongside Linux/Termux environments, Python scripting, and Android application development.

* **Portfolio:** https://sakshamsharmanepal.github.io/sakshamsharma/
* **GitHub:** https://github.com/sakshamsharmanepal
* **LinkedIn:** https://www.linkedin.com/in/sakshamsharmanepal
* **Email:** connect@sakshamsharma.work.gd
Enter fullscreen mode Exit fullscreen mode

Top comments (0)