Operations 7 min read

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.

21CTO
21CTO
21CTO
Mastering sudo: Essential Linux Privilege Tricks for Everyday Use

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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxcommand-lineSystem Administrationprivilege escalationSudo
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.