How to Create a Non-Root User on Your Server

Creating a non-root user on your server is a fundamental security practice that limits the damage of accidental mistakes and malicious attacks. Instead of logging in as the all-powerful root account, you create a separate user with only the permissions it needs, then use that user for daily tasks. This guide walks you through the process on a typical Linux server, covering everything from user creation to granting administrative privileges.

What You’ll Need

  • Access to a Linux server (Ubuntu, Debian, CentOS, or similar) with SSH enabled.
  • The root password or an SSH key that allows you to log in as root.
  • Basic familiarity with the command line (you’ll be typing commands in a terminal).

Step-by-Step

  1. Log in to your server as root. Open a terminal and use SSH: ssh root@your_server_ip. Enter your password or use your key. If you’re already on the server, skip this step.
  2. Create a new user. Use the adduser command (on Debian/Ubuntu) or useradd (on CentOS/RHEL). For a simple, interactive approach, type: adduser newusername. Replace newusername with your desired username. You’ll be prompted to set a password and fill in optional user info (full name, phone, etc.) – you can press Enter to skip those.
  3. Add the user to the sudo group (optional but recommended). The sudo command allows a regular user to run administrative commands with elevated privileges. On Debian/Ubuntu, add the user to the sudo group: usermod -aG sudo newusername. On CentOS/RHEL, the group is often called wheel: usermod -aG wheel newusername. This step lets your new user run commands like sudo apt update without logging in as root.
  4. Test the new user’s login. Open a new terminal session and log in as the new user: ssh newusername@your_server_ip. Enter the password you set. If you’re on the same machine, you can switch users with su - newusername.
  5. Verify sudo access. Once logged in as the new user, run a command that requires root privileges, such as sudo whoami. It should output root. If it asks for a password, enter the new user’s password (not the root password). This confirms sudo works.
  6. Disable root login over SSH (optional but strongly recommended). For better security, edit the SSH configuration file: sudo nano /etc/ssh/sshd_config. Find the line PermitRootLogin and change it to no. Save the file and restart the SSH service: sudo systemctl restart sshd (or service ssh restart on older systems). Important: Before doing this, ensure your new user can log in and has sudo access, or you might lock yourself out.
  7. Set up SSH keys for the new user (optional but recommended). If you use SSH keys for root, copy your public key to the new user’s home directory. On your local machine, run: ssh-copy-id newusername@your_server_ip. This will allow passwordless login for the new user, which is both more secure and more convenient.

Common Mistakes

  • Forgetting to add the user to the sudo group. Without this, you’ll be stuck as a regular user and have to log in as root for every administrative task, defeating the purpose.
  • Using useradd without options. On some systems, useradd creates a user without a home directory or a shell. Always use adduser on Debian/Ubuntu, or on CentOS use useradd -m -s /bin/bash username to create a home directory and set the shell.
  • Disabling root login before testing the new user. If the new user can’t log in or doesn’t have sudo, you’ll be locked out entirely. Always test thoroughly first.
  • Setting a weak password. A simple password defeats the purpose of having a separate user. Use a long, random password or, better, set up SSH keys.
  • Not updating the SSH configuration correctly. If you edit sshd_config incorrectly, SSH may fail to restart. Always make a backup before editing.

Tips

  • Use a descriptive username. Something like deploy or developer is clearer than user1.
  • Consider creating separate users for different purposes. For example, one user for web development and another for database management, each with minimal permissions.
  • Use SSH keys instead of passwords. Keys are much harder to brute-force than passwords. Generate a key pair on your local machine and copy the public key to the server.
  • Regularly review user accounts. Remove any unused accounts to reduce attack surface.
  • Set up two-factor authentication (2FA) for SSH. This adds an extra layer of security, but it’s optional for most setups.

Troubleshooting

You can’t log in as the new user. Check that the username is spelled correctly and that the password was set. If you used useradd, verify the user has a home directory and a shell: grep newusername /etc/passwd. If the home directory is missing, create it with mkdir /home/newusername and set ownership: chown newusername:newusername /home/newusername.

Sudo doesn’t work. Ensure the user is in the correct group. On Debian/Ubuntu, check with groups newusername. If the group is missing, add it again. On CentOS, the group might be wheel, not sudo. Also, the sudoers file must allow the group to use sudo. The default configuration usually does, but if you’ve modified it, check with visudo.

You’re locked out of the server. If you disabled root login and the new user can’t get in, you’ll need to use the server’s web-based console (if your provider offers one) or a recovery mode. In the worst case, you may need to reinstall the server. This is why testing is so important.

SSH service won’t restart. Check the configuration for syntax errors: sudo sshd -t. This will report any issues before you restart. If you made a mistake, revert to the backup you made.

Frequently Asked Questions

Why can’t I just use root for everything?

Root has unrestricted access to the entire system. A single typo in a command like rm -rf / can destroy your server. Using a non-root user limits the scope of mistakes and makes it harder for attackers to gain full control.

What’s the difference between adduser and useradd?

adduser is a friendly, interactive wrapper that sets up home directories, shells, and prompts for passwords. useradd is a low-level tool that requires you to specify options. For beginners, adduser is easier.

Can I have multiple non-root users?

Yes, you can create as many as you need. Each user has its own home directory and permissions. This is useful for separating projects or team members.

How do I delete a user I created?

Use deluser username on Debian/Ubuntu or userdel -r username on CentOS. The -r flag removes the home directory. Be careful, as this deletes all files owned by that user.

Do I need to set up SSH keys for the new user?

It’s highly recommended. SSH keys are more secure than passwords and allow automated scripts to connect without prompts. You can copy your existing key or generate a new one.

What if I’m on a server without sudo installed?

Most Linux distributions come with sudo. If not, you can install it as root: apt install sudo (Debian/Ubuntu) or yum install sudo (CentOS). Then proceed with the group addition steps.

Creating a non-root user is a simple yet powerful step toward securing your server. By following this guide, you’ve reduced the risk of accidental damage and made your server less vulnerable to attacks. Remember to always test new configurations and keep your system updated.