-   Linux - Newbie (
-   -   List all users (

eam 02-19-2004 11:48 PM

List all users
Is there a command to list all users? I tried google but all I found was how to list users who are logged in.

lyle_s 02-20-2004 01:05 AM

This should work:

cut -d: -f1 /etc/passwd


sateesh 05-11-2005 01:46 AM

Bash command to get list of users
I executed the command but that gave this....
chosa06-l:~ # cut -d: -f1 /etc/passwd

But in the list only "root" and "nmc" are users.
How to get the correct user list?
Please help.

Tanc 05-11-2005 04:05 AM

cat /etc/passwd | cut -d":" -f1

Try webmin if you have to manage users.

jschiwal 05-11-2005 05:57 AM

By convention, the uid of regular users is within a certain range. On Mandrake and Red Hat, the normal user range starts at 500, while on SuSE the user range starts at 1000. This allows you to filter out the system users. I don't know what convention that Slackware uses. Also, on a Unix/Linux system with many users, the UIDs are grouped by the function of the user also. This is done to make administrating the system easier. For example, you might have faculty between 1200-1399. You may have administrators in the range 1000-1999.

Getting back to your problem, you need to either also look at the third field of the /etc/passwd file for the uid number or use the "id" command.

You may find it easier to come up with an awk script that returns a list of regular users. Then use the result in a loop to do whatever it is you want to do in the script.

In your case, with just one regular user ( root is a super-user ) this may be mostly a learning exercise.

sateesh 05-12-2005 07:09 AM

Hello Mr.jschiwal,
Thank you. Still I am left with 1 doubt. In the listing from uid=0 to uid=503(regular user), what are bin, daemon,...ftp, ntp, mysql and adminsupport?. Are they also users?.
You told that in SuSE the use4r range starts from 1000. But in my SuSE 9.0 system by default the user range started from 501 onwards (like in Redhat Linux). What is your explanation for this?.
I tried the following according to ur suggestion.
chosa06-l:/usr/src/linux # cut -d: -f3 /etc/passwd >f1
chosa06-l:/usr/src/linux # cut -d: -f1 /etc/passwd >f2
chosa06-l:/usr/src/linux # paste f1 f2
0 root
1 bin
2 daemon
4 lp
8 mail
9 news
10 uucp
12 games
13 man
25 at
30 wwwrun
40 ftp
51 postfix
60 mysql
71 sshd
74 ntp
65534 nobody
500 adminsupport
501 nmc
502 sateesh
503 monu
Hello Mr.Tanc,
What is webmin?. How to invoke that?.
Thanks in advnace.

jschiwal 05-13-2005 09:35 PM

I had thought that I had saved an earlier response yesterday. Anyway, whenever you run a program, the running code has associated with it a UID and an EUID ( effective user id ). Certain services that run in the background are started by root, but very quickly will degrade their permissions to that of a system user setup for that purpose. This if for security reasons. Often the service will run in a chrooted jail, which means that the root is changed to a directory with it's own system file hierarchy, but stripped to the bone. Only what is needed to run the service is present, and a shell option reduces the commands that can be used. Even 'cd' doesn't work. That way if a hacker can cause the service to crash, there is another obsticle to overcome, breaking out of the jail.

I was reading parts of the bash manual on arrays and one example read both the uid and uname from /etc/password. I changed it a little here, I thought it might be useful for you.

user_id= ( $( cut -f 1,3 -d: /etc/passwd ) )

Now the variable user_id contains an array of "uname:uid" strings.

You can handle one of them like this:
echo ${user_id[1]}
You can list the entire array like this:
echo ${user_id[@]}

Suppose you want to know how many elements are in user_id. No problem: echo ${#user_id[@]}
So it would be easy to iterate through each element knowing the number of elements.

for (( index=0; index<numentries; index++ )); do
    if (( ${user_id[index]#*:} >= $NORMALUSERSTART )) ; then echo ${user_id[index]%*:}

This still will list the nobody user, so you may want to extend the test if that isn't acceptable, or decrement $numentries before the loop.

jschiwal 05-16-2005 12:55 AM

You could use:
sed -n '/^UID_MIN/s/GID_MIN[ \t]*\([[:digit:]][[:digit:]]*\).*$/\1/p' /etc/login.defs
sed -n '/^UID_MAX/s/GID_MIN[ \t]*\([[:digit:]][[:digit:]]*\).*$/\1/p' /etc/login.defs
to extract the range of UIDs on any system that uses the shadow suite.

All times are GMT -5. The time now is 12:58 AM.