How to Add a User in Linux, Set a Password and Give sudo Access
To add a user in Linux, run sudo adduser priya on Ubuntu/Debian, or sudo useradd -m priya then sudo passwd priya on Amazon Linux, RHEL and CentOS. To give admin rights, add the user to the sudo group (Ubuntu) or the wheel group (Amazon Linux/RHEL) with sudo usermod -aG. On EC2, put the user's public SSH key in /home/priya/.ssh/authorized_keys, so each person logs in with their own account.
Quick answer
# Ubuntu / Debian
sudo adduser priya
sudo usermod -aG sudo priya
# Amazon Linux 2023 / RHEL / CentOS
sudo useradd -m -s /bin/bash priya
sudo passwd priya
sudo usermod -aG wheel priya
id priya # check groups
A live example first
Three trainees join a project. Until now, everyone logged in to the Amazon Linux 2023 server as ec2-user with the same .pem key. We give Priya her own account, with sudo, and her own key.
- Create the user and add her to
wheel:
sudo useradd -m -s /bin/bash priya
sudo usermod -aG wheel priya
id priya
uid=1001(priya) gid=1001(priya) groups=1001(priya),10(wheel)
- Priya creates a key pair on her own laptop and sends you only the public key:
ssh-keygen -t ed25519 -C "priya-laptop"
cat ~/.ssh/id_ed25519.pub
- On the server, add it with the right owner and permissions:
sudo mkdir -p /home/priya/.ssh
echo "ssh-ed25519 AAAAC3Nza...xyz priya-laptop" | sudo tee /home/priya/.ssh/authorized_keys
sudo chown -R priya:priya /home/priya/.ssh
sudo chmod 700 /home/priya/.ssh
sudo chmod 600 /home/priya/.ssh/authorized_keys
- Because she logs in with a key and has no password, allow
sudowithout a password forwheel, or set a password withsudo passwd priyaso sudo can ask for it. - Priya tests from her laptop:
ssh priya@<PUBLIC_IP>
sudo -v && echo "sudo works"
Now the logs in /var/log/secure show priya instead of a shared ec2-user. Friends, when she leaves the project, we remove only her account. Nobody else has to change keys.
How do Linux users and groups work?
One person, one account. Add sudo only when needed, and lock or remove users when they leave.
Each user has a UID, a home folder and a login shell, stored in /etc/passwd (passwords are hashed in /etc/shadow). Groups are in /etc/group. sudo reads /etc/sudoers, which on Ubuntu allows the sudo group and on RHEL-family systems allows the wheel group to run commands as root.
How do I add a user step by step?
- Choose a username (lowercase, no spaces).
- Create the user with a home folder (
adduseroruseradd -m). - Set a password, or plan for key-only login.
- Add groups:
sudo/wheelonly if they really need admin rights,dockeronly if they must run containers (it is root-equivalent),www-dataorapacheto share web files. - Add their SSH public key.
- Test the login in a new terminal before closing your own session.
- Write down who has which access.
Useful user commands
| Task | Command |
|---|---|
| Create (Ubuntu) | sudo adduser NAME |
| Create (any) | sudo useradd -m -s /bin/bash NAME |
| Set password | sudo passwd NAME |
| Add to group | sudo usermod -aG GROUP NAME |
| List groups | id NAME, groups NAME |
| Switch user | sudo su - NAME |
| Lock account | sudo usermod -L NAME and sudo chage -E 0 NAME |
| Delete with home | sudo userdel -r NAME |
| Who is logged in | who, last -n 10 |
Safer sudo with sudoers.d
Never edit /etc/sudoers directly. Use a small file and check it:
echo "priya ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx" | sudo tee /etc/sudoers.d/priya
sudo chmod 440 /etc/sudoers.d/priya
sudo visudo -c
This lets Priya restart nginx and nothing else. That is least privilege.
App users, per stack
Apps should also run as their own non-login users, not as root:
| Stack | Typical service user | Create |
|---|---|---|
| nginx / Apache | nginx, www-data, apache |
created by the package |
| Node.js app | nodeapp |
sudo useradd -r -s /sbin/nologin nodeapp |
| Python Gunicorn | pyapp |
same, with User=pyapp in the systemd unit |
| Java / Tomcat | tomcat |
created by the package or by hand |
| MySQL / PostgreSQL | mysql, postgres |
created by the package |
| Docker containers | USER app in the Dockerfile |
RUN useradd -r app |
-r makes a system user, and /sbin/nologin stops anyone from logging in as it.
Real example: why shared logins hurt
In the SolarWinds investigation (2020–2021), company leaders told the US Congress that an intern had set a weak password, "solarwinds123", on an update server, and that it was found exposed on GitHub. Whatever its role in the bigger attack, the lesson for every team is clear: shared, weak credentials are hard to track and hard to take back. Personal accounts, SSH keys and least-privilege sudo make every action traceable to one person.
Mistakes to avoid
usermod -G sudowithout-a. It removes the user from all other groups.- Pasting a private key on the server. Only the public key (
.pub) goes inauthorized_keys. - Wrong permissions on
.ssh. SSH ignores keys if the folder or file is writable by others. - Giving everyone sudo. Most people need only a few commands.
Summary table
| Step | Ubuntu | Amazon Linux / RHEL |
|---|---|---|
| Create | adduser NAME |
useradd -m NAME |
| Password | asked by adduser | passwd NAME |
| Admin | usermod -aG sudo NAME |
usermod -aG wheel NAME |
| SSH key | ~NAME/.ssh/authorized_keys 600 |
same |
| Check | id NAME, sudo -l -U NAME |
same |
| Remove | userdel -r NAME |
same |
Quick quiz: check what you learned
Which group gives sudo rights on Amazon Linux and RHEL?
- wheel
- sudo
- admin
- root
Show answer
A. RHEL-family systems use the wheel group for sudo.
What does -aG do in usermod -aG sudo priya?
- Adds to group, keeps others
- Removes all other groups
- Makes the user an admin
- Creates the home folder
Show answer
A. -a appends; without it, other groups are replaced.
Which permissions should authorized_keys have?
- 600 owned by the user
- 777 owned by root
- 644 owned by root
- 755 owned by nobody
Show answer
A. SSH ignores key files that others can write.
Why not share one ec2-user login among the team?
- You cannot tell who did what
- It makes SSH much slower
- Linux allows only one user
- It breaks the sudo command
Show answer
A. Separate accounts give accountability and easy removal.
Learn it properly
These pages go deeper, with labs:
Frequently asked questions
What is the difference between useradd and adduser?
useradd is the basic tool on every Linux. adduser on Ubuntu/Debian is a friendlier script that creates the home folder and asks for a password. On RHEL and Amazon Linux, adduser is just a link to useradd.
How do I give a user sudo rights?
Add them to the admin group: sudo usermod -aG sudo NAME on Ubuntu or sudo usermod -aG wheel NAME on Amazon Linux, RHEL and CentOS.
Why does the new user not get sudo right away?
Group changes apply at the next login. Log out and log in again, then run groups.
How do I let a new user SSH into EC2?
Create /home/NAME/.ssh/authorized_keys with their public key, set the folder to 700 and the file to 600, and make the user the owner.
How do I remove a user?
Run sudo userdel -r NAME to delete the user and their home folder. To keep files, lock the account with sudo usermod -L NAME and expire it.
Why not share the ec2-user or ubuntu account?
With a shared account you cannot tell who did what, and you cannot remove one person without changing everyone's key.