-1

I am new in linux. I need to know user name from given user id in my c application. Is there any linux api function available to get this?

Thanks

techfun
  • 913
  • 2
  • 13
  • 25
  • try `id [userid]`. for example: `id 0` – Kent Feb 16 '14 at 21:03
  • @Kent: Thanks for reply. But when I give id 1000 on command line it shows no such user. But that 1000 is user id listed when I give only id. Can you please elaborate? – techfun Feb 16 '14 at 21:11
  • I cannot reproduce your problem with my `id (GNU coreutils) 8.22`. I have id `1000` user too. – Kent Feb 16 '14 at 21:13
  • @Kent: what techfun sees seems to be the default in Ubuntu 11,04, 12.04 and 13.04 – tink Feb 17 '14 at 00:28

1 Answers1

-2

If you want to get the username from a c program then getpwuid is probably the easiest way. Man entry here: http://man7.org/linux/man-pages/man3/getpwnam.3.html

From the command line, I would just grep through passwd:

grep 1000 /etc/passwd | cut -f1 -d:
fimad
  • 346
  • 2
  • 7
  • 4
    It's better to use `getent passwd` instead of grepping `/etc/passwd` as it works even if the userid is coming from NIS. See [unix.stackexchange.com](http://unix.stackexchange.com/questions/36580/how-can-i-look-up-a-username-by-id-in-linux) or [serverfault.com](http://serverfault.com/questions/264986/how-can-i-get-user-name-by-user-id). – pevik Oct 01 '14 at 11:22
  • 1
    And grepping just `1000` without any anchors will find people with this in their group id or gcos or password or ... – tripleee Apr 18 '18 at 05:47