- 1How to Add a New User in Linux
- 2Modify Existing User in Linux
- 3Delete a User in Linux — Step-by-Step for Beginners
- 4Create a User Group in Linux - Step-by-Step Tutorial
- 5Linux: Add User to Group
- 6How to Remove a User from a Group in Linux
- 7Delete a User Group in Linux - Beginner Friendly Tutorial
- 8Linux su Command Tutorial – Switch Users Easily
- 9Linux sudo Command - Execute as Another User
- 10Change a User's Password in Linux Using passwd
- 11Set Password Expiry in Linux using chage
- 12Lock a User Account in Linux
- 13How to Unlock a User Account in Linux
- 14Configure User Login Shell in Linux
- 15How to Configure User Environment Variables in Linux
- 16Edit .bashrc and .profile in Linux - User Startup Files Tutorial

- 1Linux Topics Index
- 2How to Create a File in Linux
- 3How to Rename a File in Linux
- 4How to Copy a File in Linux
- 5How to Move a File in Linux
- 6How to Delete a File in Linux
- 7How to Create a Directory in Linux
- 8How to Delete a Directory in Linux
- 9How to Copy Directories Recursively in Linux
- 10Delete Directories Recursively in Linux
- 11How to View Hidden Files in Linux
- 12How to Create a Hidden File in Linux
- 13How to Create a Hidden Directory in Linux
- 14How to Find Files in Linux Using find Command
- 15Find Files in Linux Using locate Command
- 16How to View File Contents Using cat in Linux
- 17How to View File Contents Using less in Linux
- 18View File Contents Using the more Command in Linux
- 19Compare Files Using diff Command in Linux
- 20Compare Files in Linux Using cmp Command
- 21Check File Type in Linux with file Command
- 22Create Symbolic Links with ln -s in Linux
- 23How to Archive Files Using tar Command in Linux
- 24Compress Files with gzip in Linux – Beginner Tutorial
- 25How to Compress Files Using bzip2 in Linux
- 26Compress Files in Linux Using zip Command
- 27Extract Compressed Files using tar in Linux
- 28Extract ZIP Files on Linux with unzip
- 29How to Extract .gz Files using gunzip in Linux

- 1How to Add a New User in Linux
- 2Modify Existing User in Linux
- 3Delete a User in Linux — Step-by-Step for Beginners
- 4Create a User Group in Linux - Step-by-Step Tutorial
- 5Linux: Add User to Group
- 6How to Remove a User from a Group in Linux
- 7Delete a User Group in Linux - Beginner Friendly Tutorial
- 8Linux su Command Tutorial – Switch Users Easily
- 9Linux sudo Command - Execute as Another User
- 10Change a User's Password in Linux Using passwd
- 11Set Password Expiry in Linux using chage
- 12Lock a User Account in Linux
- 13How to Unlock a User Account in Linux
- 14Configure User Login Shell in Linux
- 15How to Configure User Environment Variables in Linux
- 16Edit .bashrc and .profile in Linux - User Startup Files Tutorial
How to Execute Commands as Another User Using sudo
Next Topic ⮕Change a User's Password in Linux Using passwd
How to Execute Commands as Another User Using sudo
Hey there! 👋 If you're learning Linux, you've probably heard of the sudo
command. It's short for "superuser do", and it's a powerful way to run commands with elevated privileges.
But did you know you can use sudo
to run a command as a completely different user — not just the root user?
Let’s explore how to do that in a safe, clear, and beginner-friendly way.
🚀 The Basic Syntax
sudo -u <username> <command>
Here’s what each part means:
-u <username>
: specifies the user you want to switch to<command>
: the command you want to run as that user
🎯 Example: Run a Command as Another User
Let’s say you want to list files in another user’s home directory using their permissions. Here's how you'd do it:
sudo -u john ls /home/john
file1.txt
project/
notes.md
This command tells the system: "Run the ls
command on /home/john
as if you were the user john
."
💡 When Do You Use This?
Some common use cases include:
- Running a script as a specific service account
- Accessing user-specific files or settings
- Debugging permission issues
🔐 What If You Don't Have Permission?
Just like regular sudo
use, your system needs to be configured to allow you to run commands as other users.
If you try something you're not allowed to do, you might see:
Sorry, user yourname is not allowed to execute '/bin/ls /home/john' as john on hostname
This means your system admin hasn’t given you the right permissions in the /etc/sudoers
file. You’d need to ask them for access.
⚙️ Example: Run a Script as Another User
sudo -u deploy bash /home/deploy/deploy.sh
This runs the deploy.sh
script as the user deploy
.
📌 Pro Tip
If you just want a full login shell for another user temporarily, you can use:
sudo -u username -i
This opens a shell session as that user. It's like temporarily logging in as them, without needing their password.
✅ Recap
sudo -u username command
lets you run a command as another user- Great for system management, service accounts, or troubleshooting
- Only works if your account is authorized to use
sudo
for that user
That’s it! 🎉 You now know how to use sudo
to run commands as another user in Linux. Keep practicing, and you’ll become a Linux pro in no time.