Run One Command with sudo
Run One Command with sudo
Sometimes you need one operation performed as the school assistant, rather than a whole assistant login session. sudo can run an authorized command as a selected user. Its policy decides whether you are allowed to make that request; typing the word does not itself grant unlimited authority.
Let's inspect the assistant's identity with one command. Along the way, keep track of the shell's role, because the invoking shell handles parts of a command line that are outside the operation sudo launches.
What you’ll learn
- Run an authorized command as another user with sudo.
- Explain why the invoking shell’s redirection is a separate operation.
Command privilege and shell redirection are separate
Invoking shell
Your shell parses the line and handles its redirections.
your shell identityPolicy check
sudo authorizes the requested command and target user.
sudo -u school-assistantRun command
The permitted command runs with the selected identity; this does not elevate every shell operation.
id as assistant
Inspect the assistant’s identity through sudo
Prepare the example
Use the disposable VM with the existing practice account and a sudo policy that permits the selected operation. Authentication may be requested. We are inspecting identity here, not changing a system file, and the target username must resolve before the command can run.
Bash · Linux terminal
sudo -u school-assistant idExpected behavior; machine-dependent details
The command reports the target identity if policy permits.In sudo -u school-assistant id, -u selects the target user and id is the command to run. The result describes that selected identity under the local configuration. Compare this with su, where you might enter a new interactive shell rather than request one operation.
A common surprise occurs with output redirection. In a line such as sudo printf ... > protected-file, the original shell handles > before sudo starts the command. The write destination does not automatically gain the command's privileges. If a task needs a privileged write, choose a documented authorized operation that performs that write deliberately; do not keep adding sudo to unrelated pieces of the line.
Choose a target user and a permitted operation
sudo can run a specific command under another user's identity without permanently switching your entire terminal session to that account.
sudo syntax for running one command as another user
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.
Prefer the smallest necessary sudo scope
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.
Run only the needed command with sudo
sudo -u username commandlets you run a command as another user- Great for system management, service accounts, or troubleshooting
- Only works if your account is authorized to use
sudofor that user
Try it, then check the worked answer
Your task: Does sudo before printf automatically elevate a > redirection?
Use the disposable VM with the existing practice account and a sudo policy that permits the selected operation. Authentication may be requested. We are inspecting identity here, not changing a system file, and the target username must resolve before the command can run.
Bash · complete example for this question
sudo -u school-assistant idExpected behavior; machine-dependent details
The command reports the target identity if policy permits.Worked answer
No; the invoking shell opens the destination.
No, elevating printf does not automatically elevate a redirection handled by the invoking shell. Identify which process opens the destination. This is a process-boundary question, not proof that sudo can never write files.
Check your decision
Privilege belongs to the authorized operation
Use sudo for a specific authorized command and target identity. Read the whole shell line, including pipes and redirections, to understand which operations run where. Inspect the result and avoid treating privilege as a fix for an unexplained path or argument mistake.
Reaching the recap and completing the quizzes are separate milestones.