Get Username From UID in Unix

Finding out the username by user id (uid) in Unix is not as common a task as determining the uid by a username, but if you need to do it – I'll show you how.

How to find the username using user id (uid)

Simply use the getent command. Most common use for it is to query the passwd source for a username, like this:

ubuntu$ getent passwd greys
greys:x:1000:113:Gleb Reys,,,:/home/greys:/bin/bash

however, if you query for a user id instead (1000 in this case), getent will work just as good:

ubuntu$ getent passwd 1000
greys:x:1000:113:Gleb Reys,,,:/home/greys:/bin/bash

That's all there is to it! Enjoy!

  • Straps

    To filter only the username
    getent passwd 1000 | sed -e 's/\:.*//'

    returns greys in your example

  • Thanks, I used this. 🙂

  • Suresh

    Thanks a lot. I was searching for this for quite sometime.