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.
- /etc/profile
1.1 Loads all
*.sh
file inside/etc/profile.d
directory - ~/.bash_profile
- ~/.bash_login
- ~/.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.
#!/bin/bash
if [ -e $HOME/.yourflag ]
then
echo "No steps required"
else
echo "Your steps go here"
touch $HOME/.yourflag
fi
It is a fairly simple structure as you can see and it checks if a particular flag exists.
I hope this helps!
What next?
Stay tuned for upcoming articles. You may contact us for your software and consultancy requirements.