How to Add and Delete Users on Ubuntu (EC2, WSL, or Any Server)

3 min read

Managing user accounts is one of the first things you’ll do on a new server. Whether you’re setting up an EC2 instance, a WSL environment, or any Ubuntu machine, the commands are the same. This guide covers how to add and delete users on Ubuntu, grant sudo access, set up SSH keys for new users, and list existing accounts.

The examples here are run on WSL2 Ubuntu in Windows, but they work identically on EC2 instances and any other Ubuntu system.

Adding a User

Use adduser to create a new account. It creates the home directory, sets the shell, and prompts you for a password:

sudo adduser deploy

You’ll be prompted to set a password and enter optional info (full name, phone, etc.). Press Enter to skip the optional fields.

Verify the account was created:

id deploy
uid=1001(deploy) gid=1001(deploy) groups=1001(deploy)

adduser vs useradd

Ubuntu has two commands for creating users:

CommandWhat It Does
adduserInteractive — creates home directory, sets shell, prompts for password. Use this for regular user creation.
useraddLow-level — does the bare minimum. No home directory or password unless you pass flags. Used in scripts.

Stick with adduser for interactive use. Use useradd in automation scripts where you want full control over the flags.

Granting Sudo Access

Add the user to the sudo group so they can run commands as root:

sudo usermod -aG sudo deploy
  • -a — append (don’t replace existing groups)
  • -G sudo — add to the sudo group

Verify by switching to the new user and running a sudo command:

su - deploy
sudo whoami

If it returns root, sudo access is working. Only grant sudo to users who actually need it.

Setting Up SSH Access for the New User

On EC2 or any remote server, you’ll want the new user to log in via SSH key instead of a password. As root or a sudo user, create the .ssh directory and authorized_keys file for the new user:

sudo mkdir -p /home/deploy/.ssh
sudo nano /home/deploy/.ssh/authorized_keys

Paste the user’s public key (their id_ed25519.pub or id_rsa.pub contents), save, and set the correct permissions:

sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keys
sudo chown -R deploy:deploy /home/deploy/.ssh

The user can now SSH in with ssh deploy@your-server-ip. For a full walkthrough of SSH key setup, see How to Setup Passwordless SSH Login on EC2 Ubuntu 22.04.

Listing Users

To see all user accounts on the system:

cut -d: -f1 /etc/passwd

This lists every account, including system accounts. To show only users with a login shell (real human accounts):

grep -E '/bin/(bash|sh|zsh)' /etc/passwd | cut -d: -f1

To check which groups a specific user belongs to:

groups deploy

Deleting a User

Remove a user account but keep their home directory and files:

sudo deluser deploy

To remove the user and delete their home directory and mail spool:

sudo deluser --remove-home deploy

Verify the user is gone:

id deploy

This should return id: 'deploy': no such user.

deluser vs userdel

Similar to the add commands, Ubuntu provides two options:

CommandWhat It Does
deluserHigher-level, Ubuntu-friendly. Handles group cleanup. Use --remove-home to delete files.
userdelLow-level. Use -r to remove home directory. Used in scripts.

Quick Reference

TaskCommand
Add a usersudo adduser username
Grant sudo accesssudo usermod -aG sudo username
Check user infoid username
List all login usersgrep -E '/bin/(bash|sh|zsh)' /etc/passwd | cut -d: -f1
Delete user (keep files)sudo deluser username
Delete user + home directorysudo deluser --remove-home username
Change user’s passwordsudo passwd username
Lock a user accountsudo usermod -L username
Unlock a user accountsudo usermod -U username

Conclusion

Use adduser to create accounts, usermod -aG sudo to grant admin access, and deluser --remove-home to clean up accounts you no longer need. These commands work the same on EC2, WSL, and any Ubuntu installation.

If you’re setting up a fresh EC2 instance, see How to deploy EC2 Ubuntu 22.04 LTS on AWS. For WSL setup, check out How to Install Ubuntu 20.04 or 22.04 in WSL 2 on Windows 10.