Cheat sheet
ss, nc and curl cheat sheet
Ports and connections on one page: what is listening (ss), talking to a port by hand (nc), and HTTP requests (curl).
ss
Lists sockets: which ports are listening, which connections are open, and TCP's view of each.
ss -tlnp- Lists the TCP ports this device listens on, with the program behind each.
ss -ulnp- Lists the UDP ports this device listens on.
ss -tan- Lists every TCP connection with its state.
ss -tn state time-wait- Lists the connections that were closed and are waiting out TIME_WAIT.
ss -tin '( dport = :5201 )'- Shows RTT, congestion window and retransmits of connections to port 5201.
ss -tulpn- Lists every listening TCP and UDP port with its program: the old netstat -tulpn.
ss -tnp- Lists established TCP connections with the program that owns each.
ss -s- Prints totals: how many sockets of each kind, and TCP connections by state.
nc
Opens a raw TCP or UDP connection, or listens for one: the simplest way to test a port by hand.
nc -zv -w2 10.0.2.10 80- Checks whether port 80 on 10.0.2.10 accepts TCP connections.
echo hi | nc -w1 10.0.2.10 7- Sends one line to the echo service on port 7 and prints the answer.
echo hello | nc -u -w1 10.0.2.10 9999- Sends one UDP datagram to port 9999.
nc -l -p 9000- Listens on TCP port 9000 and prints whatever a client sends.
nc -l 9000- Listens on port 9000 with OpenBSD nc (Debian, Ubuntu) or ncat (Fedora, RHEL); traditional and BusyBox nc need -l -p 9000.
nc 10.0.2.10 9000- Connects to that listener: each line typed on one side appears on the other.
nc -lk 9000- Keeps listening after a client leaves, for the next one (OpenBSD nc and ncat).
nc -zv 10.0.2.10 20-25- Checks a range of ports, one line each (OpenBSD nc; ncat takes one port).
curl
Sends an HTTP or HTTPS request and prints the answer: the page, the headers, or the whole conversation.
curl -s http://webServer.lab/- Fetches the page and prints it.
curl -sv http://webServer.lab/- Fetches the page and shows every step: connecting, the request and the response headers.
curl -sI http://webServer.lab/- Fetches only the response headers.
curl -s -m 5 -o /dev/null -w '%{http_code} %{time_total}\n' http://webServer.lab/- Prints only the status code and total time, giving up after five seconds.
curl -s -m 5 http://10.0.2.10:81/; echo exit=$?- Tries a port and prints curl's exit code, which names what failed.
curl -sv https://bank.lab/ 2>&1 | grep -iE 'subject|issuer|expire'- Shows who the server's certificate names and who signed it.
curl -s -x http://webProxy.lab:3128 http://newsSite.lab/- Fetches the page through a forward proxy, which makes the request on your behalf.
curl -sL http://example.com/- Follows redirects (301, 302, 307, 308) to the final page instead of printing the redirect itself.