📖 Scenario: You are managing files on a Linux system. Some files need special permissions to be accessed or modified. You will learn how to use sudo to run commands with elevated privileges safely.
🎯 Goal: Learn to use sudo to list a protected directory, create a file in it, and then display the file's contents.
📋 What You'll Learn
Use the sudo command to run commands with elevated privileges
List the contents of a protected directory /root
Create a file called secret.txt inside /root using sudo
Write text into the file using sudo
Display the contents of the file using sudo
💡 Why This Matters
🌍 Real World
System administrators often need to run commands with elevated privileges to manage system files and settings securely.
💼 Career
Knowing how to use <code>sudo</code> is essential for Linux system administrators, DevOps engineers, and anyone managing servers or secure environments.
Progress0 / 4 steps
1
List the contents of the /root directory using sudo
Type the command sudo ls /root to list the contents of the /root directory, which requires elevated privileges.
Linux CLI
Hint
The sudo command lets you run commands as the superuser. Use it before ls /root to see inside the protected directory.
2
Create a file called secret.txt inside /root using sudo
Type the command sudo touch /root/secret.txt to create an empty file named secret.txt inside the /root directory.
Linux CLI
Hint
Use sudo touch /root/secret.txt to create the file with superuser rights.
3
Write the text Top Secret Data into /root/secret.txt using sudo
Type the command echo 'Top Secret Data' | sudo tee /root/secret.txt to write the text Top Secret Data into the file /root/secret.txt with elevated privileges.
Linux CLI
Hint
Use echo to send text and sudo tee to write it into the file with superuser rights.
4
Display the contents of /root/secret.txt using sudo
Type the command sudo cat /root/secret.txt to display the contents of the file /root/secret.txt with elevated privileges.
Linux CLI
Hint
Use sudo cat /root/secret.txt to read the file contents with superuser rights.
Practice
(1/5)
1. What is the main purpose of using sudo in Linux?
easy
A. To create a new user account
B. To switch to another user account permanently
C. To list files in a directory
D. To run commands with higher privileges than a normal user
Solution
Step 1: Understand what sudo does
sudo allows a user to run commands with elevated privileges, usually root access.
Step 2: Compare options with sudo purpose
Only To run commands with higher privileges than a normal user describes running commands with higher privileges, which matches sudo's main use.
Final Answer:
To run commands with higher privileges than a normal user -> Option D
Quick Check:
sudo = elevated privileges [OK]
Hint: sudo means run as superuser or admin [OK]
Common Mistakes:
Confusing sudo with user switching commands
Thinking sudo creates users
Assuming sudo lists files
2. Which of the following is the correct syntax to run the command apt update with elevated privileges?
easy
A. sudo apt update
B. apt sudo update
C. sudo -u apt update
D. apt update sudo
Solution
Step 1: Recall the correct sudo syntax
The correct syntax is sudo [command], so sudo apt update is valid.
Step 2: Check each option
Options A, B, and D place sudo incorrectly or misuse flags.
Final Answer:
sudo apt update -> Option A
Quick Check:
sudo before command = correct syntax [OK]
Hint: Put sudo before the command to elevate it [OK]
Common Mistakes:
Placing sudo after the command
Using sudo with wrong flags
Mixing sudo with user flags incorrectly
3. What will be the output of the command sudo whoami if run by a normal user?
medium
A. root
B. sudo
C. normal user name
D. Permission denied
Solution
Step 1: Understand what whoami does
whoami prints the current user's name.
Step 2: Effect of sudo on whoami
Running sudo whoami runs the command as root, so it prints root.
Final Answer:
root -> Option A
Quick Check:
sudo changes user to root for command [OK]
Hint: sudo runs command as root user [OK]
Common Mistakes:
Expecting normal username output
Thinking sudo prints 'sudo'
Assuming permission denied without sudo
4. You try to run sudo apt-get update but get the error: sudo: command not found. What is the most likely cause?
medium
A. You typed the command incorrectly
B. You are not logged in as root
C. The sudo package is not installed or not in PATH
D. You need to use su instead of sudo
Solution
Step 1: Analyze the error message
sudo: command not found means the system cannot find the sudo program.
Step 2: Identify common reasons
This usually happens if sudo is not installed or its location is missing from the PATH environment variable.
Final Answer:
The sudo package is not installed or not in PATH -> Option C
Quick Check:
command not found = missing sudo or PATH [OK]
Hint: command not found means sudo missing or PATH wrong [OK]
Common Mistakes:
Assuming user login affects sudo command presence
Thinking command typo causes 'command not found'
Confusing su and sudo usage errors
5. You want to run a script backup.sh that requires root permissions. Which command correctly runs it with sudo and ensures the script's environment variables are preserved?
hard
A. sudo ./backup.sh
B. sudo -E ./backup.sh
C. sudo -u root ./backup.sh
D. sudo --preserve-env=ALL ./backup.sh
Solution
Step 1: Understand environment preservation with sudo
By default, sudo resets environment variables for security. The -E flag preserves the user's environment.
Step 2: Evaluate options for preserving environment
sudo -E ./backup.sh uses -E which preserves environment variables. sudo --preserve-env=ALL ./backup.sh uses a longer form but --preserve-env=ALL is less common and may not be supported everywhere.
Final Answer:
sudo -E ./backup.sh -> Option B
Quick Check:
sudo -E preserves environment variables [OK]
Hint: Use sudo -E to keep environment variables [OK]
Common Mistakes:
Forgetting environment variables reset by sudo
Using sudo without -E loses variables
Confusing -u root with environment preservation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.