In Linux, the “root” user has absolute power over the system. Logging in directly as root is highly dangerous, as a single typo can destroy the operating system. Instead, the best practice is to log in as a standard user and use the sudo command to temporarily elevate your privileges only when necessary.
However, a standard user cannot use the sudo command by default. They must first be explicitly granted permission by being added to a highly sensitive configuration document known as the sudoers file. Editing this file incorrectly can instantly lock you out of your own server. This guide explains how to add a user safely using the dedicated visudo tool.
Why You Should Never Use a Standard Text Editor
The sudoers file is located at /etc/sudoers. Because it is a text file, you might be tempted to open it using a standard text editor like Nano or Vim (e.g., sudo nano /etc/sudoers). Do not do this.
If you accidentally make a syntax error, delete a vital comma, or misspell a command while editing with Nano, the system will immediately reject the entire file. The moment you save and close it, everyone on the system loses sudo access, including you. The only way to fix it is to reboot the server into a specialized recovery mode.
The Safe Method: Using visudo
Linux includes a specialized tool specifically designed to prevent this catastrophe: visudo.
When you run visudo, it opens a temporary copy of the sudoers file in your default terminal editor. When you attempt to save and exit, visudo actively scans the file for syntax errors. If it detects a mistake, it halts the save process, warns you, and refuses to overwrite the real file until you fix the error.
How to Add the User
Assuming you have a user named “alex” who needs administrative privileges, here is the safe process.
- Log into your server using an account that already has root or sudo privileges.
- Launch the safe editing tool by typing:
sudo visudo - The file will open in your terminal. Use your arrow keys to scroll down until you find a section that looks like this:
# User privilege specification
root ALL=(ALL:ALL) ALL
This line dictates that the “root” user can run any command, anywhere.
- Use your arrow keys to move the cursor to the blank line directly below the root entry.
- To grant the user “alex” the exact same absolute privileges as root, type the following line exactly as shown:
alex ALL=(ALL:ALL) ALL
Saving and Exiting
The method for saving depends on which text editor your system has assigned as the default for visudo.
- If it opened in Nano (look for a menu at the bottom with ^O and ^X): Press
Ctrl + O(the letter O) to write the changes, pressEnterto confirm the filename, and pressCtrl + Xto exit. - If it opened in Vim (no menu at the bottom): Press the
Esckey, type:wq(colon, w, q), and pressEnter.
If you typed the permissions correctly, visudo will silently copy the temporary file over the real /etc/sudoers file, and return you to the command prompt.
If you made a typo, it will print a “syntax error” warning and ask “What now?”. Press the e key to re-edit the file and fix your mistake.
Testing the Permissions
To verify it worked, switch to the newly empowered user account:
su - alex
Now, attempt to run a command that requires administrative privileges, such as updating the package manager:
sudo apt update (or sudo dnf update)
The system will prompt you for alex’s password. If the update runs successfully, the user has been safely added to the sudoers file.