3

There is a command id to get the user id. E.g. id -u rootgives me 0. Is there an opposite command to get the user name via the id? I know I can grep /etc/passwdor run getent and awk or cutthe result. My question is if there is a dedicated command or utility I don't know?

Update:

Note that the accepted answer id -un UID requires recent coreutils. E.g. on Ubuntu 14.04 LTS you get:

> id -un 0
id: 0: no such user

1 Answers1

3

id works for me:

$ id -un 0
root

From man id:

-n, --name
      print a name instead of a number, for -ugG
muru
  • 69,900
  • 13
  • 192
  • 292
  • This does not work for me. I'm on Ubuntu 14.04.3 LTS and if I enter `id -un 0` I get `id: 0: no such user` – Veterinarian Jan 24 '16 at 02:40
  • @Veterinarian Consider upgrading `coreutils`, then. – muru Jan 24 '16 at 02:43
  • I've tried it on debian jessie and Mac and it works. So either an ubuntu bug or a bug/missing feature in the older version. What a pitty ... so it cannot be used reliably in scripts. I'll accept your answer anyways. – Veterinarian Jan 24 '16 at 03:05
  • @Veterinarian It was added in coreutils 8.22. 14.04 has 8.21. – muru Jan 24 '16 at 09:35