Skip to content

Run as a service without root (systemd)

This is a way to start uFTP with fewer privileges. The program is the same, and as root it keeps working as before.

Stopping: systemctl stop sends SIGTERM; uFTP closes the idle clients and lets the transfers in progress end, up to SHUTDOWN_GRACE_SECONDS (30 s). The unit has TimeoutStopSec=60, longer than that. See Configuration.

Tried: the filters that do not need privileges (SystemCallFilter=@system-service, MemoryDenyWriteExecute, RestrictAddressFamilies, LockPersonality, NoNewPrivileges) were run on uFTP 4.4.0 with a transient user unit (systemd-run --user): FTP and FTPS logins, uploads, checksums, MFMT, an authentication hook, SITE RUN jobs (they fork, execve and close_range) all work under them, and systemctl stop ends with Result=success. The directives that need a system service (User=, ProtectSystem=strict, PrivateDevices, the capabilities) were not tried here: try the unit on your system with systemd-analyze security uftp.service and journalctl -u uftp.

Why it helps: if a bug in a server is ever used against it, the damage stops at what the user of the service can touch. With root that is the whole system.

The files are in the repository, in contrib/systemd/: uftp.service, a sample uftpd.cfg and a README.md.

Install

# the user, the folders, the program and the files
sudo useradd --system --home-dir /srv/ftp --shell /usr/sbin/nologin uftp
sudo install -d -o uftp -g uftp /srv/ftp /srv/ftp/ftpuser
sudo install -d -m 750 -o root -g uftp /etc/uftp
sudo install -d -m 750 -o root -g uftp /etc/uftp/tls
sudo install -m 755 build/uFTP /usr/local/sbin/uFTP
sudo install -m 640 -o root -g uftp contrib/systemd/uftpd.cfg /etc/uftp/uftpd.cfg
sudo install -m 644 contrib/systemd/uftp.service /etc/systemd/system/uftp.service

Enable the three account lines in /etc/uftp/uftpd.cfg and set the password before starting. With OpenSSL, generate a hash using /usr/local/sbin/uFTP --hash-password and configure the certificate and private key.

sudoedit /etc/uftp/uftpd.cfg
sudo systemctl daemon-reload
sudo systemctl enable --now uftp
systemctl status uftp
journalctl -u uftp

What the unit does

Option Effect
User=uftp the service is not root
AmbientCapabilities=CAP_NET_BIND_SERVICE the only privilege it keeps: opening port 21
NoNewPrivileges=yes it cannot get more privileges
ProtectSystem=strict, ReadWritePaths=/srv/ftp, LogsDirectory=uftp the disk is read-only, except the folder of the FTP users and the log folder
ProtectHome, PrivateTmp, PrivateDevices, ProtectKernel*, ProtectControlGroups the service does not see home folders or other users' /tmp; its private /tmp is writable. It also hides devices and kernel settings
RestrictAddressFamilies, RestrictNamespaces, SystemCallFilter=@system-service only the network and the system calls a service needs
LimitNOFILE=65536 room for many connections

These options belong to systemd, not to uFTP. If the service does not start, or a command fails, read journalctl -u uftp and relax the option it names.

Write access for SITE RUN jobs

ProtectSystem=strict makes the filesystem read-only inside the service, even when the service runs as root. Writable locations include paths in ReadWritePaths, systemd's LogsDirectory and RuntimeDirectory, and the private temporary directories from PrivateTmp=yes. This also applies to SITE RUN scripts and backup jobs: if a command uses mktemp under /backup, for example, systemd reports Read-only file system until /backup is allowed.

The same applies to the home of an FTP user that publishes a web site (for example HOME_n = /var/www/example/public_html): PERMISSIONS_n = all gives the FTP permissions, but the service still needs the directory in ReadWritePaths to create files or directories there. Add it as well:

[Service]
ReadWritePaths=/srv/ftp /var/www/example/public_html

Add every directory that a job must write to, including its temporary-file directory, to the existing ReadWritePaths list in a service drop-in. Keep the paths already there. For example, to allow /backup:

[Service]
ReadWritePaths=/srv/ftp /backup

Open the drop-in with sudo systemctl edit uftp for the service in this guide, or sudo systemctl edit uftpserver.service for the Debian and Ubuntu package. Then apply it with sudo systemctl daemon-reload and restart the matching service. The directory must already exist, and its underlying filesystem must be mounted read-write. For an unprivileged service, the uftp account also needs Unix write and search permissions on it. Do not add / as a writable path, since that removes the filesystem write restriction.

The configuration is in /etc/uftp/uftpd.cfg, readable by the group uftp and not by everybody: uFTP reads ./uftpd.cfg first, and the unit starts it in that folder. RuntimeDirectory=uftp provides the writable lock path for SINGLE_INSTANCE=true. The sample sets SINGLE_INSTANCE=true, DAEMON_MODE=false and RESPAWN_PROCESS=false: uFTP prevents a second instance while systemd keeps it in the foreground and restarts it. It uses the same passive-port range (10000–50000), connection limits and timeouts as the source configuration. Open that full range in the firewall, or narrow it in both the config and firewall if you need fewer ports.

What does not work without root

  • Logging in system users with PAM (ENABLE_PAM_AUTH) needs privileges: contrib/systemd/pam-system-users.conf is a drop-in that runs the same unit as root with a few capabilities only.
  • Giving new files to another owner (USER_NAME_OWNER_n): changing an owner needs root. The log says so at startup, and the files belong to uftp.

For the Debian or Ubuntu package, the supplied drop-in also supports USER_NAME_OWNER_n. Install it with:

sudo install -D -m 644 /usr/share/doc/uftpserver/examples/systemd/pam-system-users.conf /etc/systemd/system/uftpserver.service.d/pam-system-users.conf

Read the file before restarting: it runs the service as root with a limited capability set. ProtectSystem=strict still applies, so every job directory that must be writable must be added to ReadWritePaths in the drop-in. For example, if a job writes to /backup, extend its line to include /backup while preserving the other paths:

ReadWritePaths=/srv/ftp /home /backup

Then run sudo systemctl daemon-reload and sudo systemctl restart uftpserver. The configured user homes also matter: a virtual FTP account with HOME = / can access a broad part of the filesystem. Give such accounts narrower homes when they do not need system-wide access.

systemctl reload uftp reads the TLS certificate and the users (the configuration file and USERS_FILE) again, without closing anybody: a new user or a new password needs no restart. The rest of the configuration (ports, folders, the limits of the server, BLOCK_USER_n) needs sudo systemctl restart uftp; uFTP --check tells before what is wrong.

Certificates

The certificate and the key must be readable by the group uftp, not by everybody. Copy them to /etc/uftp/tls with mode 640 and group uftp:

sudo install -m 640 -o root -g uftp /etc/letsencrypt/live/example.com/fullchain.pem /etc/uftp/tls/cert.pem
sudo install -m 640 -o root -g uftp /etc/letsencrypt/live/example.com/privkey.pem /etc/uftp/tls/key.pem
sudo systemctl reload uftp

Do the same after every renewal: contrib/certbot/uftp-deploy-hook.sh is a ready hook for /etc/letsencrypt/renewal-hooks/deploy/. More in TLS and certificates.

Passive ports and the firewall

Open the control port (21) and the passive range of the configuration (RANDOM_PORT_START to RANDOM_PORT_END). Behind a NAT see Behind a NAT or a firewall.