sodcalculator

10 Tech How-To

How to Create a Sudo User on Ubuntu 20.04 LTS

Guide 10-31254 · Updated · Step by step

Creating a sudo user on Ubuntu 20.04 LTS is a straightforward way to grant administrative privileges to a specific account without logging in as root. This guide walks you through adding a new user and giving them sudo access, which is essential for running system-level commands securely. You’ll need an existing account with sudo privileges (usually the initial user created during installation) to follow along.

What You’ll Need

  • Access to an Ubuntu 20.04 LTS server or desktop with a terminal
  • An existing user account that already has sudo privileges (or root access)
  • Your password for that account

Step-by-Step

  1. Log in to your Ubuntu system using the account that already has sudo privileges. Open a terminal window if you’re on a desktop, or connect via SSH if it’s a server.
  2. Create the new user by running: sudo adduser newusername (replace newusername with your desired username). This command creates the user’s home directory and prompts you to set a password. Enter a strong password and confirm it. You can skip the optional fields (Full Name, Room Number, etc.) by pressing Enter.
  3. Verify the user was created by running: id newusername. You should see the user’s UID, GID, and groups listed.
  4. Add the user to the sudo group with: sudo usermod -aG sudo newusername. The -aG flags append the user to the sudo group without removing them from any existing groups.
  5. Confirm the user is in the sudo group by running: groups newusername. The output should include sudo.
  6. Test the sudo access by switching to the new user: su - newusername. Then run a sudo command, for example: sudo whoami. When prompted, enter the new user’s password. The output should be root, confirming that sudo works.
  7. Exit back to your original session by typing exit.

Common Mistakes

  • Forgetting the -a flag: If you run usermod -G sudo without -a, you’ll replace the user’s supplementary groups with just sudo, which can break access to other resources.
  • Using adduser vs useradd: adduser is the interactive, friendly wrapper that sets up home directories and prompts for a password. useradd is the lower-level tool that doesn’t create a home directory or password by default. Use adduser unless you have a specific reason not to.
  • Typing the username incorrectly: Double-check the username spelling in every command. A typo can lead to creating an unintended user or adding the wrong account to sudo.
  • Not testing with sudo whoami: Skipping the test step can leave you assuming sudo works when it doesn’t, causing frustration later.

Tips

  • Use a strong password: The sudo user has significant power, so choose a password that is long, unique, and not easily guessable.
  • Consider SSH key authentication: If this is a server, set up SSH keys for the new user to avoid password-based logins, which are more vulnerable to brute-force attacks.
  • Keep the sudo group small: Only grant sudo access to users who truly need it. Fewer sudo users means fewer potential security risks.
  • Use sudo -i carefully: When you run sudo -i, you get a root shell. This can be convenient but also dangerous. Prefer using sudo before individual commands to limit the scope of root privileges.

Troubleshooting

If you get “user is not in the sudoers file” when testing sudo, the user was not added to the sudo group correctly. Re-run the usermod command with the -aG flags and verify with groups newusername.

If you can’t create the user because the username already exists, choose a different username or check if the existing user already has sudo access.

If you’re locked out of sudo (e.g., you accidentally removed your own sudo access), you’ll need to reboot into recovery mode or use a live USB to edit the /etc/sudoers file. This is advanced and should be done carefully.

If you forget the new user’s password, you can reset it as the root user or with an existing sudo user using: sudo passwd newusername.

Frequently Asked Questions

Can I create a sudo user without an existing sudo account?

No, you need an account with sudo privileges (or root access) to grant sudo to another user. If you have root access, you can use adduser and usermod directly.

What’s the difference between adduser and useradd?

adduser is a friendly script that creates a user with a home directory, sets a password, and copies default files. useradd is the low-level utility that only adds the user entry; you’d need to manually create the home directory and set a password.

Is it safe to use sudo instead of root?

Yes, sudo is safer because it logs all commands and requires a password for each session, reducing the risk of accidental catastrophic changes compared to always being root.

Can I give sudo access to multiple users at once?

Yes, you can run the usermod command for each user, or you can edit the /etc/sudoers file to define groups, but adding users to the sudo group is the simplest method.

How do I remove sudo access from a user?

Remove the user from the sudo group with: sudo gpasswd -d username sudo or sudo deluser username sudo.

What if I want a user to have limited sudo privileges?

You can create a custom sudoers file in /etc/sudoers.d/ to specify exactly which commands the user can run. This is more advanced but provides finer control.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →