Since 2016 · 50,000+ customers · ★ 5.0 on Google Live chatWhatsAppTelegramOpen a ticket
Get started
Linux

How to Switch User in the Ubuntu Terminal (su, sudo and More)

How to switch user in the Ubuntu terminal guide by 99RDP

On an Ubuntu server you'll often need to act as another user: become root to fix a config, run a command as the www-data web user, or test what a new account can see. This guide shows every common way to switch user from the command line, how to get back, why su root fails on Ubuntu, how to switch users on the desktop, and how to rename a user if what you actually want is to change a username.

Quick answer

su - username          # switch to another user (needs their password)
sudo -i                # become root (needs your own password)
sudo -u username cmd   # run one command as another user
exit                   # go back to your previous user
whoami                 # check who you are now
su - alice switching user in the Ubuntu terminal followed by whoami and pwd
Switching user with su -

Method 1: su (switch user)

su starts a shell as another user. You need that user's password.

su - alice

The - (or -l) gives you a full login shell: alice's home directory, environment variables and PATH. Without the dash, you keep your current directory and most of your environment, which can cause confusing permission errors. Use the dash unless you have a reason not to.

Run a single command as alice without staying in her shell:

su - alice -c "ls -la ~"
sudo -i opening a root shell on Ubuntu and sudo -u running a command as www-data
Becoming root with sudo -i

Method 2: sudo (the Ubuntu way)

On Ubuntu, members of the sudo group can act as other users with their own password. That's usually more convenient and safer than sharing passwords.

Become root

sudo -i        # root login shell (recommended)
sudo su -      # same result, older habit
sudo -s        # root shell, but keeps your environment

Become another user

sudo -iu alice           # login shell as alice
sudo su - alice          # same thing

Run one command as another user

sudo -u www-data php /var/www/app/artisan cache:clear
sudo -u postgres psql

This is the cleanest way to work with service accounts such as www-data or postgres, which have no password and often no login shell.

Why "su root" gives "Authentication failure" on Ubuntu

Ubuntu locks the root account by default; it has no password, so su or su - can't succeed. Use sudo -i instead. If you really need a root password (for example for console access), set one with sudo passwd root, but keep SSH root login with passwords disabled.

Get back to your own user

Type exit or press Ctrl + D. Each su or sudo -i opens a new shell on top of the previous one, so you return to exactly where you were. Check where you are with:

whoami     # current user
id         # user, groups and IDs
who        # who is logged in

Switch user on the Ubuntu desktop

  • GNOME (Ubuntu desktop): click the system menu at the top right → power icon → Switch User… (shown when more than one account exists), or Log Out.
  • XFCE/LightDM (Xubuntu and others): run dm-tool switch-to-greeter to return to the login screen while keeping your session open.
  • Fast switch from the terminal with Ctrl + Alt + F3–F6 to open a text console and log in as another user.

Create a user to switch to

sudo adduser alice           # create user with home directory and password
sudo usermod -aG sudo alice  # give admin (sudo) rights
sudo deluser alice           # remove the user
usermod -l and groupmod -n renaming a user and their home directory on Ubuntu
Renaming a user account

Change a username from the command line

If your goal is to rename an account rather than switch to it, log in as a different admin user (you can't rename the account you're logged in as) and run:

sudo usermod -l newname -d /home/newname -m oldname   # rename and move home
sudo groupmod -n newname oldname                      # rename the user's group
sudo chfn -f "New Full Name" newname                  # optional: display name

Afterwards, check scripts, cron jobs and config files that mention the old name or path.

Troubleshooting

  • "user is not in the sudoers file": the account isn't in the sudo group. Ask an admin to run sudo usermod -aG sudo yourname, then log out and back in.
  • "This account is currently not available": the user's shell is /usr/sbin/nologin (common for service accounts). Use sudo -u user command, or sudo -u user -s /bin/bash for a temporary shell.
  • Wrong home directory or PATH after switching: you forgot the dash. Use su - user or sudo -i.

Frequently asked questions

How do I change user in the Ubuntu terminal?

Run su - username and enter that user's password, or sudo -iu username and enter your own password.

How do I switch to root in Ubuntu?

Run sudo -i. Plain su doesn't work by default because Ubuntu's root account has no password.

What's the difference between su and sudo?

su asks for the target user's password and opens a shell as them. sudo asks for your own password and checks the sudoers rules, and it logs what was run.

How do I change my username in Ubuntu from the command line?

From another admin account, run sudo usermod -l newname -d /home/newname -m oldname and sudo groupmod -n newname oldname.

Nishant Kumar, Sr. Server Engineer at 99RDP
Written byNishant KumarSr. Server Engineer, 99RDP. Part of the team that runs our servers and answers support.
Keep reading

More Linux guides

All Linux guides

Need a server for this?

✓ Setup in minutes✓ Full admin access✓ 7-day refund on new services