Socket activation

On this page 7

Socket activation is a system initialization technique in Linux in which the init system creates and holds a listening socket on behalf of a service and starts that service only when the first connection or datagram arrives. The service then receives the already open file descriptor instead of creating the socket itself.

On modern distributions the technique is implemented by systemd through .socket units, building on ideas from the classic inetd superserver and from launchd on macOS. It serves three goals at once: faster and more parallel boot, on-demand start of rarely used services and connection preservation across service restarts.

How socket activation passes sockets to services

Socket activation works by separating socket creation from the service process. At boot, systemd reads each socket unit, calls socket(), bind() and listen() itself, and adds the resulting descriptor to its event loop. No service code runs at this stage, so the port or path is reserved and ready to accept clients within milliseconds of boot.

When traffic arrives, systemd starts the paired service unit and hands over the descriptor. The handover follows the sd_listen_fds protocol: the descriptors begin at file descriptor 3, and the environment variables LISTEN_FDS, LISTEN_PID and LISTEN_FDNAMES tell the process how many sockets it received and what they are called. A daemon that supports the protocol simply skips its own bind step.

The following table lists the main stages of one activation cycle and who performs each step.

StagePerformed byResult
Socket setupsystemd at bootPort or path bound and listening
Event waitsystemd event loopNo service process consumes memory
TriggerFirst client connectionPaired service unit is queued
Descriptor handoversystemd via fork and execService inherits fd 3 and up
ServingService processQueued clients are accepted in order
Stages of a socket activation cycle

Because the kernel keeps accepting connections into the listen backlog while the service starts, early clients wait briefly rather than receiving a connection refusal. This kernel-side queuing is what makes boot ordering less strict.

Socket unit types and key directives

A socket unit describes what to listen on and how to dispatch traffic. It normally shares its name with the service it activates, so cups.socket starts cups.service, although the Service= directive can point elsewhere.

The most important directives and their typical values are summarized below.

DirectiveTypical valueEffect
ListenStream=80 or /run/app.sockTCP or Unix stream socket
ListenDatagram=514UDP or Unix datagram socket
ListenFIFO=/run/app.fifoNamed pipe instead of a socket
Accept=no (default) or yesOne service for all or one per connection
Backlog=Up to the kernel somaxconn limitSize of the pending connection queue
SocketMode=0660File permissions for Unix sockets
FileDescriptorName=httpName exposed in LISTEN_FDNAMES
Common directives in a .socket unit

The Accept= setting defines the two dispatch models. With Accept=no, a single long-running service receives the listening socket and accepts all connections itself, which suits most daemons. With Accept=yes, systemd accepts each connection and spawns a separate templated instance such as sshd@.service, mirroring the classic inetd model and suiting only low-volume services.

Socket activation compared with other start methods

Socket activation is one of several ways to start daemons, and each method answers a different question about when a service should run. Neighboring concepts in system initialization, such as the systemd timer, the systemd target and the traditional SysVinit script, solve related but distinct problems.

MethodStart triggerTypical use
Socket activationIncoming connection or datagramNetwork and IPC daemons
Boot-time startTarget reached during bootAlways-needed core services
Timer activationCalendar or monotonic timeScheduled jobs and cleanup
Path activationFile or directory changeSpool and drop-folder processing
D-Bus activationRequest for a bus nameDesktop and system bus services
inetd or xinetdConnection on a listed portLegacy per-connection services
Service start methods in Linux system initialization

Compared with inetd, the systemd approach supports both per-connection and single-instance models, Unix sockets, FIFOs and netlink, and it integrates with dependency ordering and cgroup-based resource control. Upstart and OpenRC offered only partial equivalents, which is one reason socket activation became a distinguishing feature of systemd.

Benefits for boot and service management

The central benefit is that socket availability replaces explicit ordering. If a client service starts before the server, its connection simply waits in the queue, so systemd can launch both in parallel without an After= dependency. This removes a large class of race conditions during boot.

  • On-demand resource use: a print or debugging daemon consumes no memory until someone needs it.
  • Zero-downtime restarts: systemd keeps the listening socket open while the service restarts, so no connection is refused during an upgrade.
  • Privilege separation: systemd binds a privileged port such as 443, and the service can then run as an unprivileged user without CAP_NET_BIND_SERVICE.
  • Crash resilience: if the service fails, the socket survives and the next connection starts a fresh instance.

Tools such as systemd-analyze make these effects visible, because services that are socket-activated usually disappear from the critical chain of the boot sequence.

Limitations and common configuration mistakes

Socket activation is not free: the first request pays the full startup latency, which can range from milliseconds for a small daemon to several seconds for a heavy runtime. Services that must answer instantly, or that need warm caches, are usually better started at boot.

The table below lists frequent problems, their causes and practical fixes.

SymptomLikely causeFix
Address already in useDaemon binds the port itselfEnable sd_listen_fds support in the daemon
Service restarts in a loopAccept=yes with a non-inetd daemonSet Accept=no for long-running services
Thousands of processesAccept=yes under heavy trafficSwitch to a single-instance service
Socket not listening after bootOnly the .service unit is enabledEnable the .socket unit with systemctl
Permission denied on Unix socketWrong SocketUser or SocketModeAdjust ownership and mode directives
Typical socket activation problems

A frequent misconception is that any daemon becomes socket-activated once a socket unit exists. In reality the program must accept inherited descriptors, either through libsystemd or by reading the environment variables directly; otherwise it tries to bind the same address and fails. Debugging is easier with systemd-socket-activate, which emulates the handover for testing, and with journalctl output for both units.

In everyday language

Socket activation works like a receptionist who answers the door for an office that is still empty. The computer opens the entrance for a program right away, but the program itself stays asleep. When the first visitor knocks, the receptionist wakes the right worker and passes the visitor straight to them.

Visitors never find a locked door, and workers who are rarely needed do not sit around using energy. The result is a faster startup and less wasted memory, while every request still reaches the correct program.

Seen in the real world

A system administrator runs a fleet of small Linux virtual machines that each host an internal metrics API, a CUPS print service and an SSH server. Memory is limited to 1 GB per machine, and the print service is used only a few times per week.

The administrator enables cups.socket instead of cups.service, so the print daemon starts only when a job arrives on port 631. For the metrics API, a socket unit with Accept=no and ListenStream=8080 lets systemd hold the port during deployments; when a new version is installed and the service restarts, clients see a pause of about one second instead of refused connections.

Measured with systemd-analyze, boot time drops by roughly a second, idle memory use falls noticeably, and deployments no longer cause failed health checks, because the socket stays open throughout every restart.