krishna murti
krishna murti

Reputation: 1061

Shell script to logout a user from unix

I need some guidance regarding this question. I want to write a shell script which will immediately log out the user (except root user) as soon as the user logs in to the system (should work on any unix variant). I am wondering whether this is possible or not?

My approach would be something like this condition:

If [[ $(id) != root]]  then 
    logout 
else 
    login

But i am not sure how to script this and in which initialization file to put the script (maybe .profile?).

Upvotes: 0

Views: 2428

Answers (3)

clt60
clt60

Reputation: 63942

Answer to your question:

for bash

(( $(id -u) > 0 )) && echo 'do something if NOT ROOT (eg. exit)'

or for sh

[ `whoami` == "root" ] || echo 'do something if NOT ROOT (eg. exit)'

Upvotes: 0

Thomas Junk
Thomas Junk

Reputation: 5676

Why kicking out the user at all? I do not see the advantage of letting a user in and kick him afterwards out

1) Delete the user's account.

2) as rekire said: disallow login

3) use a random password passwd -l sets the password to a random one (is effectively the same as 2)

Upvotes: 0

rekire
rekire

Reputation: 47955

Simply set to all users the shell /bin/false

That closes every shell after a successful login. To set that shell you can use this command:

usermod -s /bin/false someuser

Upvotes: 4

Related Questions