Mastering sudo: 5 Real-World Scenarios to Elevate Your Linux Permissions
This guide walks through five common sudo use‑cases on Linux—granting temporary root rights, fixing forgotten sudo in vim, re‑executing commands with sudo, handling shell built‑ins, and configuring sudo logging—providing clear examples and configuration tips for system administrators.
Linux sudo lets regular users run selected root commands. The article examines typical sudo scenarios and shows how to configure and use them effectively.
Scenario 1: User Lacks Permission to Run a Root Command
When a normal user cannot access a file or execute a command, adding sudo before the command temporarily grants root privileges. Before sudo, su was used, which required knowing the root password. sudo avoids that requirement.
Permissions are defined in /etc/sudoers. To grant a single user:
The fields mean:
guohl – the username allowed to use sudo
ALL – from any terminal
(ALL) – as any user
ALL – any command
To restrict user test on host guohl-pc to only run /bin/chown and /bin/chmod as root:
Group‑based rules replace the username with %groupname. Users in the sudo group inherit the group’s permissions.
Scenario 2: Forgetting sudo While Editing with vim
If you edit a file with vim and try to save ( :wq) without sufficient rights, you can stay in vim and run: :w !sudo tee % This writes the file using root privileges without leaving the editor.
Scenario 3: Running a Command Without sudo
When a long command fails due to missing permissions, instead of re‑typing it, use: sudo !! Here !! expands to the previous command, automatically prefixing it with sudo.
Scenario 4: Using sudo with Shell Built‑in Commands
Shell built‑ins cannot be prefixed with sudo because sudo forks a new process to run an external program. To run a built‑in as root, first obtain a root shell: sudo bash After entering the root shell, any command can be executed without additional sudo. You can inspect a command’s type with type:
Scenario 5: Logging sudo Operations
Administrators can record every command executed via sudo, enhancing security and aiding troubleshooting. Basic logging configuration is required in /etc/sudoers or related files.
References
sudo manual
7 Linux sudo Command Tips and Tricks
sudo logging configuration
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
