How to Add a Sudo User in Ubuntu

Learn how to add a sudo user in Ubuntu safely. Step-by-step commands, best practices, and security tips for VPS and servers.

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

👉 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:

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

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:

Tharindu

Hey!! I'm Tharindu. I'm from Sri Lanka. I'm a part time freelancer and this is my blog where I write about everything I think might be useful to readers. If you read a tutorial here and want to hire me, contact me here.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button