Sub topic 13
Running Jobs with Least Privilege
Why job permissions matter
Assume you are comfortable with the Linux command line, shell scripts, users, file permissions, processes, and logs.
A scheduled job runs without a person watching it. If it runs as root, one mistake in the script can change or delete almost anything on the server. A compromised tool, unsafe input, or incorrect path can have the same effect. Least privilege means giving a job only the users, files, commands, and network access it needs.
Use a dedicated service account for important jobs. Do not reuse a human login account. Do not run a job as root unless the job truly needs unrestricted access.
Create a dedicated account
A service account is a user created for one application or task. It normally has no interactive login shell and owns only the directories it must modify.
For example, a backup job might read a database through a controlled command and write to /var/backups/app. The backup account does not need permission to edit application code or log in over SSH.
sudo useradd --system --home-dir /var/lib/app-backup \
--create-home --shell /usr/sbin/nologin app-backup
sudo install -d -o app-backup -g app-backup -m 0750 /var/backups/app
sudo install -o app-backup -g app-backup -m 0750 /dev/null /var/log/app-backup.logThe --system option creates a system account. The nologin shell prevents normal interactive login. The directory mode 0750 gives the owner full access, the group read and execute access, and other users no access.
Check every path in the job. The account needs execute permission on parent directories and the required read or write permission on the final file. A job can fail even when the final file has the expected mode if a parent directory blocks traversal.
Use systemd restrictions
A systemd service unit can select the account and add extra boundaries. ReadWritePaths limits writable locations when combined with ProtectSystem. PrivateTmp gives the service a separate temporary directory. NoNewPrivileges prevents the process from gaining more privileges through set-user-ID programs or file capabilities.
# /etc/systemd/system/app-backup.service
[Unit]
Description=Application backup
[Service]
Type=oneshot
User=app-backup
Group=app-backup
ExecStart=/usr/local/bin/app-backup.sh
WorkingDirectory=/var/lib/app-backup
ProtectSystem=strict
ReadWritePaths=/var/backups/app
PrivateTmp=true
NoNewPrivileges=trueThese settings can expose hidden dependencies. The script may need another writable directory, a device, or access to a Unix socket. Add only the specific permission required, then test again. Do not remove all restrictions just because one path failed.
Test as the real account
Run the script manually as the service account before scheduling it. This catches missing permissions without waiting for the next timer activation.
sudo -u app-backup -- /usr/local/bin/app-backup.sh
sudo systemctl start app-backup.service
sudo journalctl -u app-backup.service -n 50 --no-pager
systemctl show app-backup.service -p User -p Group -p NoNewPrivilegesIf a job needs one privileged action, isolate that action rather than granting full root access. A narrowly scoped sudoers rule or a separate privileged service is safer than running the entire script as root. Review the rule for unsafe arguments, writable scripts, and commands that can launch another shell.
Exercise
What is safer for a backup job: running the entire script as root, or using a dedicated account with access only to the backup destination?
Show answer
Use a dedicated account. Grant root access only to a small, reviewed operation if one step genuinely requires it.