How to single out root user from normal user

squirrel96

New Member
I have to write a script in which I have to enforce different policies for different users. I am new to scripting how would I differentiate root user and normal user in script. This script should enforce policies differently for root user and normal user. Should I use regular expression to directly match the root keyword or is there any other way
 


JasKinasis

Well-Known Member
If you mean that you want to be able to tell whether your script is being ran as root, or a normal user - you can simply check the $EUID variable (the effective user ID).
An EUID of 0 means that the script is running as root, otherwise, it's the user number for the user running the script.

e.g.
Bash:
if [ "$EUID" -ne 0 ]; then
    echo "running as user"
else
    echo "Running as root"
fi
That way your script can differentiate between whether it is running as root, or as a normal user and you can put whatever actions need to be performed in place of the echo commands that are in my snippet.

If that's NOT what you're trying to do - then please explain in a little more detail what you are trying to do - and what you mean by enforcing policies.
 

squirrel96

New Member
Actually I have to apply password policy on all users except root user. I will run the script as root only it will apply a particular password policy on all ordinary system users but leave the root users aside. I don't know much about scripting and programming but to explain myself script would execute like following.

for [ user =! root]
do [ change password age
block usb access
set a complicated pass ]

I am trying to getent from /etc/passwd file but the problem is, it will throw all users including users created for applications like apache. My purpose is to just get the ordinary systems users.
 
Last edited:

JasKinasis

Well-Known Member
In that case - perhaps get a list of actual system users by listing the directories in /home/ and then find the entries for each of those users in the passwd file and change their settings/options?
 



Members online


Latest posts


Top