Reference · Linux servers
Linux persistence: check cron, systemd timers and services, at jobs and start-up files
Find code set to run on a schedule or at start-up on Ubuntu: crontabs, systemctl list-timers --all, enabled units and drop-ins, atq, /etc/rc.local, shell start-up files and /etc/ld.so.preload, then check them with dpkg -S and dpkg --verify.
| Target artifact | Code set to run on a schedule or at start-up: crontabs in /var/spool/cron/crontabs, /etc/crontab, /etc/cron.d and the cron.* folders, systemd timers and services, at jobs, /etc/rc.local, shell start-up files and /etc/ld.so.preload. |
|---|---|
| Default behaviour | cron runs every crontab it finds and systemd every enabled unit; nothing in either shows whether a package or a person put it there. Installing cron adds no user crontabs; /etc/rc.local runs at boot only if it exists and is executable. |
| Safe state | Every crontab line, timer, enabled unit, at job and start-up line is one you can explain; dpkg -S names a package for each system file, or you know who added it; /etc/ld.so.preload does not exist. |
| Check on Linux | sudo ls -la /var/spool/cron/crontabs /etc/cron.d, then sudo crontab -l -u USER for each crontab |
| Check on Linux | systemctl list-timers --all |
| Check on Linux | systemctl list-unit-files --state=enabled |
| Check on Linux | sudo atq, then ls -la /etc/rc.local /etc/ld.so.preload |
| Check on Linux | dpkg -S FILE for each file you cannot explain |
| Fix on Linux | copy the job to an evidence folder, then remove it: sudo crontab -e -u USER, sudo systemctl disable --now NAME.timer NAME.service or sudo atrm JOB |
Where code waits to run
| Place | Run by, and when |
|---|---|
/var/spool/cron/crontabs/USER | cron, as that user |
/etc/crontab, /etc/cron.d/ | cron, as the account named on each line |
/etc/cron.hourly to /etc/cron.monthly | run-parts, as root, from /etc/crontab |
.timer and .service units | systemd, on a schedule or at boot |
~/.config/systemd/user/ | The user's own systemd manager |
/var/spool/cron/atjobs | atd, once, at a set time |
/etc/rc.local | rc-local.service, as root, at boot |
/etc/profile.d/, ~/.bashrc, ~/.profile, ~/.ssh/rc | The shell or sshd, at sign-in |
/etc/ld.so.preload | The dynamic linker, in every program it starts |
MITRE ATT&CK lists each as an attacker technique: Cron (T1053.003), Systemd Timers (T1053.006), Systemd Service (T1543.002), At (T1053.002), RC Scripts (T1037.004), Unix Shell Configuration Modification (T1546.004) and Dynamic Linker Hijacking (T1574.006).
Start with what changed recently. touch can set a file's modification time back, not its change time, which find -ctime reads: in a test, a file set back to 2020 was still found by -ctime -1. MITRE records an intrusion, campaign C0064 (December 2025 to February 2026), that reset a crontab script's modification time after using it to add an SSH key for root.
sudo find /etc/cron* /var/spool/cron /etc/systemd /etc/profile.d /etc/rc.local /etc/ld.so.preload /root/.config/systemd /home/*/.config/systemd -ctime -30 -ls 2>/dev/nullCron
sudo ls -la /var/spool/cron/crontabs
sudo crontab -l -u USER
cat /etc/crontab
ls -la /etc/cron.d /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly
run-parts --test /etc/cron.daily- Each file in the spool folder is named after an account;
no crontab for USERmeans there is none. cron's manual says not to edit these files directly. /etc/crontaband the files in/etc/cron.dname an account after the five time fields, and that account runs the command. Ubuntu's/etc/crontabrunscron.hourlyat 17 minutes past each hour and, unless anacron is installed,cron.dailyat 06:25,cron.weeklyat 06:47 on Sundays andcron.monthlyat 06:52 on the first of the month.run-parts --testprints the scripts that would run. cron's manual says they must be executable and owned by root, and that names containing a dot are ignored; the dot rule also applies in/etc/cron.d. In a test,update.shand a file that was not executable were left out.- A line starting
@rebootruns once, when cron starts at boot; MITRE lists the GoldMax malware using one.
systemd timers and services
systemctl list-timers --all
systemctl list-unit-files --state=enabled
systemd-delta --type=extended,overridden
systemctl cat NAME.servicelist-timersshows each timer's next and last run and, under ACTIVATES, the service it starts;--allincludes inactive timers. MITRE lists Sandworm starting malware at boot withWantedBy=multi-user.target.- systemd's manual describes
/etc/systemd/systemas "System units created by the administrator" and/usr/lib/systemd/systemas the package manager's. In a test on Ubuntu 24.04,/etc/systemd/systemheld only the symbolic links that enabling a unit creates; a regular file there is a unit or drop-in that someone wrote, or that a tool such as snapd created for a snap's services. - A drop-in, a
.conffile in aNAME.service.dfolder, changes a unit without editing it, and can replace a legitimate service's command.systemd-deltalists units extended or overridden, Ubuntu's own drop-ins under/usr/libincluded;systemctl catprints a unit and its drop-ins with their paths. - Generators in
/etc/systemd/system-generators/and/usr/local/lib/systemd/system-generators/run very early at boot and at eachdaemon-reload, and can create units.
Units in a user's home folder
ls -la /root/.config/systemd/user /home/*/.config/systemd/user /home/*/.local/share/systemd/user /etc/systemd/user
ls /var/lib/systemd/lingerThese units run under the user's own systemd manager, without root. loginctl's manual says that with lingering on, set by loginctl enable-linger, that manager starts at boot and keeps running after sign-out; systemd 255 keeps one file per lingering user in /var/lib/systemd/linger. MITRE lists the Mini Shai-Hulud malware writing ~/.config/systemd/user/gh-token-monitor.service.
at jobs and /etc/rc.local
sudo atq
sudo at -c JOB_NUMBER
ls -la /etc/rc.local- As root,
atqlists every user's jobs: number, time, queue letter and owner.at -cprints the job as a shell script, with the commands at the end. On Ubuntu 24.04,atandatdcome from the universe component; ifatqis not found, neither is installed. - Ubuntu's
rc-local.serviceruns/etc/rc.local startat boot, as root, when the file exists and is executable. MITRE lists HiddenWasp adding itself to/etc/rc.local.
Shell start-up files and ld.so.preload
ls -la /etc/profile.d
diff /etc/profile /usr/share/base-files/profile
sudo diff /home/USER/.bashrc /etc/skel/.bashrc
cat /etc/ld.so.preload- Ubuntu's
/etc/profileruns every readable/etc/profile.d/*.shand, in interactive bash,/etc/bash.bashrc. MITRE notes a login shell then runs the first of~/.bash_profile,~/.bash_loginand~/.profile; Ubuntu's~/.profileruns~/.bashrc, where RotaJakiro adds a command when it lacks root. - New home folders get copies of the files in
/etc/skel, sodiffshows what was added since. Users edit these files too: read each added line. - sshd runs
~/.ssh/rc, or/etc/ssh/sshrcif that is missing, at each SSH sign-in, before the shell;PermitUserRC nostops the first. See unknown SSH keys for keys with forced commands. - The
ld.somanual says libraries listed in/etc/ld.so.preloadload into every program, which is "usually undesirable" and "typically employed only as an emergency remedy". MITRE lists Rocke editing the file to hook C library functions and hide its dropper; such hooks can hide files from tools such asls, so if in doubt, examine the disk from your provider's rescue system.
Package files and added files
dpkg -S /etc/cron.d/certbot
dpkg -S /usr/lib/systemd/system/ssh.service
sudo dpkg --verifydpkg -Snames the package that installed a file, as incertbot: /etc/cron.d/certbot;no path found matching patternmeans none did. Ask about the/usr/libpath: on Ubuntu 24.04,/lib/systemd/system/ssh.servicefound nothing in a test, while the/usr/libpath namedopenssh-server.- No package is not proof of an attack: base-files writes
/etc/profilefrom/usr/share/base-files/profilewhile installing, sodpkg -Sfinds nothing for it, and installers outside apt add files too. dpkg --verifycompares installed files with the checksums dpkg recorded:??5??????means changed contents,ca configuration file, perhaps edited on purpose, andmissinga deleted file. dpkg's manual says the check "should not be considered as any kind of security verification": root can change the records too.debsums -c, from universe, lists changed files other than configuration files, anddebsums -cechanged configuration files.
Remove what you find
Warning Keep evidence, then look for the way in
Copy each job and note its times with stat before you remove it. An added job means someone could write files as that account, or as root, and removing it does not remove their access: check the server's SSH keys too.
sudo install -d -m 700 /root/persistence-evidence
sudo cp -a /var/spool/cron/crontabs/USER /root/persistence-evidence/
sudo crontab -e -u USER
sudo systemctl disable --now NAME.timer NAME.service
sudo at -c JOB_NUMBER | sudo tee /root/persistence-evidence/at-JOB_NUMBER.txt > /dev/null
sudo atrm JOB_NUMBERcrontab -e -u USERinstalls the edited copy when you save;crontab -r -u USERremoves the whole crontab. cron rereads changed crontabs every minute, its manual says.disablealone does not stop a running unit;--nowdoes. Then move the filessystemctl catshowed to the evidence folder and runsudo systemctl daemon-reload.- For the other places, move the file or delete the line.
Related guides and lookups
Sources
- MITRE ATT&CK: T1053.003, Cron
- MITRE ATT&CK: T1053.006, Systemd Timers
- MITRE ATT&CK: T1543.002, Systemd Service
- MITRE ATT&CK: T1053.002, At
- MITRE ATT&CK: T1037.004, RC Scripts
- MITRE ATT&CK: T1546.004, Unix Shell Configuration Modification
- MITRE ATT&CK: T1574.006, Dynamic Linker Hijacking
- MITRE ATT&CK: campaign C0064
- Ubuntu manual: cron, 24.04
- Ubuntu manual: crontab, file format, 24.04
- Ubuntu manual: at, 24.04
- Ubuntu manual: systemctl, 24.04
- Ubuntu manual: systemd.unit, 24.04
- Snapcraft forum: snap service units stored in /etc/systemd/system
- Ubuntu manual: systemd.generator, 24.04
- Ubuntu manual: loginctl, 24.04
- Ubuntu manual: sshd, 24.04
- Ubuntu manual: ld.so, 24.04
- Ubuntu manual: dpkg, 24.04