Replacing /bin/bash with /bin/false in /etc/passwd file

5

1

I am writing a program to disable users from a system, i want to replace /bin/bash to /bin/false.

Example

xxx:x:1:22:xx:/export/home/xx:**/bin/bash**

replace to

xxx:x:1:22:xx:/export/home/xx:**/bin/false**

I want do with using bash script.

I know one way to do this is using sed. But i am not good at regular expressions.

Can any one help?

user1212207

Posted 2013-02-27T17:02:36.587

Reputation:

mv /bin/bash /tmp/bin/false – Stepo – 2013-02-27T17:07:38.820

5@Stepo That just makes /bin/bash unavailable to everyone. – chepner – 2013-02-27T17:08:55.953

ah gosh, I see. you're right! – Stepo – 2013-02-27T17:10:29.090

Answers

8

Well you don't do it at all with sed or regular expressions. What you do is to use the program chsh to change the shell of a user.

chsh -s /bin/false username

alternatively:

usermod -s /bin/false username

If you wanted to replace it with an actual shell you'd also have to make sure that it is listed in /etc/shells.

0xC0000022L

Posted 2013-02-27T17:02:36.587

Reputation: 5 091

chsh says command not found!! i am using solaris – None – 2013-02-27T17:13:09.497

@user1212207: on my SunOS 5.8 SUNW,UltraAX-i2 ther is a usermod. And it even supports -s. I stand by my answer. – 0xC0000022L – 2013-02-27T17:24:45.280

yeah usermod works fine!!! and solved my problem – None – 2013-02-27T17:36:32.703

2

Please be aware that chsh is not always available! in alpine Linux for example or embeded systems using busybox ..

Simple inline sed command :

sed -i '/www-data/s/false/sh/g' /etc/passwd

(This example changes shell from /bin/false to /bin/sh for user www-data )

Pooya

Posted 2013-02-27T17:02:36.587

Reputation: 131