How to switch user in terminal

112,392

You should use su:

su [-|-l|--login] USERNAME

If you specify the login option (either -, or -l, or --login: all are synonyms), you will get an environment similar to if you directly logged in as that user. Otherwise you stay in the current environment (e.g. many env variables, the current directory, etc).

Example: (note the usernames and current working directories!)

root@wolf-pack:~# su bytecommander
bytecommander@wolf-pack:/root$ exit
exit

root@wolf-pack:~# su - bytecommander
bytecommander@wolf-pack:~$ exit
logout

root@wolf-pack:~# 

PS: If you run su not as root, you will of course be prompted for the password of the user as who you want to log in.

Share:
112,392

Related videos on Youtube

Karl Morrison
Author by

Karl Morrison

Updated on September 18, 2022

Comments

  • Karl Morrison
    Karl Morrison over 1 year

    I am currently:

    root@sensu-server-client:/# 
    

    I wish to change to:

    sensu@sensu-server-client:/# 
    

    How does one do this. I tried: su sensu but nothing happens.

    • Raphael
      Raphael over 8 years
      Possible duplicate of - askubuntu.com/a/489937/119531
    • Karl Morrison
      Karl Morrison over 8 years
      @Raphael Not possible duplicate. I logged in as root, Exit will not work as I will log out of the server. Think.
    • kos
      kos over 8 years
      su - sensu should work. However try also sudo -iu sensu
    • muru
      muru over 8 years
      What's the output of getent passwd sensu?