136

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

$ id -u ubuntu
1000

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:

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
pcap:x:77:77::/var/arpwatch:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/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
avahi-autoipd:x:100:104:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
mailman:x:32006:32006::/usr/local/cpanel/3rdparty/mailman/mailman:/usr/local/cpanel/bin/noshell
dovecot:x:97:97:dovecot:/usr/libexec/dovecot:/sbin/nologin
mysql:x:101:105:MySQL server:/var/lib/mysql:/bin/bash
cpaneleximfilter:x:32007:32009::/var/cpanel/userhomes/cpaneleximfilter:/usr/local/cpanel/bin/noshell
nagios:x:102:106:nagios:/var/log/nagios:/bin/sh
ntp:x:38:38::/etc/ntp:/sbin/nologin
myuser:x:1747:1744::/home/myuser:/usr/local/cpanel/bin/jailshell

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.

cwd
  • 45,389

7 Answers7

125

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.

Stefan
  • 1,251
97

Try

getent passwd "$uid" | cut -d: -f1
jw013
  • 51,212
  • 1
    If this returns nothing does that mean I don't have access to translate the id to a username? – cwd Apr 17 '12 at 04:07
  • 2
    More likely you didnt set"$uid"or that uid doesn't exist. Doesgrep ":$uid:" /etc/passwdfind it? Doesgetent passwd` produce any output? – Mikel Apr 17 '12 at 04: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 07: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
  • @jw013 - does that mean I should look for the etc/passwd file or give up? – cwd Apr 17 '12 at 19:57
  • @cwd Are you sure the particular userid you are looking at exists? – jw013 Apr 17 '12 at 19:58
  • @jw013 - i will update the question with more info. – cwd Apr 17 '12 at 19:59
52

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.

12

The command id can be used to both to look up UID and/or USER name

  1. look up a UID by USER, for example:
    $ id -u ubuntu
    1000
  1. look up a USER by UID, for example:
    $ id -un 1000
    ubuntu

If the UID or USER is not found on current machine it prints no such user message to stderr, for example:

$ id -un 1234
id: 1234: no such user

From man id:

id - print real and effective user and group IDs
   -n, --name
          print a name instead of a number, for -ugG
   -u, --user
          print only the effective user ID
   -g, --group
          print only the effective group ID
   -G, --groups
          print all group IDs
ino
  • 317
  • 2
    Note, that the no such user message is printed on stderr. You get an empty string if you capture the name like User=$(id -un 1000 2> /dev/null). – Raoul Steffen May 24 '21 at 16:00
  • @RaoulSteffen good point. I have updated my original post with stderr output. – ino May 25 '21 at 07:59
1

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

awk -F: '{print $1,$3}' /etc/passwd | grep <UID>
don_crissti
  • 82,805
D Smith
  • 21
  • 1
    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 '16 at 21:42
  • I prefer the answer with getent passwd. But if parsing /etc/passwd, it would be nicer to use awk only ID=0 ; awk -F: '{if ( $3 == '"$ID"' ) print $1}' /etc/passwd. – Franklin Piat Dec 29 '16 at 10:47
0
id | awk '{print $1}' | sed 's/.*(//;s/)$//'

This oneliner does the following:

  • get user information
  • extract the first column ("uid=ID(NAME)")
  • remove anything before brackets, and the brackets themselves
Stephen Kitt
  • 434,908
NAIM
  • 11
0

Parse /etc/passwd:

% awk -F: "/:$(id -u ubuntu):/{print \$1}" /etc/passwd
ubuntu
laebshade
  • 2,176