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:
| Command | What It Does |
|---|---|
adduser | Interactive — creates home directory, sets shell, prompts for password. Use this for regular user creation. |
useradd | Low-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 thesudogroup
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. If you’d rather not open port 22 at all on EC2, you can reach the instance with AWS SSM Session Manager instead.
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 regular (human) accounts, filter by UID. On Ubuntu, regular users start at UID 1000, and 65534 is the special nobody account:
awk -F: '$3 >= 1000 && $3 < 65534 {print $1}' /etc/passwd
Filtering by UID is more reliable than matching shell paths, which miss shells like /usr/bin/zsh and still include root.
To check which groups a specific user belongs to:
groups deploy
Group membership is also how you grant access to things like the Docker daemon. See Resolving WSL Permission Denied When Connecting to the Docker Daemon for a docker group example.
Deleting a User
Deletion fails if the user still has running processes (for example an open SSH session or a background job). Stop them first:
sudo pkill -u deploy
Then remove the 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:
| Command | What It Does |
|---|---|
deluser | Higher-level, Ubuntu-friendly. Handles group cleanup. Use --remove-home to delete files. |
userdel | Low-level. Use -r to remove home directory. Used in scripts. |
Locking an Account Instead of Deleting It
If you only want to disable a user temporarily, lock the account rather than deleting it. Note that usermod -L on its own only locks the password. A user with an SSH key in authorized_keys can still log in. To block all logins, also expire the account:
sudo usermod -L -e 1 deploy
-e 1 sets the expiry date to 2 January 1970, so the account is treated as expired. To re-enable it, unlock the password and clear the expiry date:
sudo usermod -U -e '' deploy
Quick Reference
| Task | Command |
|---|---|
| Add a user | sudo adduser username |
| Grant sudo access | sudo usermod -aG sudo username |
| Check user info | id username |
| List all login users | awk -F: '$3 >= 1000 && $3 < 65534 {print $1}' /etc/passwd |
| Stop the user’s processes | sudo pkill -u username |
| Delete user (keep files) | sudo deluser username |
| Delete user + home directory | sudo deluser --remove-home username |
| Change user’s password | sudo passwd username |
| Lock a user account (password + SSH key) | sudo usermod -L -e 1 username |
| Unlock a user account | sudo usermod -U -e '' 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.


