$_ CLI In Depth
Learning progress 0%
lesson networking sockets ssh

Sockets & Ports with ss

3 min read Module 16: Networking Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:21

Addresses and ports

A TCP connection is named by four values: the local address and port, and the remote address and port. A server listens on one local port, and each client connection gets its own socket on the server, so many clients can talk to one port at the same time. The port number alone does not identify a connection; the full four-tuple does.

Port numbers below 1024 are the well-known range, reserved for system services, and binding to them normally requires root. Ports from 1024 upward are available to ordinary users. The client side usually gets a random high port chosen by the kernel.

Listing sockets with ss

The flags you will use most are -t for TCP, -u for UDP, -l for listening sockets, -n to print numbers instead of service names, and -a for all sockets. Combined, ss -tln lists TCP ports where a server waits for connections.

BASH1 lines
ss -tan

The first line is the header: State, Recv-Q, Send-Q, Local Address:Port, and Peer Address:Port. Each later line is one socket. On the lab machine the output may contain only the header, because no server is running. The header alone is still a useful sample, because it shows the layout you would read on a busy server.

Warning

In this lab image, ss does not always show a listener that a test program has just opened. Do not depend on ss output to prove a port is open. Test the port by connecting to it, as the next section shows.

States of a connection

Each TCP socket has a state. LISTEN means the socket is waiting for connections. ESTAB means a connection is open. TIME-WAIT means the local side has closed and the kernel is holding the record briefly so that late packets are not misdelivered. Reading the state tells you whether a service is waiting or a client is connected.

Testing a port by connecting

Bash can open TCP connections without any extra program. The path /dev/tcp/HOST/PORT is special: redirecting to it opens a connection, and the redirection fails when nothing listens. This is a dependable test, because it asks the kernel to complete the handshake.

BASH1 lines
bash -c 'exec 3<>/dev/tcp/127.0.0.1/9999' 2>/dev/null; echo "status=$?"

If nothing listens on port 9999 the status is 1, which means connection refused. If a server is listening, the status is 0. The exec with 3 opens the connection on file descriptor 3, and closing it happens when the bash process exits.

Who owns the port

To find which process owns a socket on a normal Linux system, ss can print process information with the -p option. That works only for sockets your user is allowed to inspect, and it does not change what the socket does. Use it when two programs fight over a port and you need the name of the one that holds it.

Hands-on: write the socket listing to a file

Run the commands below. The first saves the TCP socket table, and the second tests whether anything listens on port 9999. The lab asks you to save both results in your home directory.

BASH2 lines
ss -tan > sockets.txt
head -1 sockets.txt

The file starts with the header line. The lab checks the header and asks you to record a port test as well.

A TCP connection on loopback, from listen to close

Visualizer

A TCP connection starts with a three-packet handshake and ends with a four-packet close. Watch the kernel state of each socket as the packets pass, the way ss -tan shows them.

1 / 5
socket()
bind(127.0.0.1:9000)
listen(backlog)
accept() waits
Three-way handshake, as ss shows it
StepPacketClient socketServer connection
server: listen()--LISTEN (listener)

The server calls socket(), then bind() to pick the address and port, then listen(). listen() makes the socket a passive listener with a queue for finished connections. Only the listener exists so far.

nc -l -s 127.0.0.1 -p 9000 & pid=$!; sleep 0.3; ss -tlnp | grep 9000; kill $pid
Terminal lab · 2 steps · real Linux in your browser

Save a socket table and test a port

Record the TCP socket listing with ss and save the status of a connection to a closed port.

01
Save the TCP socket listing
Create sockets.txt in /workspace with all TCP sockets, printed with numeric addresses. Use ss with the -t, -a, and -n flags: ``bash ss -tan > sockets.txt `` The first line of the file is the header with the column names.
Show hint for step 1
The flags are -t for TCP, -a for all states, and -n for numbers. The header begins with State.
02
Record a refused connection
Create refused.txt in /workspace with the exit status of a bash connection to 127.0.0.1 port 9999. Nothing listens on that port, so the status is not zero: ``bash bash -c 'exec 3<>/dev/tcp/127.0.0.1/9999' 2>/dev/null; echo $? > refused.txt ``
Show hint for step 2
The $? after the bash command is the status of the connection attempt. Write it to refused.txt.
Check yourself Question 1 of 4
Concept Check Listing listening TCP ports recall

Which ss flags list TCP listening sockets with numeric port numbers?

Recap & Key Takeaways

A socket is identified by local and remote addresses and ports, and each accepted client connection gets its own socket on the server.
ss -tln lists TCP listening sockets and ss -tan lists all TCP sockets, and it replaces netstat on modern systems.
Connecting through /dev/tcp is a reliable port test: a refused connection means nothing listens on that port.
In this lab image ss may not show a listener, so confirm a port by connecting to it.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue