brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: UMask= in a systemd unit sets the permissions of every file a service creates

Permissions
~/tips/tip-permissions-systemd-umask

sudo systemctl edit myapp.service

Controls the default mode of new files written by one service.

A shell’s umask doesn’t reach a service started by systemd. Those get 0022 by default, so every file they create is readable by all users. One line in the unit changes that.

Add a drop-in for the service:

sudo systemctl edit myapp.service
[Service]
UMask=0027

After sudo systemctl restart myapp, new files come out as 640 and new folders as 750. The owner can read and write, the group can read, and other users get nothing. For a service that writes logs, exports or uploaded files, that’s usually what you want.

Check what a service runs with now:

systemctl show -p UMask myapp.service

I like 0027 for apps that share files with a web server through a group, and 0077 for anything that only the service itself should read.

The umask only removes bits, it never adds them. If the program creates a file with mode 600 itself, a looser umask won’t open it up. And existing files keep their permissions; fix those once with chmod. Some programs set their own umask at start, which overrides this, so if nothing changes, look in the app’s own settings.

Further reading: systemd.exec(5): UMask=

More Permissions tips sudo su -s /bin/bash www-data sudo setfacl -R -d -m g:media:rwX /srv/media sudo cp -r /srv/site /srv/site.r ls -ln /srv/data newgrp media All Permissions tips (30) All topics

Link