Skip to content
Laviyx Learn

Try

Linux and Bash course menu

Linux Networking Commands: ip, ss, ping, dig and traceroute

Inspect your network from the Linux terminal: IP addresses with ip, open ports with ss, DNS with dig, connectivity with ping and nc, and the firewall with ufw.

Intermediate5 min readUpdated
On this page

When an app cannot reach a server, these commands tell you where the problem is.

My address#

ip shows network interfaces and addresses. -br gives a brief view:

Terminal
ip -br addr show lo
hostname
Output
lo               UNKNOWN        127.0.0.1/8 ::1/128 
laviyx

lo is the loopback interface, and 127.0.0.1 (or localhost) always means "this computer". On a real machine, you will also see an interface such as eth0, with an address such as 192.168.1.20.

See how traffic leaves the machine with ip route. The line beginning default shows your gateway:

Terminal
ip route

Ports#

A port identifies one service on a computer. A web server listens on 80 or 443, SSH on 22, and PostgreSQL on 5432.

Start a small web server, and see it with ss:

Terminal
python3 -m http.server 8000 > /dev/null 2>&1 &
sleep 1
ss -ltn | grep 8000
Output
LISTEN 0      5            0.0.0.0:8000      0.0.0.0:*

The options are -l listening, -t TCP and -n numeric. Add -p with sudo to see which program owns the port. 0.0.0.0 means it accepts connections on all addresses, while 127.0.0.1 would accept them only from this machine.

Is a port open?#

nc (netcat) tests a connection:

Terminal
nc -zv localhost 8000
nc -zv localhost 8001
Output
nc: connect to localhost (::1) port 8000 (tcp) failed: Connection refused
Connection to localhost (127.0.0.1) 8000 port [tcp/*] succeeded!
nc: connect to localhost (::1) port 8001 (tcp) failed: Connection refused
nc: connect to localhost (127.0.0.1) port 8001 (tcp) failed: Connection refused

Is the host reachable?#

ping sends small packets and measures the reply time. Press Ctrl+C to stop:

Terminal
ping -c 3 example.com

Some networks block ping, so a failure does not always mean that the host is down. traceroute example.com shows the path, hop by hop.

DNS#

DNS turns names into IP addresses. dig asks for records, and +short prints only the answer:

Terminal
dig +short localhost
dig +short NS example.com | sort | head -n 2
Output
127.0.0.1
elliott.ns.cloudflare.com.
hera.ns.cloudflare.com.

Local names can be set in /etc/hosts, which is read before DNS:

Terminal
grep -m1 localhost /etc/hosts
Output
127.0.0.1 localhost

/etc/resolv.conf shows which DNS servers are used. "Name or service not known" means a DNS problem, not a server problem.

Firewall#

Ubuntu's simple firewall tool is ufw. Allow SSH before enabling it on a remote server, or you lock yourself out:

Terminal
sudo ufw allow OpenSSH
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status

Cloud servers usually have a separate firewall in the provider's dashboard, often called a security group.

A troubleshooting order#

StepCommandQuestion
1ip -br addrDo I have an address?
2ip routeDo I have a gateway?
3dig example.comDoes DNS work?
4ping example.comCan I reach it?
5nc -zv example.com 443Is the port open?
6curl -v https://example.comDoes the app answer?
7ss -ltn (on the server)Is the service listening?

Common mistakes#

Practice#

Practice exercise

Print how many TCP ports are listening on this machine, then print the numeric address of localhost using getent.

Show solution
Terminal
ss -ltnH | wc -l | awk '{print ($1 > 0) ? "Some ports are listening" : "No ports listening"}'
getent ahostsv4 localhost | head -n 1 | awk '{print $1}'
Output
Some ports are listening
127.0.0.1

Key takeaway#

Next: Calling Web Services with curl.

Finished this lesson?