How to get the username if I know only the user's UID?

Hi,

I know the user's UID, but I need to know the user's username which has this UID. How can I do it?? Any ideas?

Thanks!

$ grep 'UIDnumber' /etc/passwd

Is this OK?

That's OK if it's a local user (i.e. not an AD user, etc.)

I think

getent passwd

might yield more success.

i.e.

/root # getent passwd 0
root:x:0:0:root:/root:/bin/ksh

(I think it works with non-local users, etc... can't remember, need to check :))

The uid is normally the third field in /etc/passwd. Thus, to find username for uid=1000

awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd

The grep solutions could return multiple answers.
If there is a user with uid 123 and another user with uid 1123.

joeyg's awk solution is in post #4 works perfectly.

Yes, the awk solution in post #4 works perfectly!

Thanks a lot!!

Whomever the currently logged in user is, should also own the console. So many times I do this via log in hook and awk out the ownership of /dev/console to see what user is logged in. Assuming these are AD users that have network homes and may move from computer to computer.