
When setting up an Ubuntu server, it’s essential to create a separate user with sudo privileges to execute administrative commands securely. This guide will walk you through the process step by step.
Step 1: Log in as Root
Before creating a new sudo user, ensure you are logged into your Ubuntu server as the root user or an existing sudo user.
ssh root@your_server_ip
Step 2: Create a New User
Use the adduser
command to create a new user. Replace username
with your preferred name.
adduser username
You will be prompted to set a password and provide optional user details.
Step 3: Grant Sudo Privileges
Once the user is created, add them to the sudo
group to grant administrative privileges:
usermod -aG sudo username

Step 4: Verify Sudo Access
Switch to the new user account:
su - username
To check if the user has sudo privileges, run:
sudo whoami
If configured correctly, the output should be root
.
Step 5: Secure Your Server
For enhanced security, disable root login via SSH. Open the SSH configuration file:
nano /etc/ssh/sshd_config
Find the line:
PermitRootLogin yes
Change it to:
PermitRootLogin no
Save the file and restart SSH:
systemctl restart ssh
Conclusion
Creating a sudo user on an Ubuntu server enhances security by preventing direct root access. By following these steps, you can manage administrative tasks safely and efficiently.
How to Create a Sudo User in Ubuntu Server (F.A.Q)
Why should I create a sudo user instead of using root?
Using a sudo user minimizes security risks, as it prevents accidental system-wide changes and reduces vulnerability to attacks.
How do I delete a sudo user?
To delete a user, use:
sudo deluser username
To remove their home directory as well:
sudo deluser --remove-home username
What if I forget my sudo user password?
Log in as root and reset the password with:
passwd username
Can I add an existing user to the sudo group?
Yes, use:
usermod -aG sudo existing_username