Linux Run Command As Another User
Last update
2023-11-29
2023-11-29
«runuser, su, sudo»
runuser
Only root
can use it, no password asked, less overhead than others.
1 2 3 4 5 | runuser -u username -- command args # -l = login shell, -P = allocate a pty runuser -l userNameHere -c 'command args' runuser -P -l userNameHere -c '/user/bin/bash -ilc "command args"' |
Note: You can also use #!/usr/bin/env -S bash -il
shebang in a command
script to get the real final user environment.
sudo
Asks user password.
1 2 3 | # -i = login shell, -u = username sudo -iu username # open default shell for `username` sudo -iu username command args |
su
Asks target password.
1 2 3 | # - or --login = login shell su - # open root shell su - username -c "command args" |
Source: nixCraft