Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The command id can be used to look up a user's uid, for example:

$ id -u ubuntu

Is there a command to lookup up a username from a uid? I realize this can be done by looking at the /etc/passwd file but I'm asking if there is an existing command to to this, especially if the user executing it is not root.

I'm not looking for the current user's username, i.e. I am not looking for whoami or logname.

This also made me wonder if on shared web hosting this is a security feature, or am I just not understanding something correctly?

For examination, the /etc/passwd file from a shared web host:

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
avahi:x:70:70:Avahi daemon:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
mysql:x:101:105:MySQL server:/var/lib/mysql:/bin/bash

And here is a sample directory listing of /tmp/

drwx------  3 root     root        1024 Apr 16 02:09 spamd-22217-init/
drwxr-xr-x  2      665      664    1024 Apr  4 00:05 update-cache-44068ab4/
drwxr-xr-x  4      665      664    1024 Apr 17 15:17 update-extraction-44068ab4/
-rw-rw-r--  1      665      664   43801 Apr 17 15:17 variable.zip
-rw-r--r--  1      684      683    4396 Apr 17 07:01 wsdl-13fb96428c0685474db6b425a1d9baec

We can see root is the owner of some files, and root is also showing up in /etc/passwd , however the other users/groups all show up as numbers.

share|improve this question
Keep in mind that more than one user may have the same UID. It's rare, but happens occasionally. – Barry Brown Apr 17 '12 at 2:47
up vote 18 down vote accepted

ls already performs that lookup. You can perform a user information lookup from the command line with getent passwd.

If ls shows a user ID instead of a user name, it's because there's no user by that name. Filesystems store user IDs, not user names. If you mount a filesystem from another system, or if a file belongs to a now-deleted user, or if you passed a numerical user ID to chown, you can have a file that belongs to a user ID that doesn't have a name.

On a shared host, you may have access to some files that are shared between several virtual machines, each with their user database. This is a bit weird (why share files but not the users that own them?), but it's technically possible.

share|improve this answer


getent passwd "$uid" | cut -d: -f1
share|improve this answer
If this returns nothing does that mean I don't have access to translate the id to a username? – cwd Apr 17 '12 at 4:07
More likely you didnt set "$uid"` or that uid doesn't exist. Does grep ":$uid:" /etc/passwd find it? Does getent passwd produce any output? – Mikel Apr 17 '12 at 4:14
@cwd: You should always have access to translate an id to a username. For instance, ls -l is always doing this. – camh Apr 17 '12 at 7:00
Just curious because looking at an ls listing on a shared host was showing numbers in the user / group name columns with ls. Perhaps it is a security precaution or jailshell thing? – cwd Apr 17 '12 at 19:50
@cwd More likely the shared host is using an /etc/passwd mounted / shared from elsewhere which wasn't mounted at the time. – jw013 Apr 17 '12 at 19:56

Parse /etc/passwd:

% awk -F: "/:$(id -u ubuntu):/{print \$1}" /etc/passwd
share|improve this answer
Unlike getent, this does not work if the system uses LDAP. – choroba Apr 17 '12 at 8:51
this doesn't work either if NIS is used or whatever other distributed authentication protocol. – jlliagre Apr 18 '12 at 1:25
@choroba : if he had a specific requirement for LDAP or NIS, he probably would have mentioned it. My solution works on typical linux setups. – laebshade Apr 18 '12 at 2:55

You might enjoy this little ditty.

$ id -nu [number]

3.17.3-1-ARCH #1 SMP PREEMPT Fri Nov 14 22:56:01 CET 2014 i686 GNU/Linux

I can confirm that it returns a corresponding user name, if one exists, on Arch Linux. I can also confirm that it does not work on Ubuntu when run as a normal user, although I have not tested this as the superuser. It also does not work on Alpine Linux. Maybe a security feature prevents this from working on some systems.

share|improve this answer
id -u jimmij => 1000. id -nu 1000 => id: 1000: no such user. – jimmij Nov 21 '14 at 12:39
works for me, with id --version = id (GNU coreutils) 8.23 – eMPee584 Jan 5 at 16:49
Works in FreeBSD 10.3 also. – forquare May 3 at 14:12
id | awk '{print $1}' | sed 's:.*(::;s:)$::'
share|improve this answer
Hello NAIM. While this may be a correct answer it would help the asker (and quite possibly future readers of your solution) if you could edit your answer to explain briefly how this works. – roaima May 3 at 14:32

I realize this is an old question, but here's another answer

awk -F: '{print $1,$3}' /etc/passwd | grep <UID>
share|improve this answer
Pipe to grep what? (Also if you add four spaces at the start of the line, it will render it as fixed width text.) – Wildcard Mar 31 at 21:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.