SaferComputing.com

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.

Published
Updated
Last checked
Applies to
Ubuntu Server 24.04 LTS with Ubuntu's cron, at and systemd 255 packages
Technical specification
Target artifactCode 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 behaviourcron 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 stateEvery 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 Linuxsudo ls -la /var/spool/cron/crontabs /etc/cron.d, then sudo crontab -l -u USER for each crontab
Check on Linuxsystemctl list-timers --all
Check on Linuxsystemctl list-unit-files --state=enabled
Check on Linuxsudo atq, then ls -la /etc/rc.local /etc/ld.so.preload
Check on Linuxdpkg -S FILE for each file you cannot explain
Fix on Linuxcopy 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

PlaceRun by, and when
/var/spool/cron/crontabs/USERcron, as that user
/etc/crontab, /etc/cron.d/cron, as the account named on each line
/etc/cron.hourly to /etc/cron.monthlyrun-parts, as root, from /etc/crontab
.timer and .service unitssystemd, on a schedule or at boot
~/.config/systemd/user/The user's own systemd manager
/var/spool/cron/atjobsatd, once, at a set time
/etc/rc.localrc-local.service, as root, at boot
/etc/profile.d/, ~/.bashrc, ~/.profile, ~/.ssh/rcThe shell or sshd, at sign-in
/etc/ld.so.preloadThe 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.

Ubuntu, as a user with sudo
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/null

Cron

Ubuntu, as a user with sudo
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 USER means there is none. cron's manual says not to edit these files directly.
  • /etc/crontab and the files in /etc/cron.d name an account after the five time fields, and that account runs the command. Ubuntu's /etc/crontab runs cron.hourly at 17 minutes past each hour and, unless anacron is installed, cron.daily at 06:25, cron.weekly at 06:47 on Sundays and cron.monthly at 06:52 on the first of the month.
  • run-parts --test prints 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.sh and a file that was not executable were left out.
  • A line starting @reboot runs once, when cron starts at boot; MITRE lists the GoldMax malware using one.

systemd timers and services

Ubuntu, as a user with sudo
systemctl list-timers --all
systemctl list-unit-files --state=enabled
systemd-delta --type=extended,overridden
systemctl cat NAME.service
  • list-timers shows each timer's next and last run and, under ACTIVATES, the service it starts; --all includes inactive timers. MITRE lists Sandworm starting malware at boot with WantedBy=multi-user.target.
  • systemd's manual describes /etc/systemd/system as "System units created by the administrator" and /usr/lib/systemd/system as the package manager's. In a test on Ubuntu 24.04, /etc/systemd/system held 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 .conf file in a NAME.service.d folder, changes a unit without editing it, and can replace a legitimate service's command. systemd-delta lists units extended or overridden, Ubuntu's own drop-ins under /usr/lib included; systemctl cat prints 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 each daemon-reload, and can create units.
Units in a user's home folder
Ubuntu, as root (sudo -i first)
ls -la /root/.config/systemd/user /home/*/.config/systemd/user /home/*/.local/share/systemd/user /etc/systemd/user
ls /var/lib/systemd/linger

These 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

Ubuntu, as a user with sudo
sudo atq
sudo at -c JOB_NUMBER
ls -la /etc/rc.local
  • As root, atq lists every user's jobs: number, time, queue letter and owner. at -c prints the job as a shell script, with the commands at the end. On Ubuntu 24.04, at and atd come from the universe component; if atq is not found, neither is installed.
  • Ubuntu's rc-local.service runs /etc/rc.local start at 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

Ubuntu, as a user with sudo
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/profile runs every readable /etc/profile.d/*.sh and, in interactive bash, /etc/bash.bashrc. MITRE notes a login shell then runs the first of ~/.bash_profile, ~/.bash_login and ~/.profile; Ubuntu's ~/.profile runs ~/.bashrc, where RotaJakiro adds a command when it lacks root.
  • New home folders get copies of the files in /etc/skel, so diff shows what was added since. Users edit these files too: read each added line.
  • sshd runs ~/.ssh/rc, or /etc/ssh/sshrc if that is missing, at each SSH sign-in, before the shell; PermitUserRC no stops the first. See unknown SSH keys for keys with forced commands.
  • The ld.so manual says libraries listed in /etc/ld.so.preload load 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 as ls, so if in doubt, examine the disk from your provider's rescue system.

Package files and added files

Ubuntu, as a user with sudo
dpkg -S /etc/cron.d/certbot
dpkg -S /usr/lib/systemd/system/ssh.service
sudo dpkg --verify
  • dpkg -S names the package that installed a file, as in certbot: /etc/cron.d/certbot; no path found matching pattern means none did. Ask about the /usr/lib path: on Ubuntu 24.04, /lib/systemd/system/ssh.service found nothing in a test, while the /usr/lib path named openssh-server.
  • No package is not proof of an attack: base-files writes /etc/profile from /usr/share/base-files/profile while installing, so dpkg -S finds nothing for it, and installers outside apt add files too.
  • dpkg --verify compares installed files with the checksums dpkg recorded: ??5?????? means changed contents, c a configuration file, perhaps edited on purpose, and missing a 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, and debsums -ce changed 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.

Ubuntu, as a user with sudo
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_NUMBER
  • crontab -e -u USER installs the edited copy when you save; crontab -r -u USER removes the whole crontab. cron rereads changed crontabs every minute, its manual says.
  • disable alone does not stop a running unit; --now does. Then move the files systemctl cat showed to the evidence folder and run sudo systemctl daemon-reload.
  • For the other places, move the file or delete the line.

Sources