Run a Script Before the Linux Login Prompt with systemd

Deepak Prasad
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:

text
your script
getty-pre.target
getty service
login prompt

This 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:

bash
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"
EOF

Make it executable:

bash
sudo chmod 755 /usr/local/sbin/before-login-task.sh

Create the pre-login service

Create /etc/systemd/system/before-login-task.service:

ini
[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.target

Two lines make the ordering work:

  • Wants=getty-pre.target brings the pre-getty synchronization point into this boot transaction.
  • Before=getty-pre.target tells 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:

bash
sudo systemctl daemon-reload

Enable the unit for future boots:

bash
sudo systemctl enable before-login-task.service

Sample output:

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:

bash
sudo cat /var/log/before-login-task.log

Sample output:

output
time=2026-08-21T11:14:44+05:30 before_login_task ran

Confirm the unit's ordering properties:

bash
systemctl show before-login-task.service -p Before -p Wants --no-pager

Sample output:

output
Before=getty-pre.target shutdown.target
Wants=getty-pre.target

Troubleshooting

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.


Frequently Asked Questions

1. Why use getty-pre.target instead of Before=getty.target?

Before=getty.target does not reliably order your unit before the actual getty service. systemd provides getty-pre.target for units that must finish just before console login; getty services are ordered After=getty-pre.target.

2. Will this block the login prompt if my script is slow?

Yes. A slow or hung script delays the console login until the service finishes, fails, or reaches its start timeout. Keep pre-login tasks short; set TimeoutStartSec= explicitly only when you need a different limit.

3. Does this affect SSH logins the same way as the console?

This unit runs during multi-user boot before getty-pre.target. SSH may already be available depending on how sshd is ordered on your system. Test both console and SSH if both matter.
Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware.

  • Debian
  • Ubuntu
  • Linux
  • Red Hat Enterprise Linux
  • Shell Script
  • System Administration