Linux › Section J · Network applications and services › Topic J.5
Network sockets and Unix domain sockets
Every network program, and a great many programs that never touch a network, talk through sockets. Seeing how a server uses them, and how Unix domain sockets carry traffic between processes on one machine, explains much of what ss and lsof show on a running system.
Sockets: where user space meets the network
A process cannot reach a network card directly. Instead it asks the kernel for a socket, an endpoint for communication, and receives an ordinary file descriptor. From then on it sends and receives with system calls such as send(), recv(), sendto() and recvfrom(), or the familiar read() and write(), and the kernel's transport and internet layers do the rest. The socket is therefore the boundary between an application in user space and the network stack in the kernel. The Networking topic "Socket programming" builds UDP and TCP clients and servers in Python; this topic looks at the same calls from the operating system's side and adds sockets that never leave the machine.
Families and types
Two choices define a socket. The address family says what kind of address it uses: AF_INET for IPv4, AF_INET6 for IPv6 and AF_UNIX (also called AF_LOCAL) for local sockets named by file paths. The type says how data flows: SOCK_STREAM gives a reliable, ordered byte stream with connections, which over IP means TCP, and SOCK_DGRAM gives separate messages without connections, which over IP means UDP. A third type, SOCK_SEQPACKET, keeps message boundaries over a connection. Because the rest of a program reads and writes in the same way whatever the family, changing from IPv4 to IPv6 or to a local socket mostly means changing the set-up code, not the code that handles data.
How a TCP server uses two kinds of socket
A server first creates a socket and calls bind() to attach it to an address and port, then listen() to mark it as a listening socket (the Networking course's welcoming socket) that queues incoming connection requests. Its main loop calls accept(), which waits for a client and returns a brand new socket, with its own descriptor, connected to that one client. The listening socket is never used for data; it stays open to accept the next client. A classic server then calls fork(): the child serves the client on the new socket and exits, while the parent closes its copy and goes back to accept(). The client's side is simpler: socket(), then connect(), which performs the TCP handshake.
Worked example: matching ss output to the calls
A web server, PID 900, has two clients connected (illustrative ss -tnp output, trimmed). 1. sudo ss -tlnp shows LISTEN 0 511 0.0.0.0:80 with users:(("nginx",pid=900,fd=6)). That is the listening socket from bind() and listen(), on descriptor 6. 2. sudo ss -tnp state established '( sport = :80 )' shows two lines, 10.0.0.2:80 10.0.0.7:51544 with fd=11 and 10.0.0.2:80 10.0.0.9:40112 with fd=12. 3. Interpret: each accept() returned a new descriptor, 11 and 12, one per client. 4. All three share local port 80; the kernel tells them apart by the full set of source and destination addresses and ports. 5. On the listening line, the send-queue figure of 511 is the backlog limit set by listen().
Unix domain sockets
Processes on the same machine can talk over the loopback address 127.0.0.1, but most local services use Unix domain sockets instead. They offer the same stream and datagram types and the same calls, including listen() and accept(), but the data never enters the network stack: the kernel copies it straight from one process to the other. That makes them faster, and it removes any chance of the service being reached from the network. A named Unix socket appears in the filesystem as a socket file, shown by ls -l with the type letter s. Examples include /run/dbus/system_bus_socket for D-Bus, /run/systemd/journal/socket for the journal and /run/mysqld/mysqld.sock for MySQL.
Worked example: a local echo service in Python
These two short programs need Python 3 on Linux (illustrative output). The server: line 1, import os, socket; line 2, path = "/tmp/upper.sock"; line 3, s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM); line 4, s.bind(path); line 5, s.listen(1); line 6, conn, addr = s.accept(); line 7, conn.sendall(conn.recv(1024).upper()); line 8, conn.close(); s.close(); os.unlink(path). The client: c = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM), then c.connect("/tmp/upper.sock"), c.sendall(b"hello") and print(c.recv(1024)). 1. With the server waiting, ls -l /tmp/upper.sock showed srwxr-xr-x. 2. The client printed b'HELLO'. 3. After line 8 the socket file had gone.
Permissions, identity and stale files
Because a named Unix socket is a file, ordinary permissions decide who may connect: a process needs write permission on the socket file. That is a simple, strong access-control tool, and it cuts both ways. /var/run/docker.sock, for example, is usually owned by root and the docker group with mode srw-rw----, and anyone who can write to it can tell the Docker daemon to start a container with full access to the host, so membership of the docker group is effectively root. A server can also ask the kernel who is connecting, through the SO_PEERCRED option, which returns the client's PID, UID and GID. If a server crashes without removing its socket file, its next bind() fails with "Address already in use" and clients get "Connection refused"; delete the stale file before restarting.
Abstract and unnamed sockets
Not every Unix socket has a file. Linux also supports an abstract namespace: a socket address that starts with a null byte has no filesystem entry, vanishes automatically when the last socket closes and cannot be controlled with file permissions; ss shows such names with a leading @. Unnamed sockets have no address at all. The socketpair() call creates two already-connected sockets, and after a fork() parent and child each keep one end, much like a two-way pipe. Unix sockets can even pass open file descriptors from one process to another, a feature servers use to hand connections to worker processes and that systemd's file-descriptor store uses to keep a service's descriptors across a restart. (Socket activation itself works differently: the service simply inherits the listening socket when systemd starts it.)
Seeing Unix sockets on a system
ss -x lists Unix domain sockets and ss -xl only the listening ones; add -p, with sudo, for the owning processes. sudo lsof -U gives a similar list, and its NAME column shows a path for named sockets and only the socket type, such as type=STREAM, for unnamed ones, which are the large majority on a desktop. find /run -type s lists socket files under /run. In output from lsof or /proc/PID/fd, a descriptor shown as socket:[48211] can be any kind of socket; the number is its inode, which ss -e can match. On systemd systems, systemctl list-sockets shows the sockets systemd itself listens on, both network ports and Unix paths.
Common mistakes to avoid
First, trying to send data on a server's listening socket instead of the one accept() returns. Second, using TCP over 127.0.0.1 for a service that never needs the network, when a Unix socket would be faster and unreachable from outside. Third, giving a socket file loose permissions, or adding users to the docker group without realising it grants root. Fourth, forgetting to remove a stale socket file, so the next start fails with "Address already in use". Fifth, assuming every Unix socket has a file; abstract and unnamed ones do not.
Key takeaways
A socket is a file descriptor for communication, the boundary between applications and the kernel's network stack. A family (AF_INET, AF_INET6, AF_UNIX) and a type (SOCK_STREAM, SOCK_DGRAM) define it. Servers bind() and listen() on one socket and get a new one from each accept(); classic servers then fork(). Unix domain sockets use the same calls but stay inside the kernel, faster and unreachable from the network. Socket files have type s and their permissions control access; SO_PEERCRED identifies peers. Abstract and socketpair() sockets have no file. ss -x, lsof -U and systemctl list-sockets show them.
Flashcards
Flashcard
Communication endpoint, used through a file descriptor
Test your understanding
Question 1 of 5
A TCP server has called bind(), listen() and then accept(), which has just returned. Which socket should it use to talk to the client?
Question 2 of 5
Why do many local services use a Unix domain socket instead of TCP on 127.0.0.1?
Question 3 of 5
ls -l shows srw-rw---- 1 root docker for /var/run/docker.sock. Who can send commands to the Docker daemon through it?
Question 4 of 5
A service crashed and now fails at start-up with "Address already in use" on its Unix socket path. What is the likely fix?
Question 5 of 5
ss -x lists a socket named @/tmp/.X11-unix/X0, but no such file exists. Why?
