Switching Users: su and sudo -u

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
whoami

In 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/alice

In 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
CommandBecomesPassword asked
su - aliceAlice, in a new shellAlice's
sudo -u alice cmdAlice, for one commandYours
sudo -iroot, in a new shellYours
su -root, in a new shellroot's (locked on Ubuntu)

Remember: su - user switches user with their password. sudo -u user cmd runs one command as them with yours. exit goes back.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…