Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What I need is something like:

$ who-has-uid 1000
cyrus

I know the file /etc/passwd contains such informations, I'm not asking a script that parses it.

share|improve this question
2  
Why not? Sounds like the easiest solution if you have read access to that file... –  Yitzchak Nov 30 '11 at 22:25
    
Because I can't believe there isn't such built-in tool in Debian (e.g. id -u cyrus just do the opposite thing). –  cYrus Nov 30 '11 at 22:28
    
Simple enough to do in a one line bash function: who-has-uid() { grep $1 /etc/passwd| awk -F: '{print $1;}' } –  Doug Harris Nov 30 '11 at 22:38
    
who-has-uid 100 will match also 100, 1001, ... I would use getent passwd 100 | cut -f1 -d: instead. –  cYrus Nov 30 '11 at 22:45

4 Answers 4

up vote 2 down vote accepted

If you have root access, it's as simple as:

sudo -u \#${pid} whoami
share|improve this answer
    
Finally! Thanks, I meant something like that. –  cYrus Dec 1 '11 at 17:33

You could try getent (getent man page):

$ getent passwd 1005
shufler:x:1005:119:shufler,,,:/home/local/shufler:/bin/bash

You could parse out just the username with sed.

share|improve this answer
$ who-has-uid() { perl -e 'print +(getpwuid('$1'))[0], "\n"'; }
$ who-has-uid 0
root
$ who-has-uid 1
daemon

Note that this will work (assuming Perl is configured properly) even if the information comes from somewhere other than the /etc/passwd file.

There's no real error checking; who-has-uid 999 prints an empty line if there's no such UID on the system.

If you don't insist on a one-liner, you can put this somewhere in your $PATH:

#!/usr/bin/perl

use strict;
use warnings;

my $ok = 1;
foreach my $uid (@ARGV) {
    my @pw = getpwuid $uid;
    if (@pw) {
        print "$pw[0]\n";
    }
    else {
        warn "$uid: No such user\n";
        $ok = 0;
    }
}

exit 1 if not $ok;
share|improve this answer
    
Thanks but way too complicated outside a Perl context. –  cYrus Dec 1 '11 at 17:32
    
@cYrus: Perhaps, but you only have to write it (or copy-and-paste it from here) once. –  Keith Thompson Dec 1 '11 at 18:02
1  
Sure, I understand your point. With "complicated" I mean: unnecessarily complicated. The accepted answer is an example. –  cYrus Dec 1 '11 at 19:08
1  
@cYrus: I would say that a simple getpwuid() or getent, which is available to all users, is much less complicated than executing a program as the target user via a privileged tool. –  grawity Dec 1 '11 at 21:45
    
@grawity: much less complicated is quite subjective. –  cYrus Dec 1 '11 at 22:29

Use awk, (usually installed GNU awk, gawk) Tell it to use colon as a delimiter, see if field 3 (the UID) is 1000, then print the first field (username)

gawk -F: '$3 == 1000{print $1}' < /etc/passwd

This is if the info is in /etc/passwd, if you have network login info (e.g. on NIS or LDAP) getent works better

getent passwd | gawk -F: '$3 == 1000{print $1}'

You do need single quotes: if you use double quotes, bash will try to figure out what $1 means to the shell, and not pass it to gawk.

share|improve this answer
    
This works, but is awfully inefficient with centralized user directories: with 500 users, it'd have to make 499 unnecessary lookups. And that's even assuming the directory supports/allows enumeration, which not all do. –  grawity Dec 1 '11 at 13:29

Your Answer

 
discard

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.