Linux Networking Commands Cheat Sheet
These commands answer the basic network questions: what is my address, what is listening, can I reach that host, and what does DNS say? Most work without root, but some need sudo.
Your addresses and routes: ip
| Command | What it does | Example |
|---|---|---|
ip a |
Show all network interfaces and their IP addresses | ip a |
ip -4 a |
Show only IPv4 addresses | ip -4 a |
ip a show <iface> |
Show one interface | ip a show eth0 |
ip link |
Show interfaces and whether they are UP or DOWN | ip link |
ip r |
Show the routing table and default gateway | ip r |
ip neigh |
Show the ARP table (IP to MAC address) | ip neigh |
The old ifconfig and route commands still exist on some systems but ip replaces them.
What is listening: ss
| Command | What it does | Example |
|---|---|---|
ss -tuln |
Listening TCP and UDP ports, numbers only | ss -tuln |
sudo ss -tulnp |
Same, plus the program that owns each port | sudo ss -tulnp |
ss -tn |
Current TCP connections | ss -tn |
ss -tn state established |
Only established TCP connections | ss -tn state established |
Flags: -t TCP, -u UDP, -l listening, -n numbers (no name lookups), -p process.
Reading the output: 0.0.0.0:22 or [::]:22 means the port is open on every interface. 127.0.0.1:3306 means only this machine can reach it.
Can I reach it? ping and traceroute
| Command | What it does | Example |
|---|---|---|
ping <host> |
Send ICMP echo requests until you press Ctrl+C | ping 192.0.2.10 |
ping -c 4 <host> |
Send only 4 packets | ping -c 4 www.example |
traceroute <host> |
Show each router (hop) on the way | traceroute 198.51.100.7 |
traceroute -n <host> |
Same, without name lookups (faster) | traceroute -n 198.51.100.7 |
tracepath <host> |
Similar to traceroute, no root needed | tracepath 198.51.100.7 |
No reply to ping does not always mean the host is down. Many firewalls block ICMP.
DNS: dig
| Command | What it does | Example |
|---|---|---|
dig <domain> |
Look up the A record (IPv4 address) | dig www.example |
dig <domain> +short |
Show only the answer | dig www.example +short |
dig <domain> AAAA |
IPv6 address | dig www.example AAAA |
dig <domain> MX |
Mail servers | dig shop.example MX |
dig <domain> NS |
Name servers | dig shop.example NS |
dig <domain> TXT |
Text records (SPF, verification) | dig shop.example TXT |
dig @<server> <domain> |
Ask a specific DNS server | dig @192.0.2.53 www.example |
dig -x <ip> |
Reverse lookup (IP to name) | dig -x 203.0.113.5 |
nslookup and host also work for quick checks: host www.example.
Web requests: curl
| Command | What it does | Example |
|---|---|---|
curl <url> |
Download a page and print it | curl https://www.example/ |
curl -I <url> |
Show only the response headers (HEAD request) | curl -I https://www.example/ |
curl -i <url> |
Show headers and body | curl -i https://www.example/ |
curl -v <url> |
Verbose: show the full request and response | curl -v https://www.example/ |
curl -L <url> |
Follow redirects | curl -L http://www.example/ |
curl -o <file> <url> |
Save output to a file | curl -o page.html https://www.example/ |
curl -X POST -d <data> <url> |
Send a POST request with form data | curl -X POST -d "user=test" https://lab.example/login |
curl -H "<header>" <url> |
Add a header | curl -H "Accept: application/json" https://api.example/ |
curl -b "<cookie>" <url> |
Send a cookie | curl -b "session=abc123" https://lab.example/ |
curl -k <url> |
Ignore certificate errors (labs only) | curl -k https://192.0.2.20/ |
Netcat basics: nc
| Command | What it does | Example |
|---|---|---|
nc -zv <host> <port> |
Check if a TCP port is open | nc -zv 192.0.2.10 22 |
nc -zv <host> <range> |
Check a range of ports | nc -zv 192.0.2.10 20-25 |
nc <host> <port> |
Connect and type by hand (see the banner) | nc 192.0.2.10 25 |
nc -lvnp <port> |
Listen on a port for an incoming connection | nc -lvnp 4444 |
Flags differ a little between netcat versions (traditional, OpenBSD, ncat). If one does not work, check nc -h.
Firewall: ufw (Ubuntu and Debian)
| Command | What it does | Example |
|---|---|---|
sudo ufw status verbose |
Show if the firewall is on and its rules | sudo ufw status verbose |
sudo ufw status numbered |
Show rules with numbers | sudo ufw status numbered |
sudo ufw default deny incoming |
Block incoming traffic by default | sudo ufw default deny incoming |
sudo ufw default allow outgoing |
Allow outgoing traffic by default | sudo ufw default allow outgoing |
sudo ufw allow <port>/<proto> |
Allow a port | sudo ufw allow 22/tcp |
sudo ufw allow from <ip> to any port <port> |
Allow one IP to one port | sudo ufw allow from 192.0.2.50 to any port 22 |
sudo ufw deny from <ip> |
Block an IP | sudo ufw deny from 203.0.113.99 |
sudo ufw delete <number> |
Delete a rule by its number | sudo ufw delete 3 |
sudo ufw enable |
Turn the firewall on | sudo ufw enable |
sudo ufw disable |
Turn the firewall off | sudo ufw disable |
On a remote server, always run
sudo ufw allow 22/tcp(or your SSH port) beforesudo ufw enable. Otherwise you can lock yourself out.
Quick troubleshooting order
ip a- do I have an IP address?ip r- do I have a default gateway?ping -c 4 <gateway>- can I reach my router?ping -c 4 <outside IP>- can I reach the internet without using DNS?dig www.example +short- is DNS working?curl -I https://www.example/- does the web service answer?
Common ports
| Port | Service |
|---|---|
| 21 | FTP |
| 22 | SSH |
| 25 | SMTP (mail) |
| 53 | DNS |
| 80 | HTTP |
| 443 | HTTPS |
| 445 | SMB (Windows file sharing) |
| 3306 | MySQL |
| 3389 | RDP (Windows remote desktop) |
Safe and legal use
Port checks and banner grabbing are a form of scanning. Only test systems you own or have written permission to test.
SECUREK Learn · learn.securek.co/cheat-sheets/linux-networking-commands
Spotted a mistake or something unclear? Report a mistake