-1

Possible Duplicate:
How to list all users and groups on linux?

How to determine all the users on a Linux machine (not only the ones who are logged in)?

I searched and found the following command:

$ cat /etc/passwd | grep "/home" | cut -d: -f1

But my concern is that I could not find root in the output of above command. Also when I try who command I get the same response when I am logged in a root and as a normal user. Please help!

1
  • You've specified additional information for your question in the comments. Please update your question to include these points. Sep 11, 2011 at 19:18

3 Answers 3

4

your command wont find root because root's home is /root not /home/[user] also, it wont show remote network accounts.

a better command is getent passwd | cut -d ":" -f1.

5
  • But what about the who command? When I am logged into as sam user then it shows output sam. When I am logged into as root then also it shows sam.
    – sumit
    Sep 11, 2011 at 18:28
  • refer to "man who" for more info. Shows logged in users, not all users. re your query, did you log in as sam and then run su/sudo, or did you go directly as root from login prompt. Also if you are logged in as sam else where (like anouther window) itll show up
    – Sirex
    Sep 11, 2011 at 18:33
  • getent is the right way to check the user database. Sep 11, 2011 at 18:40
  • Sirex: I want to login as a account which is having /bin/false shell. How can I do it? Is there any way to do it?
    – sumit
    Sep 11, 2011 at 18:45
  • this is nearly always a bad idea. However you can do it by changing the shell to be /bin/bash and setting a password on the account. In all seriousness though, the need to do this is a good indication of a case of "you're doing it wrong"
    – Sirex
    Sep 11, 2011 at 20:42
2

Take the search (grep) out and cut at the first semi-colon: cat /etc/passwd | cut -d: -f1

6
  • It lists many many things like: operator, games, gopher, ftp, dbus, vcsa. All of them are users?
    – sumit
    Sep 11, 2011 at 18:14
  • Yes, they are users. Many are created by the OS/applications on install. Have a look at cat /etc/passwd
    – xofer
    Sep 11, 2011 at 18:18
  • yes, but unless they have a shell in them like /bin/bash they cant log in. typically the shell is set to /bin/false for these "service accounts".
    – Sirex
    Sep 11, 2011 at 18:28
  • ok, to exclude /bin/false, use cat /etc/passwd | grep -v "/bin/false" | cut -d: -f1 To only include /bin/bash use cat /etc/passwd | grep "/bin/bash" | cut -d: -f1
    – xofer
    Sep 11, 2011 at 18:36
  • 1
    Login as root and su - <user> -s /bin/bash.
    – quanta
    Sep 11, 2011 at 18:41
2

Because $HOME folder for root is /root, not in /home.

To list all the 'real' users have shell (assumming is /bin/bash), you can use:

grep "/bin/bash" /etc/passwd | cut -d: -f1
4
  • Ohh..ok. But what about the who command? When I am logged into as sam user then it shows sam. When I am logged into as root then also it shows sam.
    – sumit
    Sep 11, 2011 at 18:15
  • Please give us the output of who and egrep 'root|sam' /etc/passwd?
    – quanta
    Sep 11, 2011 at 18:23
  • Output of egrep: root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
    – sumit
    Sep 11, 2011 at 18:24
  • Output of 'who': sam pts/0 Sep 11 23:01 <ip address>
    – sumit
    Sep 11, 2011 at 18:25

Not the answer you're looking for? Browse other questions tagged .