You run 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
Cron is not a single file. Entries can exist in at least six different places, and attackers know most admins only check one or two. Here is the full list:
- 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/.
An attacker who plants entries in /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
Start by listing every per-user crontab on the system. This single loop covers all users:
# 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
Look for entries you do not recognize. Red flags:
- 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).
Also check modification timestamps. If the root crontab was last modified at a time nobody was working, that is worth investigating. The stat command gives you the answer:
stat /var/spool/cron/crontabs/root 2>/dev/null || stat /var/spool/cron/root 2>/dev/null
Keep in mind: timestamps can be faked with touch, so treat them as supporting evidence, not proof.
Check system-level cron directories
Next, inspect the system crontab and drop-in directories. These are writable only by root under normal circumstances, but if root was compromised, anything goes.
Look at /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/:
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/
Legitimate packages install files in /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
The at command schedules one-shot tasks. Attackers sometimes use it because it is checked even less often than cron. List pending at jobs:
atq
If you see jobs you did not schedule, inspect them with at -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
On systems running systemd, timers are a parallel scheduling mechanism that does not touch cron at all. List all active timers:
systemctl list-timers --all
This shows every timer, its next and last trigger time, and the service unit it activates. Look for timers you do not recognize. Legitimate timers typically belong to installed packages (logrotate, fstrim, apt updates, certbot).
If a timer looks suspicious, inspect its unit files:
systemctl cat suspicious-timer-name.timer
systemctl cat suspicious-timer-name.service
The service unit's ExecStart= 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:
find /home -path '*/.config/systemd/user/*.timer' -ls 2>/dev/null
Correlate with other compromise indicators
Finding a suspicious cron entry is not the end of the investigation — it is the beginning. Once you identify the payload (the script or binary the cron job runs), trace it further:
- 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
Before removing anything, preserve evidence. Copy the cron entry, the payload script or binary, and relevant log sections to a separate location (ideally off the compromised server). If your organization has an incident response process, follow it.
Removing the cron entry alone is not sufficient. If the attacker still has access through another vector (an SSH key, a webshell, a compromised user account), they will just recreate it. The cron job is a symptom. The root cause is how they got root in the first place.
After containment, set up monitoring. A file integrity monitoring tool watching /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.