Ravindra BagaleCourses & study guides Track your progress

Guides

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.

  1. 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)
  1. 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
  1. 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
  1. Because she logs in with a key and has no password, allow sudo without a password for wheel, or set a password with sudo passwd priya so sudo can ask for it.
  2. 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?

Add a Linux user safely Create the user, set a password or key, add sudo only if needed, test the login, and lock or remove the user later. Add a Linux user safely Createuseradd -mPassword/keypasswd / .sshGroupsudo / wheelTestnew terminalOffboardlock / userdel you One person, one account. Give sudo only when needed

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?

  1. Choose a username (lowercase, no spaces).
  2. Create the user with a home folder (adduser or useradd -m).
  3. Set a password, or plan for key-only login.
  4. Add groups: sudo/wheel only if they really need admin rights, docker only if they must run containers (it is root-equivalent), www-data or apache to share web files.
  5. Add their SSH public key.
  6. Test the login in a new terminal before closing your own session.
  7. 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

  1. usermod -G sudo without -a. It removes the user from all other groups.
  2. Pasting a private key on the server. Only the public key (.pub) goes in authorized_keys.
  3. Wrong permissions on .ssh. SSH ignores keys if the folder or file is writable by others.
  4. 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

  1. Which group gives sudo rights on Amazon Linux and RHEL?

    1. wheel
    2. sudo
    3. admin
    4. root
    Show answer

    A. RHEL-family systems use the wheel group for sudo.

  2. What does -aG do in usermod -aG sudo priya?

    1. Adds to group, keeps others
    2. Removes all other groups
    3. Makes the user an admin
    4. Creates the home folder
    Show answer

    A. -a appends; without it, other groups are replaced.

  3. Which permissions should authorized_keys have?

    1. 600 owned by the user
    2. 777 owned by root
    3. 644 owned by root
    4. 755 owned by nobody
    Show answer

    A. SSH ignores key files that others can write.

  4. Why not share one ec2-user login among the team?

    1. You cannot tell who did what
    2. It makes SSH much slower
    3. Linux allows only one user
    4. It breaks the sudo command
    Show answer

    A. Separate accounts give accountability and easy removal.

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.