1. Overview

In Linux systems, we often need to execute specific commands or scripts before a user logs into the system. This can be necessary for various purposes, such as setting up environment variables, initializing services, or performing system checks.

In this tutorial, we’ll look at different ways to run a command before a user logs in.

2. Using the /etc/profile File

The /etc/profile file acts as a global configuration file executed when all users log in. By adding commands or scripts to this file, we ensure their execution before any user logs in:

$ sudo nano /etc/profile

After opening the file, we can enter our commands:

echo "Executing commands before user login..."
# Our commands here

In this example. we used the sudo nano command to open the /etc/profile file, then in the file, we added the commands we want to execute. Thereafter, we save and exited the file. Upon saving and exiting the file, these commands will execute upon user login.

When we log in, all Bourne-compatible shells look for the /etc/profile file. Any instructions or scripts within this file execute for all users during the login process.

3. Using systemd Service

systemd, a modern init system included in most Linux distributions**, allows the development of custom systemd service units to execute commands or scripts before user login**:

$ sudo nano /etc/systemd/system/before-login.service

After opening the file, we define our service unit:

[Unit]
Description=Commands to run before user login

[Service]
Type=oneshot
ExecStart=/path/to/your/script.sh

[Install]
WantedBy=multi-user.target

In this example, using the sudo command, we opened the /etc/systemd/system/before-login.service file. Then, in the service file, the ExecStart option would contain the path to our commands. We need to replace /path/to/your/script.sh with the path to our script file. Thereafter, we’ll save the file and exit.

Furthermore, systemd service units offer a versatile way to manage services and activities. By building a custom service unit, we ensure the execution of commands or scripts before user login during system startup.

4. Using the /etc/rc.local File

The /etc/rc.local file is executed during system startup, just before entering the default run level. We can use this file to execute commands before a user logs in:

$ sudo nano /etc/rc.local

After opening the file, let’s enter our commands:

echo "Executing commands before user login..."
# Our commands here

In this example, the first command opens up the /etc/rc.local file. Thereafter, we add the commands or scripts to the file just before the exit 0 line. Finally, we save the file and exit. The provided commands will now run before the user logs in during system setup.

The /etc/rc.local file is an established method to run commands or scripts during startup time. Any commands entered into this file will be executed during the system startup process.

5. Using Pluggable Authentication Modules

Pluggable Authentication Modules (PAM) provide a flexible mechanism for authenticating users and running tasks before user login. We can create custom PAM modules to execute commands or scripts:

$ sudo nano /etc/pam.d/before-login

After opening the file, we need to define our PAM module:

session required pam_exec.so /path/to/our/script.sh

In this example, the first command generates a new PAM configuration file. Then, we add the line for our module to the file. Also, we need to ensure that we change /path/to/our/script.sh with the path to our script file.
Finally, we then save the file and exit. The provided commands will now run before the user logs in during system setup.

Pluggable Authentication Modules (PAMs) enable modular authentication and session management. We can use a custom PAM module to execute commands or scripts prior to user login as part of the session management process.

6. Conclusion

Running commands or scripts before a user logs in on Linux systems is necessary for a variety of administrative activities.

In this article, we’ve discussed four main methods to execute commands or scripts on Linux systems before a user signs in. These methods include using global configuration files such as /etc/profile and /etc/rc.local, using contemporary init systems such as systemd, and even using Pluggable Authentication Modules.

Finally, users can manage system setup and configuration more efficiently by understanding and applying these approaches, ensuring that important tasks are completed smoothly before user login. These techniques give flexibility and reliability in pre-login operations on Linux systems, whether they’re for setting up environment variables, initializing services, or running system checks.