Stop Running Cron Jobs Under Your Own User Account: A Security and Compliance Guide

Stop Running Cron Jobs Under Your Own User Account: A Security and Compliance Guide

Why scheduled jobs on production servers belong to a dedicated service account, and which SOC 2, ISO 27001, PCI DSS, NIST and CIS controls expect it.

Somewhere on your production servers there is probably a backup script that runs every night under a real person's login. It works, nobody touches it, and it quietly holds the keys to far more than it needs.

Today is January 26, 2023, and this small habit deserves attention right now. ISO/IEC 27001 was revised in October 2022 and many teams are starting their move to the new control set. PCI DSS version 4.0 arrived in March 2022 with new, explicit rules for "system and application accounts". SOC 2 auditors keep asking the same question during access reviews: "Whose account is this, and why does it still exist?" A cron job tied to a person's account is one of the easiest findings an auditor can raise, and one of the easiest for an attacker to abuse.

This guide explains the risk in plain terms, walks through a safe migration with real commands, and maps the change to the specific controls in SOC 2, ISO 27001, PCI DSS, NIST SP 800-53 and the CIS Controls.

A quick refresher: what cron is

Cron is the standard job scheduler on Linux and Unix systems. Each user can have a personal schedule called a crontab, edited with crontab -e. The system also reads schedules from /etc/crontab and the /etc/cron.d/ folder, where each line names the user the job runs as.

The key point: a cron job runs with exactly the identity and permissions of the account that owns it. If it runs as you, it can do everything you can do.

Why a personal account is the wrong owner

Imagine an engineer's account, called engineer1 here, that owns a nightly database backup on a production server. Here is what that really means.

1. The job inherits far more access than it needs

A personal account usually has an SSH key, a login shell, membership in groups like sudo or docker, and read access to many folders. The backup script needs to read one database and write to one folder. Everything else is extra power that the job never uses but an attacker could. This breaks the principle of least privilege, meaning every account should have only the access its task requires.

2. Stealing one person's login means controlling the job

If the engineer's laptop, password or SSH key is compromised, the attacker can edit the crontab or the script it calls. The job then runs the attacker's code every night, on schedule, with no further effort. This is a well-known persistence technique: a way to keep access after the first break-in. Because the change sits inside a normal user's crontab, it blends in.

3. The script is usually writable by its owner

Jobs set up by hand often live in the person's home folder, for example /home/engineer1/scripts/backup.sh. Anything that runs as that user, including a malicious browser extension on a jump host or a compromised tool, can rewrite the script. The safer pattern is that the account that runs a script cannot change it.

4. People leave, and the job becomes a reason not to lock the account

When the engineer changes teams or leaves the company, the offboarding checklist says "disable the account". Disabling it silently stops the backup. So teams often leave the account active "for now". Months later, a departed person's account is still live on production, which is exactly what access reviews are meant to catch.

5. Logs no longer tell the truth

Audit logs record actions by account. When engineer1 both logs in by hand and runs scheduled jobs, you cannot easily tell whether a 2 a.m. database export was the job or a person. Clear accountability, meaning knowing who or what did each action, is lost.

6. Changes skip review

Anyone with the account can run crontab -e and change the schedule or the command on the spot. No pull request, no approval, no record. For a production system that is an uncontrolled change.

And the other extreme: root

The opposite mistake is to move every job into root's crontab "to be safe". That makes things worse. A small bug or a writable script then runs with full control of the machine. Root should own the scheduler configuration, not run the business logic.

What a dedicated service account looks like

A service account is an account made for one program or job, not for a person. A good one on Linux has these properties:

  • No interactive login. Its shell is /usr/sbin/nologin, and it has no password and no SSH key.
  • One clear purpose. One account per job or per closely related set of jobs, named after the job, such as acme-backup.
  • Minimum access. It can read only what the job reads and write only where the job writes. No sudo, or a single tightly scoped sudo rule if truly needed.
  • Read-only code. Its scripts are owned by root and only readable and executable by the service account.
  • Its own secrets. Credentials come from a protected file or a secrets manager, readable only by that account.
  • A named owner. The account itself is not a person, but a team or person is recorded as accountable for it.
  • Logged and reviewed. Its activity shows up in logs under its own name, and it appears in the regular access review.

A worked example at Acme (illustrative)

The following scenario is fictional. Acme is a placeholder company, and the names describe example roles only. Hostnames, paths and output are illustrative.

During a quarterly access review, Malaya, who leads Cyber Security, notices that the account engineer1 belongs to someone who moved to another department three months ago. The account is still active on the production database server. When Malaya asks why, the answer is: "If we lock it, the nightly backup stops."

Malaya raises it with Rohit, who works in DevSecOps and owns the servers. Soumya, the CEO, approves a short project with one goal: no scheduled job on production may run under a person's account. Rohit follows the steps below.

Step 1: Find every scheduled job and its owner

First, list all user crontabs. On Debian and Ubuntu they live in /var/spool/cron/crontabs/; on Red Hat style systems in /var/spool/cron/.

# Every user's crontab, with the owner shown
for u in $(cut -d: -f1 /etc/passwd); do
  out=$(sudo crontab -l -u "$u" 2>/dev/null | grep -v '^\s*#' | grep -v '^\s*$')
  [ -n "$out" ] && printf '== %s ==\n%s\n' "$u" "$out"
done

# System-wide schedules (these name the user in the sixth field)
sudo cat /etc/crontab
sudo ls -l /etc/cron.d/ /etc/cron.daily/ /etc/cron.hourly/

Rohit finds this (illustrative output):

== engineer1 ==
30 2 * * * /home/engineer1/scripts/db-backup.sh >> /home/engineer1/backup.log 2>&1
== root ==
*/5 * * * * /opt/legacy/check_disk.sh

He records each job in a simple table: what it does, which account runs it, which files it reads and writes, which secrets it uses, and who is responsible for it.

Step 2: Check what the current account can really do

Next, he checks how much power the job currently holds.

id engineer1                       # groups, for example sudo or docker
sudo -l -U engineer1               # sudo rights
sudo ls -la /home/engineer1/.ssh   # keys that allow login
ls -l /home/engineer1/scripts/db-backup.sh

The illustrative result: engineer1 is in the sudo and docker groups, has two SSH keys, and owns the backup script, so anyone using that account can change it. The script reads database credentials from /home/engineer1/.pgpass.

Step 3: Create the dedicated service account

Rohit creates a system account with no login shell and a home folder outside /home.

sudo useradd --system \
  --home-dir /var/lib/acme-backup --create-home \
  --shell /usr/sbin/nologin \
  --comment "Nightly DB backup job, owner: DevSecOps" \
  acme-backup

sudo passwd -l acme-backup     # lock the password; it should never be used

The --system flag gives it a low user ID reserved for services. The comment records what it is for and which team owns it, which helps future reviewers.

Step 4: Move the script and lock it down

The script moves to a shared location, owned by root, so the account that runs it cannot change it.

sudo install -d -o root -g root -m 0755 /opt/acme/bin
sudo install -o root -g acme-backup -m 0750 \
  /home/engineer1/scripts/db-backup.sh /opt/acme/bin/db-backup.sh

# Output folder: only the service account may write here
sudo install -d -o acme-backup -g acme-backup -m 0750 /srv/backups/db

Rohit also reads the script line by line. He removes any reliance on the old user's PATH by using full paths to commands, and he points the output to /srv/backups/db.

Step 5: Give it its own, minimal credentials

Instead of reusing the engineer's database login, Rohit creates a database user that can only do what a backup needs. In PostgreSQL 14 and later, the built-in pg_read_all_data role is a reasonable starting point for a read-only backup user; tighten it further if you can.

The password goes into a file only the service account can read:

sudo install -o acme-backup -g acme-backup -m 0600 /dev/null /var/lib/acme-backup/.pgpass
# Then add one line in the format host:port:database:user:password,
# using REPLACE_WITH_VAULT_REFERENCE instead of a real secret in any shared notes.

If Acme uses a secrets manager, the script should fetch the secret at run time instead, so no password sits on disk at all.

Step 6: Schedule the job under the new account

Rohit puts the schedule in /etc/cron.d/, owned by root, so it is a file that can be tracked in configuration management and reviewed like code. The sixth field names the user.

sudo tee /etc/cron.d/acme-db-backup > /dev/null <<'EOF'
# Nightly database backup. Owner: DevSecOps. Change via pull request only.
SHELL=/bin/sh
PATH=/usr/local/bin:/usr/bin:/bin
30 2 * * * acme-backup /opt/acme/bin/db-backup.sh 2>&1 | /usr/bin/logger -t acme-db-backup
EOF
sudo chmod 0644 /etc/cron.d/acme-db-backup

Sending the output to logger means every run appears in the system log under the tag acme-db-backup, where central logging can collect it.

To stop people from adding ad hoc personal crontabs on production, he also limits who may use crontab at all by creating /etc/cron.allow. When that file exists, only the users listed in it can run crontab; schedules in /etc/cron.d are not affected.

echo root | sudo tee /etc/cron.allow
sudo chmod 0600 /etc/cron.allow

Step 7: Test as the service account, then remove the old job

Before switching over, Rohit runs the script once as the new account:

sudo -u acme-backup /opt/acme/bin/db-backup.sh
ls -l /srv/backups/db/

He also confirms the account cannot log in or escalate:

sudo su - acme-backup      # should print "This account is currently not available."
sudo -l -U acme-backup     # should show no sudo rights

When the test passes, he removes the old personal job and checks the next scheduled run in the logs:

sudo crontab -r -u engineer1
grep acme-db-backup /var/log/syslog        # Debian and Ubuntu
# or: journalctl -t acme-db-backup --since today

Finally, Malaya disables the engineer1 account on production, as the offboarding process intended all along. The backup keeps running.

Step 8: Keep it that way

Two lasting controls stop the problem from coming back:

  • The scheduled-job inventory from Step 1 becomes part of the quarterly access review. Every service account must have a named owning team and a purpose.
  • A small check runs in configuration management and alerts if any account with a login shell has a crontab on a production server.

A note on systemd timers: on modern distributions, a systemd timer is a good alternative to cron. The service unit can set User=acme-backup, NoNewPrivileges=yes, ProtectSystem=strict and ReadWritePaths=/srv/backups/db, which adds sandboxing that cron cannot. The principle stays the same: a dedicated, minimal account.

Which compliance controls apply

No major framework has a clause that literally says "do not run cron as a person". Instead, several controls together expect exactly this outcome: unique, owned, least-privilege accounts, controlled changes and clear logs. Here is how the change maps. Always check the exact wording in the edition your organisation is certified against.

ISO/IEC 27001:2022 (Annex A, controls from ISO/IEC 27002:2022)

  • 5.15 Access control - access rules based on business need.
  • 5.16 Identity management - each identity, including non-human ones, is managed across its life cycle. A job should have its own identity.
  • 5.18 Access rights - granting, reviewing and removing access, including when people change roles or leave. A job tied to a leaver's account blocks this.
  • 5.3 Segregation of duties - separating conflicting tasks and identities.
  • 8.2 Privileged access rights - limiting and controlling elevated rights such as sudo and root.
  • 8.3 Information access restriction - the job reaches only the data it needs.
  • 8.5 Secure authentication - no shared or human passwords used by programs.
  • 8.9 Configuration management - scheduled jobs defined and tracked as configuration.
  • 8.15 Logging and 8.16 Monitoring activities - actions attributable to a specific account.
  • 8.32 Change management - changes to production jobs go through review.

If your certificate is still on ISO/IEC 27001:2013, the closest controls are A.9.2.1 to A.9.2.6 (user registration, access provisioning, privileged access, review and removal of access rights), A.9.4.1 (information access restriction), A.6.1.2 (segregation of duties), A.12.1.2 (change management), A.12.4.1 and A.12.4.3 (event logging, administrator and operator logs) and A.12.5.1 (software on operational systems). Certified organisations have a transition period to move to the 2022 version.

SOC 2 (AICPA Trust Services Criteria, Common Criteria)

  • CC6.1 - logical access security over protected information assets, including identifying and authenticating users and restricting access.
  • CC6.2 - registering, authorising and removing user and system credentials. A job account needs its own approved creation and an owner.
  • CC6.3 - role-based access, least privilege and segregation of duties, with access changed or removed when roles change.
  • CC6.8 - controls to prevent or detect unauthorised or malicious software, which includes protecting scheduled scripts from tampering.
  • CC7.2 - monitoring system components for anomalies, which needs clear account-level logs.
  • CC8.1 - authorising, testing and approving changes to infrastructure and software, including scheduled jobs.

PCI DSS v4.0

  • 7.2.1 and 7.2.2 - access based on job need and least privilege.
  • 7.2.5 - all application and system accounts and related access privileges are assigned and managed based on least privilege. 7.2.5.1 requires periodic review of that access.
  • 8.2.1 and 8.2.2 - unique IDs, and shared or generic accounts used only when strictly necessary and managed.
  • 8.2.6 - inactive user accounts removed or disabled within 90 days.
  • 8.6.1 - if system or application accounts can be used for interactive login, that use is prevented unless needed for an exceptional circumstance, and is then limited, approved and attributable.
  • 8.6.2 and 8.6.3 - passwords for system and application accounts are not hard-coded in scripts or files, and are protected and changed periodically.
  • 10.2 - audit logs capture actions by individual accounts.

Note that 7.2.5, 7.2.5.1 and 8.6.1 to 8.6.3 are future-dated: they are best practice until March 31, 2025, after which they become mandatory. Starting now gives you time.

NIST SP 800-53 Revision 5

  • AC-2 Account Management - defining account types, owners and conditions for use, including system and service accounts. AC-2(3) covers disabling unused accounts.
  • AC-6 Least Privilege, with AC-6(9) logging the use of privileged functions.
  • AC-5 Separation of Duties.
  • IA-2 and IA-4 Identifier Management - unique identifiers for users and processes.
  • IA-5 Authenticator Management - protecting credentials, including those used by programs.
  • AU-2, AU-3 and AU-12 - logging events with enough detail to show which account acted.
  • CM-3 Configuration Change Control and CM-5 Access Restrictions for Change.

CIS Critical Security Controls v8

  • 5.1 Establish and maintain an inventory of accounts, and 5.5 Establish and maintain an inventory of service accounts, each with an owner, purpose and review date.
  • 5.3 Disable dormant accounts.
  • 5.4 Restrict administrator privileges to dedicated administrator accounts.
  • 6.1 and 6.2 - processes to grant and revoke access.
  • 8.2 Collect audit logs.

The CIS Benchmarks for Linux distributions also include specific checks for cron, such as restricting crontab use through /etc/cron.allow and tightening permissions on /etc/crontab and the /etc/cron.* folders.

Conclusion

A cron job under a personal account works fine until the person leaves, their login is stolen, or an auditor asks why a former employee's account is still active. Moving each job to a dedicated, non-interactive service account with only the access it needs fixes all three. It is a small change, often an afternoon per server, and it lines up directly with ISO 27001, SOC 2, PCI DSS, NIST and CIS expectations around identity, least privilege, change control and logging.

Merits

  • Limits the damage if a person's credentials are stolen.
  • Lets you disable leavers' accounts without breaking production jobs.
  • Makes logs clearly show whether a person or a job acted.
  • Brings scheduled jobs under change control and configuration management.
  • Produces clean evidence for SOC 2, ISO 27001 and PCI DSS audits.
  • Keeps secrets out of personal home folders.

Demerits

  • More accounts to create, document and review.
  • Jobs that quietly relied on a person's wide access may fail at first and need careful testing.
  • Narrow permissions take time to work out for complex scripts.
  • Teams lose the convenience of editing schedules on the fly with crontab -e.

Caution

This article is for education only. The Acme scenario, account names, paths and sample output are illustrative, and placeholder values such as acme-backup, /opt/acme/bin and REPLACE_WITH_VAULT_REFERENCE must be replaced with your own. Test every change on a non-production server first and keep a rollback plan. Compliance control numbers and wording differ between editions and can change, so verify each mapping against the official standard and with your auditor before relying on it.

Frequently asked questions

  • Why is it insecure to run cron jobs under a normal user account? — The job inherits that person's full access, keys and group rights, so stealing one login gives control of the job. It also stops you disabling the account when the person leaves.
  • Should cron jobs run as root instead? — No. Root gives the job full control of the server, so any bug or tampered script becomes a full compromise. Use a dedicated account with only the access the job needs.
  • What is a service account in Linux? — It is an account created for a program rather than a person, usually with a low system user ID, no password, no login shell and narrow permissions.
  • How do I find all cron jobs on a Linux server? — List each user's crontab with crontab -l -u <user> and check /etc/crontab, /etc/cron.d/ and the /etc/cron.hourly, daily, weekly and monthly folders.
  • Which ISO 27001 controls cover service accounts? — In ISO/IEC 27001:2022, the main ones are 5.15, 5.16 and 5.18 for access and identity, 8.2 for privileged access, 8.15 for logging and 8.32 for change management.
  • Which SOC 2 criteria apply to cron job accounts? — Mainly CC6.1, CC6.2 and CC6.3 for logical access and least privilege, CC7.2 for monitoring and CC8.1 for change management.
  • Does PCI DSS have rules for system accounts? — Yes. PCI DSS v4.0 requirement 7.2.5 covers least privilege for system and application accounts, and 8.6.1 to 8.6.3 cover their interactive use and passwords.
  • Is a systemd timer better than cron? — It can be, because the service unit can add sandboxing options and runs under a set user. Either way, the job should use a dedicated, least-privilege account.

Tags

#linux #cron #devsecops #leastprivilege #serviceaccount #iso27001 #soc2 #pcidss #compliance #serverhardening

Free field guide

Incident Response: First Hour

A calm, evidence-preserving checklist for establishing control, bounding impact, communicating clearly, and containing an incident safely.