8 October 2026 · One command a day
How do I open a shell as another user on Linux with sudo?
The command sudo -u followed by a user name and bash opens a new shell running as that user. You use it when you are logged in as root, or as an admin account, and need to work as the account that owns a website. Anything you create or start in that shell belongs to that user, not to root.
The command
sudo -u sitename bashsudo runs a command with different privileges. If you are already root, it does not ask for a password. If you are a normal admin user, it asks for your own password. -u sitename tells sudo which user to run as. Replace sitename with the account that owns your site files. bash is the command to run, so you get an interactive shell as that user. Type exit or press Ctrl+D to go back to your original account.
What you will see
root@host:~# sudo -u sitename bash
sitename@host:/root$ whoami
sitename
sitename@host:/root$ cd /var/www/example.com
sitename@host:/var/www/example.com$ touch test.txt
sitename@host:/var/www/example.com$ ls -l test.txt
-rw-rw-r-- 1 sitename sitename 0 Oct 8 09:14 test.txt
sitename@host:/var/www/example.com$ exit
exit
root@host:~#The prompt changes from root@host to sitename@host, and the # becomes a $. Together they tell you that you are no longer root. whoami confirms it. The new file is owned by sitename, which is the whole point. Notice that the shell starts in /root, the directory you were in before. The site user cannot read that directory, so cd somewhere useful before you do anything else.
When to use it
- You need to run
git pullornpm installfor a site, and you do not want root to own the new files. - You want to check whether the site user can actually read a config file or write to an uploads folder. Testing as root proves nothing, because root can read everything.
- The site's Node apps run under that user's own PM2 instance, and you want to manage them as that user. See how to see all your Node apps running under PM2.
Watch out for
This opens a non-login shell. On Ubuntu 24.04, sudo sets HOME to the site user's home directory, but it does not read that user's ~/.profile. Anything set up there can be missing, such as a Node version installed with nvm or extra folders added to PATH. The usual symptom is command not found for node or pm2, even though they work when you log in as that user over SSH. If that happens, use sudo -iu sitename instead. The -i flag starts a full login shell in the user's home directory, the same as a fresh SSH login.
Questions people ask
What is the difference between sudo -u and su?
su sitename asks for the target user's password, while sudo -u sitename asks for yours, or nothing if you are root. Site accounts often have no password at all, so sudo is usually the one that works.
Can I run just one command as the user without opening a shell?
Yes. Put the command in place of bash, for example sudo -u sitename git -C /var/www/example.com pull. It runs once as that user and then returns you to your own prompt.
Switching between accounts like this is only possible on a server where you control the users and permissions yourself. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.