crontab -l as root and see an entry you never created. Maybe it downloads a script from a remote URL every five minutes. Maybe it runs a binary from /tmp. Either way, nobody on your team put it there. This is one of the most common persistence mechanisms attackers use after gaining root on a Linux server: drop a cron job, and the payload survives reboots, service restarts, even partial cleanup efforts.
This guide walks through every location where scheduled tasks can hide on a typical Linux server, with read-only commands you can run right now to audit what is there.
Where cron entries actually live
- Per-user crontabs stored in the spool directory. On Debian/Ubuntu this is
/var/spool/cron/crontabs/. On RHEL/CentOS/AlmaLinux it is/var/spool/cron/. Each file is named after the user (e.g.,root,www-data). - System crontab at
/etc/crontab. - Drop-in directory
/etc/cron.d/— any file placed here is parsed as a system crontab (with a username field). - Periodic directories:
/etc/cron.hourly/,/etc/cron.daily/,/etc/cron.weekly/,/etc/cron.monthly/. Scripts dropped here run viarun-parts. - anacron spool at
/var/spool/anacron/with its config at/etc/anacrontab. - at queue (one-shot scheduled commands) in
/var/spool/at/or/var/spool/atjobs/.
/etc/cron.d/ or drops a script into /etc/cron.hourly/ often goes unnoticed because admins check crontab -l and stop there.
Audit every crontab spool file
BASH
# List all per-user crontab files (adjust path for your distro)
ls -la /var/spool/cron/crontabs/ 2>/dev/null || ls -la /var/spool/cron/ 2>/dev/null
# Print contents of each file with the username
for f in /var/spool/cron/crontabs/* /var/spool/cron/*; do
[ -f "f" ] && echo "===(basename f) ===" && cat "f" && echo
done- Commands that
curl,wget, orfetchfrom external URLs. - Anything executing from
/tmp,/dev/shm,/var/tmp, or a dot-prefixed hidden directory. - Base64-encoded strings piped to
bashorsh. - Entries running every one to five minutes (high frequency keeps the payload alive even if the process gets killed).
- A crontab file for a user that should not have one (e.g.,
www-data,nobody,mail).
stat command gives you the answer:
BASH
stat /var/spool/cron/crontabs/root 2>/dev/null || stat /var/spool/cron/root 2>/dev/nulltouch, so treat them as supporting evidence, not proof.
Check system-level cron directories
/etc/crontab first — compare it against a known-good copy or a fresh install of your distribution. Most distros ship a minimal /etc/crontab that just calls run-parts on the periodic directories.
Then list everything in /etc/cron.d/:
BASH
ls -la /etc/cron.d/
for f in /etc/cron.d/*; do
[ -f "f" ] && echo "===f ===" && cat "$f" && echo
done
# Check periodic directories for unexpected scripts
ls -la /etc/cron.hourly/ /etc/cron.daily/ /etc/cron.weekly/ /etc/cron.monthly//etc/cron.d/ (logrotate, certbot, sysstat are common). Anything with a generic or random-looking name (update, systemd-runcheck, .cache) deserves scrutiny. Open it, read it, and verify the binary it calls is a real package file. On Debian-based systems, dpkg -S /path/to/binary tells you which package owns a file. On RHEL-based systems, use rpm -qf /path/to/binary. If nothing owns it, that file was placed manually.
For the periodic directories, pay attention to file permissions. A script in /etc/cron.daily/ needs the execute bit set to run via run-parts. If you find a recently added executable script there, check its contents.
Do not forget at jobs and anacron
at command schedules one-shot tasks. Attackers sometimes use it because it is checked even less often than cron. List pending at jobs:
BASH
atqat -c <job_number> to see the full command that will run.
For anacron, check /etc/anacrontab. This file typically has entries for cron.daily, cron.weekly, and cron.monthly. If there are extra lines, investigate.
Systemd timers: the other scheduler
BASH
systemctl list-timers --allBASH
systemctl cat suspicious-timer-name.timer
systemctl cat suspicious-timer-name.serviceExecStart= line tells you what actually runs. Check that binary or script the same way: verify package ownership, read the source, check the file hash.
Also check for user-level systemd timers. These live under ~/.config/systemd/user/ for each user. An attacker with access to a non-root account can set up a user timer that persists across logins:
BASH
find /home -path '*/.config/systemd/user/*.timer' -ls 2>/dev/nullCorrelate with other compromise indicators
- Check the file's creation time and compare it with authentication logs (
/var/log/auth.logor/var/log/secure) to see who was logged in at that time. - Search for the same filename or URL across the filesystem:
grep -r 'suspicious-url.example' /etc/ /var/spool/ /tmp/ /home/. - Look at what the payload does. If it downloads and executes a remote script, the cron entry is just the persistence layer. The real threat is the downloaded payload, which may change every time it runs.
- If the server runs a web stack, check for webshells that may have been the initial entry point. Uploaded webshells often execute commands that install cron-based persistence. For guidance on finding those uploads, see Webshell Uploaded but No Suspicious File in access_log? Search POST Requests to Upload Endpoints.
- On cPanel servers, attackers sometimes combine cron persistence with injected
.htaccessredirects. If you find both, the compromise is deeper than a single entry point. Auditing .htaccess Files for Injected Redirects After a cPanel Server Compromise covers that side of the audit.
What to do when you find something
/etc/cron.d/, /var/spool/cron/, and the systemd unit directories will alert you if new entries appear. Even a simple approach — a nightly script that checksums those directories and compares against a known baseline — catches re-infection early.
The practical takeaway: crontab -l only shows you one user's crontab. A real audit means walking through spool directories, /etc/cron.d/, periodic script directories, at queues, anacron, and systemd timers. After a compromise, check all of them. It takes five minutes and covers the persistence mechanism attackers rely on most.