8

I only know the UID of a user, is it possible to switch user using UID in linux distribution like we do using username like su -u someuser?

pevik
  • 288
  • 1
  • 12
Vishrant
  • 243
  • 1
  • 3
  • 9

2 Answers2

10

If you have sudo installed, you can specify a user by UID. The user doesn't even need to exist in /etc/passwd:

# sudo -u \#1234 id
uid=1234 gid=0(root) groups=0(root)
  • Interesting observation: if you have sufficient 'sudo' permissions, this method can be used to act as a user who has been disabled a la https://unix.stackexchange.com/questions/7690/how-do-i-completely-disable-an-account – 0x574F4F54 Jan 05 '23 at 23:49
9

Look up the username with the id command, e.g.:

id -un 1003

Then put it together with command substitution:

su - $(id -un 1003)
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972