Managing sudo access is an important task for Linux administrators. Instead of logging in directly as root, it is considered a best practice to grant administrative privileges to individual users using sudo.
This article explains how to:
- Add a user to the sudo list
- Configure sudo timeout settings
-
Safely edit the
sudoersfile usingvisudo - Switch to the new privileged user
Why Use sudo Instead of Root?
Using sudo provides several advantages:
- Better security
- Command auditing
- Reduced risk of accidental system damage
- Controlled privilege escalation
- Individual accountability for administrative actions
Instead of sharing the root password, each user can use their own credentials.
Important: Never Edit sudoers Directly
The /etc/sudoers file controls sudo access.
Never edit this file using standard editors like:
-
vi -
vim -
nano
Direct editing can corrupt the file if multiple administrators edit simultaneously or if syntax errors are introduced.
Step 1 – Open sudoers File
Run:
sudo visudoStep 2 – Navigate to Bottom of File
Inside the editor: Press
Shift + GThis moves the cursor to the end of the document.
Step 3 – Enter Insert Mode
Press: i
This enables editing mode.
Step 4 – Configure sudo Timeout
Add the following line at the bottom of the file:
Defaults timestamp_timeout=1800
Step 5 – Add User to sudo Privileges
Locate the section containing:
root ALL=(ALL) ALL
Below that line, add:
myuserid ALL=(ALL) ALL
Note:Replace myuserid with your actual Linux username.
Step 6 – Save and Exit
Press:ESC
Then type::wq
Press Enter.
This will: Save the file and Exit the editor
Step 7 – Switch to the New User
Now switch from the current user (example: opc) to the new sudo-enabled user.
su - myuserid
Verify sudo Access
Run a simple sudo command:
sudo whoami