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.
ss -tanThe 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.
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.
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.
ss -tan > sockets.txt
head -1 sockets.txtThe file starts with the header line. The lab checks the header and asks you to record a port test as well.