Mastering sudo: Essential Linux Privilege Tricks for Everyday Use
This guide walks through common sudo scenarios—granting temporary root rights, fixing permission errors in vim, re‑executing forgotten sudo commands, handling shell built‑ins, and configuring sudo logging—to help Linux users work more efficiently and securely.
Linux’s sudo command lets regular users run selected root commands. Below are typical usage scenarios and tips.
Scenario 1: User lacks permission to run root commands
When a normal user needs root privileges, prefix the command with sudo. Before sudo existed, su was used, which required the root password. Permissions are defined in /etc/sudoers.
Granting a single user:
guohl – username allowed to use sudo
ALL – from any terminal
(ALL) – as any user
ALL – any command
To let user test run only /bin/chown and /bin/chmod on host guohl-pc as root:
Similarly, replace the username with %group to grant the rule to all members of a group (e.g., the sudo group).
Scenario 2: Forgot sudo while editing with vim
If you edit a file in vim and realize you lack permission to save, you can run: :w !sudo tee % This writes the file with root privileges without leaving the editor.
Scenario 3: Forgot sudo before a command
When a command fails due to missing privileges, instead of re‑typing it, simply execute: sudo !! Here !! expands to the previous command.
Scenario 4: Using sudo with shell built‑in commands
Shell built‑ins (e.g., cd, type) cannot be prefixed with sudo because sudo runs external programs. To run a built‑in as root, first obtain a root shell: sudo bash After entering the root shell, you can execute any command without additional sudo prefixes.
Scenario 5: sudo command logging
Administrators can record every command run via sudo, enhancing security and aiding troubleshooting. Basic logging configuration involves editing /etc/sudoers to enable log output.
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.
