Linux › Section J · Network applications and services › Topic J.1
The basics of network services
A web page, a remote login and a name lookup all work the same way underneath: a client program opens a connection to a server program waiting on a port, and the two exchange bytes. Talking to a server by hand, then watching a real client do it, shows exactly where the kernel's job ends and the application's begins.
Clients, servers and services
A network service is a server program, usually a daemon started by systemd, that waits for requests on a port: a web server on TCP port 80 or 443, an SSH server on TCP 22, a DNS resolver on port 53. A client is the program that starts the conversation, such as a browser, ssh or curl. Both are ordinary user-space processes. The kernel's transport layer, described in the topic "The transport layer: TCP, UDP, and services", delivers their bytes to the right port; what those bytes mean is decided entirely by the two programs, using an application-layer protocol such as HTTP. This topic looks at that top layer from a Linux command line.
Port names and /etc/services
Port numbers have conventional names, listed in /etc/services, a plain text file with lines in the form name, port/protocol and optional aliases, for example ssh 22/tcp or https 443/tcp. Tools use it in both directions: they print https instead of 443 unless told not to, and many accept a name where a number is expected. The file is only a lookup table: listing a port there does not start or allow anything, and a server may listen on any port it likes. On Linux, binding to a port below 1024 normally needs root or the CAP_NET_BIND_SERVICE capability, which is why services that start as root often drop to an unprivileged user afterwards.
A TCP service is a two-way byte stream
Once a TCP connection is open, each side simply writes bytes and reads what the other wrote, in order and without gaps, as the Networking topic "Connection-oriented transport: TCP" explains. Many classic protocols, HTTP/1.1 and SMTP among them, send plain text lines ending in carriage return and line feed. That means you can play the client yourself if you have a tool that connects to a port and passes your keystrokes through. Three are common. telnet host 80 was designed for remote login and is still handy as a test client, though it is often not installed. nc host 80 (netcat) does the same for TCP or UDP. And bash itself can open a connection through the special path /dev/tcp/host/port.
Worked example: a request typed by hand
These commands were run in bash against the documentation site example.org (real output, trimmed). 1. Open a connection on descriptor 3: exec 3<>/dev/tcp/example.org/80. bash asks the kernel to connect, and the TCP handshake happens now. 2. Send a request: printf 'GET / HTTP/1.1\r\nHost: example.org\r\nConnection: close\r\n\r\n' >&3. The empty line at the end tells the server the request is complete. 3. Read the reply: head -c 300 <&3 printed HTTP/1.1 200 OK, then header lines such as Content-Type: text/html; charset=utf-8 and Connection: close. 4. Close the descriptor: exec 3>&-. Connection: close asked the server to hang up after replying, so the read did not wait for ever.
Encrypted services need a TLS client
Most services on the internet now use TLS, so the bytes on the wire are encrypted and typing GET at port 443 gets nowhere. The Networking topic "Securing TCP connections: TLS" explains the handshake. For testing, openssl s_client -connect example.org:443 -servername example.org performs the TLS handshake, prints the server's certificate chain and then lets you type a request exactly as before, with openssl encrypting it. The -servername option sends the host name (Server Name Indication), which servers hosting many sites need in order to pick the right certificate. This is also the quickest way to see a certificate's expiry date when a site starts showing warnings.
curl: a client that knows the protocol
curl speaks HTTP, HTTPS and many other protocols for you. curl https://example.org/ prints the page; -o page.html saves it; -I asks for the headers only; -L follows redirects. To see the conversation, -v prints the connection steps and headers, marking lines you send with > and lines received with <, and --trace-ascii file records everything, including byte counts, in a file. curl is installed on most systems; if not, apt install curl or dnf install curl adds it. wget is a similar tool aimed at downloading files, and both are ordinary user-space clients making the same system calls as any other program.
Worked example: reading a curl trace
curl --trace-ascii trace.txt -o page.html http://example.org/ was run with curl 8.10.1 (real output). 1. The Info lines show transport-layer work: the name resolved to IPv4 addresses, Trying 104.20.26.136:80 (the address varies), then Connected. 2. Then the application layer starts: Send header, 75 bytes, followed by GET / HTTP/1.1, Host: example.org, User-Agent: curl/8.10.1, Accept: */* and an empty line. 3. Check the count: each line ends in two bytes, CR and LF, so 16 + 19 + 25 + 13 + 2 = 75 bytes. 4. The reply begins Recv header, 17 bytes: HTTP/1.1 200 OK plus CR LF is 15 + 2 = 17. 5. After the headers come Recv data lines carrying the HTML, saved to page.html.
Where the application layer lives
The trace labels some bytes as headers and others as data, but the kernel and the network treat them identically: curl received everything through the same read calls on one socket. The difference exists only inside curl, which knows the HTTP rule that a blank line ends the headers and that a Content-Length header, or chunked encoding, says where the body stops. The server works the same way in reverse. This is the practical meaning of layering: the kernel provides a reliable stream to a port, and each application gives that stream its meaning. The Networking topic "The web and HTTP" covers the protocol itself.
Which services is this machine running?
To list the services listening for connections, run sudo ss -tlnp: -t for TCP, -l for listening sockets, -n for numbers instead of names and -p for the owning process. Add -u to include UDP. The older netstat -tlnp gives similar output but is legacy, part of the net-tools package that many distributions no longer install. On the systemd side, systemctl list-units --type=service shows running services, and systemctl list-sockets shows socket units, through which systemd itself listens on a port and starts the service only when a connection arrives. The topic "Diagnostic tools and remote procedure calls" goes further with ss, lsof and tcpdump.
Common mistakes to avoid
First, typing a plain-text request at an HTTPS port; use openssl s_client or curl. Second, forgetting the empty line that ends an HTTP request, so the server waits for more. Third, assuming /etc/services controls which ports are open; it only maps names to numbers. Fourth, expecting the kernel to understand headers and bodies; that split is inside the application. Fifth, reaching for telnet or netstat first on a modern system, where nc, curl and ss are more likely to be installed.
Key takeaways
A network service is a user-space server waiting on a port; a client starts the conversation. /etc/services maps port names to numbers, and ports below 1024 need privilege to bind. A TCP connection is a two-way byte stream, so text protocols can be spoken by hand with nc, telnet or bash's /dev/tcp. TLS services need openssl s_client or a TLS-aware client such as curl, whose -v, -I, -L, -o and --trace-ascii options show the conversation. Headers and bodies are distinguished by the application, not the kernel. ss -tlnp and systemctl list-sockets show what is listening.
Flashcards
Flashcard
A server program waiting for requests on a port
Test your understanding
Question 1 of 5
You connect to port 443 of a web server with nc and type GET / HTTP/1.1. Nothing sensible comes back. Why?
Question 2 of 5
An HTTP request sends four header lines of 16, 19, 25 and 13 bytes (each including CR LF), then the blank line. How many bytes is the request?
Question 3 of 5
What does adding a line ssh 2222/tcp to /etc/services do?
Question 4 of 5
In a curl trace, the kernel delivered the response headers and the HTML body. How did curl know where the headers ended?
Question 5 of 5
Which command lists the TCP ports on which this machine is listening, with the owning processes?
