| Tested on | RHEL 10.2 (Coughlan) — vm1.lab.example |
|---|---|
| Package | systemd 257-23.el10_2.2 |
| Applies to | Ubuntu, Debian, Kali Linux, Linux Mint, Pop!_OS, Raspberry Pi OS, elementary OS, Zorin OS, Parrot OS, MX Linux, RHEL, Rocky Linux, AlmaLinux, Oracle Linux, CentOS Stream, Fedora, Arch Linux |
| Privilege | sudo or root |
| Scope | Run a oneshot script before getty-pre.target so console getty starts afterward, verify unit ordering with systemctl show, and read the script log file. Does not cover graphical display managers or delayed timers. |
| Related guides | Run script at boot after network Run script after N minutes of boot Create a systemd service Beginners guide to systemd systemctl command |
A normal boot service may run before, during, or after the console login prompt unless you explicitly order it. systemd provides getty-pre.target as a synchronization point for work that must finish before console getty services start.
The chain you want is:
your script
↓
getty-pre.target
↓
getty service
↓
login promptThis suits short boot checks, banner text, or hardware probes that must complete before anyone can log in locally on the console.
Create the script
This example logs a timestamp; replace the body with your real task:
sudo tee /usr/local/sbin/before-login-task.sh <<'EOF'
#!/bin/bash
LOG=/var/log/before-login-task.log
echo "time=$(date -Is) before_login_task ran" >> "$LOG"
EOFMake it executable:
sudo chmod 755 /usr/local/sbin/before-login-task.shCreate the pre-login service
Create /etc/systemd/system/before-login-task.service:
[Unit]
Description=Run task before console login prompt
Wants=getty-pre.target
Before=getty-pre.target
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/before-login-task.sh
[Install]
WantedBy=multi-user.targetTwo lines make the ordering work:
Wants=getty-pre.targetbrings the pre-getty synchronization point into this boot transaction.Before=getty-pre.targettells systemd that your script must finish before that point is reached.
WantedBy=multi-user.target makes the custom service start during normal multi-user boot. Enabling the unit makes it part of that boot path; the Wants= and Before= lines then place it ahead of the pre-getty synchronization point. Do not use Before=getty.target alone — that does not reliably order you before the actual getty process.
Enable the service
Reload and enable:
sudo systemctl daemon-reloadEnable the unit for future boots:
sudo systemctl enable before-login-task.serviceSample output:
Created symlink '/etc/systemd/system/multi-user.target.wants/before-login-task.service' → '/etc/systemd/system/before-login-task.service'.Reboot and verify
After a reboot, confirm the script ran:
sudo cat /var/log/before-login-task.logSample output:
time=2026-08-21T11:14:44+05:30 before_login_task ranConfirm the unit's ordering properties:
systemctl show before-login-task.service -p Before -p Wants --no-pagerSample output:
Before=getty-pre.target shutdown.target
Wants=getty-pre.targetTroubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Login prompt appears before the script | Used Before=getty.target only |
Switch to Wants= and Before=getty-pre.target |
| Long delay before login | Slow script blocks pre-getty ordering | Shorten the script or move work to after-network boot unit |
References
Summary
To run a script before the Linux login prompt, order a oneshot service before getty-pre.target. Wants= pulls that target into boot; Before= makes your script finish first. Keep the task short because a slow oneshot delays the console login until the service finishes or hits its start timeout.
For network-dependent work or delays measured in minutes, use the after-network or delayed timer guides instead.

