Login script that runs only once per user (Linux)

While preparing a Linux server for general use, you might want to initialize certain aspects of the environment at the first login by the user. This article assumes that your shell is bash. When bash starts, it executes commands from different scripts in the order given below.

  1. /etc/profile 1.1 Loads all *.sh file inside /etc/profile.d directory
  2. ~/.bash_profile
  3. ~/.bash_login
  4. ~/.profile

To check which shell you have, type echo $SHELL.

You may modify the files, but it is usually a good idea to have your own custom.sh file created inside /etc/profile.d if you want a certain script to be executed for all users.

The Script

You can use this simple script structure to execute something for the first time a user logs in.


if [ -e $HOME/.yourflag ]
    echo "No steps required"
    echo "Your steps go here"
    touch $HOME/.yourflag

It is a fairly simple structure as you can see and it checks if a particular flag exists.

I hope this helps!

What next?

Well, stay tuned for upcoming articles. You may contact us at contact@attosol.com for your software and consultancy requirements.

© 2023, Attosol Private Ltd. All Rights Reserved.