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.
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:
ip -br addr show lo
hostnamelo UNKNOWN 127.0.0.1/8 ::1/128
laviyxlo 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:
ip routePorts#
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:
python3 -m http.server 8000 > /dev/null 2>&1 &
sleep 1
ss -ltn | grep 8000LISTEN 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:
nc -zv localhost 8000
nc -zv localhost 8001nc: 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 refusedIs the host reachable?#
ping sends small packets and measures the reply time. Press Ctrl+C to stop:
ping -c 3 example.comSome 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:
dig +short localhost
dig +short NS example.com | sort | head -n 2127.0.0.1
elliott.ns.cloudflare.com.
hera.ns.cloudflare.com.Local names can be set in /etc/hosts, which is read before DNS:
grep -m1 localhost /etc/hosts127.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:
sudo ufw allow OpenSSH
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw statusCloud servers usually have a separate firewall in the provider's dashboard, often called a security group.
A troubleshooting order#
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
ss -ltnH | wc -l | awk '{print ($1 > 0) ? "Some ports are listening" : "No ports listening"}'
getent ahostsv4 localhost | head -n 1 | awk '{print $1}'Some ports are listening
127.0.0.1Key takeaway#
Finished this lesson?