114

该命令id可用于查找用户的uid,例如:

$ id -u ubuntu
1000

是否有从 a 中查找用户名的命令uid我意识到这可以通过查看/etc/passwd文件来完成,但我询问是否存在对此的现有命令,特别是如果执行它的用户不是 root。

我不是在寻找当前用户的用户名,即我不是在寻找whoamior logname

这也让我想知道在共享网络托管上这是否是一项安全功能,或者我只是没有正确理解某些东西?

为了检查,/etc/passwd来自共享网络主机的文件:

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

这是一个示例目录列表/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

我们可以看到root一些文件的所有者,并且root也显示在 中/etc/passwd,但是其他用户/组都显示为数字。

7 个答案

96

你可能会喜欢这个小曲子。

$ id -nu [number]

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

我可以确认它会在 Arch Linux 上返回相应的用户名(如果存在)。我还可以确认它在以普通用户身份运行时在 Ubuntu 上不起作用,尽管我还没有以超级用户身份对其进行测试。它也不适用于 Alpine Linux。也许安全功能会阻止它在某些系统上运行。

92

尝试

getent passwd "$uid" | cut -d: -f1
  • 1
    如果这不返回任何内容,这是否意味着我无权将 id 转换为用户名? 
    – cwd
     2012 年 4 月 17 日 4:07
  • 2
    更有可能你没有t set “$uid”`或者那个uid不存在。grep ":$uid:" /etc/passwd找到了吗是否getent passwd产生任何输出?  2012 年 4 月 17 日 4:14 
  • @cwd:您应该始终有权将 id 转换为用户名。例如,ls -l总是这样做。 
    – 凸轮
     2012 年 4 月 17 日 7:00
  • 只是好奇,因为查看ls共享主机上的列表会在用户/组名列中显示带有 ls 的数字。也许这是一个安全预防措施或越狱的东西? 
    – cwd
     2012 年 4 月 17 日 19:50
  • @cwd 共享主机更有可能正在使用/etc/passwd当时未安装的其他地方的已安装/共享。 
    – jw013
     2012 年 4 月 17 日 19:56
50

ls已经执行了该查找。您可以使用命令行从命令行执行用户信息查找getent passwd

如果ls显示的是用户 ID 而不是用户名,那是因为没有该名称的用户。文件系统存储用户 ID,而不是用户名。如果您从另一个系统挂载文件系统,或者如果文件属于现在已删除的用户,或者如果您将数字用户 ID 传递给chown,则您可以拥有属于没有名称的用户 ID 的文件。

在共享主机上,您可能有权访问在多个虚拟机之间共享的某些文件,每个虚拟机都有其用户数据库。这有点奇怪(为什么要共享文件而不是拥有它们的用户?),但在技术上是可行的。

8

该命令id可用于查找UID 和/或用户名

  1. 按 USER查找UID,例如:

    $ id -u ubuntu
    1000
  1. 通过 UID查找USER,例如:

    $ id -un 1000
    ubuntu

如果在当前机器上未找到 UID 或 USER,则会将no such user消息打印到stderr,例如:

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

来自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
1

我意识到这是一个老问题,但这是另一个答案

awk -F: '{print $1,$3}' /etc/passwd | grep <UID>
0

解析 /etc/passwd:

% awk -F: "/:$(id -u ubuntu):/{print \$1}" /etc/passwd
ubuntu
0
id | awk '{print $1}' | sed 's/.*(//;s/)$//'

这个 oneliner 执行以下操作:

  • 获取用户信息

  • 提取第一列 ("uid=ID(NAME)")

  • 删除括号之前的任何内容,以及括号本身

你的答案


来自   https://unix.stackexchange.com/questions/36580/how-can-i-look-up-a-username-by-id-in-linux


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



Top Forums UNIX for Dummies Questions & Answers How to get the username if I know only the user's UID?
1  
Old 03-10-2010
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!
2  
Old 03-10-2010
Code:
$ grep 'UIDnumber' /etc/passwd

Is this OK?
3  
Old 03-10-2010
Quote:
Originally Posted by tukuyomi 
Code:
$ 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
Code:
getent passwd

might yield more success.

i.e.
Code:
/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 Smilie)
4  
Old 03-10-2010
Bug To find a particular UID

The uid is normally the third field in /etc/passwd. Thus, to find username for uid=1000
Code:
awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd

5  
Old 03-10-2010
The grep solutions could return multiple answers.
If there is a user with uid 123 and another user with uid 1123.
6  
Old 03-10-2010
joeyg's awk solution is in post #4 works perfectly.
7  
Old 03-12-2010
Yes, the awk solution in post #4 works perfectly!

Thanks a lot!!


9 More Discussions You Might Find Interesting

1. AIX

Hi Everyone, We are encountering the following issue on AIX 5.3. When we do ls -ltr the list displays only user id and group id instead of user name and group name. This is happening for all users except root. Whe we do ls -ltr with root user it shows perfectly fine. When we searched...

2. Shell Programming and Scripting

Hi, 'ps -ef' returns output of the following format UID PID PPID C STIME TTY TIME CMD root 17573 1 0 Sep12 tty6 00:00:00 /sbin/mingetty tty6 hpsmh 18150 14864 0 Sep12 ? 00:00:00 /opt/hp/hpsmh/sbin/hpsmhd -DSSL -f /opt/hp/hpsmh/conf/smhpd.conf root ...

3. UNIX for Advanced & Expert Users

Hi, As the /etc/passwd file contents all system and other users accounts on a linux/unix system, can anyone tell me what is the User ID values range for human user accounts (not system user accounts) on a linux (Red Hat/SuSE etc) and Unix (Solaris) OS. I heard below number 100, all users are...

4. Shell Programming and Scripting

Hi, I want to write a script to check whether an user ID is used in my server and then create that user. If the user ID is not used, I will echo something like "OK, continue" and then continue to execute the script. Else, I will echo something like "Used, exit" and then exit the script. As...

5. Shell Programming and Scripting

Possible to get this? Thanks

6. UNIX for Dummies Questions & Answers

Hi, I know the uid and I wan to know the user name the uid belongs to. How can I get it. Suppose My user name is ssnayak and coresponding uid is 1110 Similarly I know one uid 1212 and how can I come to know the user name for this uid. Thanks & Regards, Siba

7. Shell Programming and Scripting

Hi All, I need to change the UID numbers of many NIS users, is there any command to modify the UID in NIS maps ? ( like usermod) so that their file permissions will be same even with their new UID. If not, how to check all the files owned by particular user in a computer and change the...

8. UNIX for Dummies Questions & Answers

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

9. AIX

what is the upper limit for UID is AIX 5.3 ?