su: switch user
su - alice starts a login shell as Alice. It asks for Alice's password (in this example it's Sea-Breeze-7). The - gives you her full environment: her home directory, her settings.
su - alice
whoami
pwd
exit
whoamiIn the terminal
student@vulntech:~$ su - alice Password: alice@vulntech:~$ whoami alice alice@vulntech:~$ pwd /home/alice alice@vulntech:~$ exit logout student@vulntech:~$ whoami student
exit (or Ctrl+D) ends her shell and brings you back to yours.
su for root?
su - on its own means become root, with root's password. But root is locked here, as on Ubuntu, so it fails even with the right-looking password:
su -In the terminal
student@vulntech:~$ su - Password: unix_chkpwd[1522]: password check failed for user (root) su: Authentication failure
Use sudo -i for a root shell instead: it asks for your password and leaves a record of who did what.
sudo -u: one command as another user
sudo -u alice command runs a single command as Alice, without knowing her password (you type yours). Admins use it to fix files or test what a user can see:
sudo -u alice whoami
sudo -u alice touch /home/alice/notes.txt
ls -l /home/aliceIn the terminal
student@vulntech:~$ sudo -u alice whoami [sudo] password for student: alice student@vulntech:~$ sudo -u alice touch /home/alice/notes.txt student@vulntech:~$ ls -l /home/alice total 1 -rw-r--r-- 1 alice alice 0 Oct 17 06:38 notes.txt
| Command | Becomes | Password asked |
|---|---|---|
su - alice | Alice, in a new shell | Alice's |
sudo -u alice cmd | Alice, for one command | Yours |
sudo -i | root, in a new shell | Yours |
su - | root, in a new shell | root's (locked on Ubuntu) |
Remember:
su - userswitches user with their password.sudo -u user cmdruns one command as them with yours.exitgoes back.