Skip to content

Instantly share code, notes, and snippets.

@MitsuhaMiyamizu
Last active May 16, 2020 14:11
Show Gist options
  • Save MitsuhaMiyamizu/db407d23acdf0740553073ad5b0c6fe3 to your computer and use it in GitHub Desktop.
Save MitsuhaMiyamizu/db407d23acdf0740553073ad5b0c6fe3 to your computer and use it in GitHub Desktop.
This is an example of systemd service configuration file.
[Unit]
Description=In this sample , 'foo' is the name of the software.
Documentation=
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
[Service]
Restart=on-failure
; User and group the process will run as.
User=www-data
Group=www-data
; Some sort of files will be written to this directory.
Environment=HOME=/etc/foo/bar
; Always set "-root" to something safe in case it gets forgotten in the bar.conf .
ExecStart=/usr/local/bin/foo -log stdout -agree=true -conf=/etc/foo/bar.conf -root=/var/tmp
ExecReload=/bin/kill -USR1 $MAINPID
; Limit the number of file descriptors; see `man systemd.exec` for more limit settings.
LimitNOFILE=1048576
; Unmodified foo is not expected to use more than that.
LimitNPROC=64
; Use private /tmp and /var/tmp, which are discarded after foo stops.
PrivateTmp=true
; Use a minimal /dev
PrivateDevices=true
; Hide /home, /root, and /run/user. Nobody will steal your SSH-keys.
ProtectHome=true
; Make /usr, /boot, /etc and possibly some more folders read-only.
ProtectSystem=full
; … except /etc/foo/bar, because we want the specified files there.
; This merely retains r/w access rights, it does not add any new. Must still be writable on the host!
ReadWriteDirectories=/etc/foo/bar
; Drop all other capabilities. Important if you run foo as privileged user (which you should not).
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
; … but permit foo to open ports reserved for system services.
; This could be redundant here, but is needed in case foo runs as nobody:nogroup.
AmbientCapabilities=CAP_NET_BIND_SERVICE
; … and prevent gaining any new privileges.
NoNewPrivileges=true
; Caveat: Some plugins need additional capabilities. Add them to both above lines.
; - plugin "upload" needs: CAP_LEASE
[Install]
WantedBy=multi-user.target
@MitsuhaMiyamizu
Copy link
Author

MitsuhaMiyamizu commented Aug 15, 2016

The provided file is written for systemd version 229 or later!

Got an error? Revisit permissions!

Prefer systemctl edit over modifying the unit file directly:
systemctl edit foo.service to make user-local modifications
systemctl edit --full foo.service for system-wide ones

@MitsuhaMiyamizu
Copy link
Author

MitsuhaMiyamizu commented Aug 15, 2016

To view the resulting configuration use
systemctl cat foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment