Blog Archive

Sunday, December 6, 2009

View all users in linux

As we all know the user list reside in /etc/passwd, so we can view the registered user by looking up at this file.
But now the fuss is that it also contains many other fields & machine trust accounts & inbuilt accounts.

So now we'll make a command of our own..

We'll start by
1.cat /etc/passwd

2.As we all know that by default all the users created will have their home directories in /home share
so we'll modify our command a bit by using grep.
Now it'll be
cat /etc/passwd | grep "/home"

3. Now we'll get all the user accounts which have their home share in /home.
But the only output we need is the list of users & nothing else.

4. So we'll modify our command again
cat /etc/passwd | grep "/home" |cut -d: -f1
Now what we have done is that we have piped the output of previous command to another variable "cut"
What we have done here is we have added
cut -d: -f1
-d: means delimiter :
-f1 means display first field of line i.e. username.

So final command is
Cat /etc/passwd |grep "/home" |cut -d: -f1

This works until all your users have their home share in /home. If you have defined their home share to some other destination. Modify the above command.


(Hint : In previous case we started grep "/home" , this time we'll use grep "/bin/bash" or whatever valid shell you are using)

Now command will be like..
Cat /etc/passwd |grep "/bin/bash" |cut -d: f1

0 comments:

Post a Comment

Followers