If you’re managing a VPS or Ubuntu server, one of the first things you should do is create a new sudo user and stop using the root account.
Using root directly is risky. A simple mistake can break your entire server.
In this guide, I’ll show you the correct and secure way to add a sudo user in Ubuntu, step by step.
Table of Contents
Why You Should Not Use Root
- No safety restrictions (can break system easily)
- Higher security risk (common target for attacks)
- No accountability (no user tracking)
👉 Best practice: Use a normal user with sudo privileges.
Step 1: Log in as Root
If you’re not already root, log in:
ssh root@your_server_ip
Step 2: Create a New User
Set Your Username Variable (Recommended)
Instead of typing the username repeatedly, define it once:
NEW_USER="yourusername"
✅ Example:
NEW_USER="vpsfix"
💡 About $NEW_USER
$NEW_USER is a temporary variable that stores your username, allowing you to reuse it across commands instead of typing it repeatedly. It only exists in your current terminal session—if you disconnect or start a new session, you’ll need to define it again. Always include the $ when using it (e.g., adduser $NEW_USER), otherwise it will be treated as plain text.
Now we can reuse $NEW_USER in all commands.
adduser $NEW_USER
You’ll be prompted to:
- Set a password
- Enter optional user details (you can press Enter to skip)
Step 3: Add User to Sudo Group
This is the key step:
usermod -aG sudo $NEW_USER
This gives the user admin privileges.
Step 4: Test the Sudo Access
Switch to the new user:
su - $NEW_USER
Then run:
sudo whoami
Expected output:
root
✅ This confirms sudo is working.
Step 5: Secure Your Server (Recommended)
Now that your sudo user works, disable root login:
Edit SSH config:
nano /etc/ssh/sshd_config
Find:
PermitRootLogin yes
Change to:
PermitRootLogin no
Restart SSH:
systemctl restart ssh
Optional: Add SSH Key Authentication
For better security, set up SSH keys:
chmod 700 /home/$NEW_USER/.ssh
chmod 600 /home/$NEW_USER/.ssh/authorized_keys
Paste your public key and save.
Then fix permissions:
chown -R $NEW_USER:$NEW_USER /home/$NEW_USER/.ssh
chmod 700 /home/$NEW_USER/.ssh
chmod 600 /home/$NEW_USER/.ssh/authorized_keys
Common Mistakes to Avoid
- Forgetting to add user to sudo group
- Disabling root before testing sudo
- Wrong file permissions on .ssh
- Using weak passwords
Conclusion
Adding a sudo user in Ubuntu is simple—but it’s one of the most important steps for securing your server.
Once done, you should:
- Stop using root
- Enable SSH key login
- Harden your server further