Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

Linux User Management: useradd, passwd and sudo Access

Let's start. You already know how to make a user and log in with a key. In this chapter we follow one user, ravi, through his whole life on the server. We create him, give him a password, switch to him, give him sudo, take it away again, and finally delete him. On the way you'll see why sudo sometimes keeps working after you take it away, what each part of /etc/passwd means, and why deleting a user can leave files behind. Keep your server running and type every command with me.

In this class

  • Why a normal user can't simply use sudo, and what that protects
  • What a normal user can and can't see without sudo
  • Creating a user with useradd and checking what it made
  • Setting and resetting passwords with passwd, and the BAD PASSWORD warnings
  • Switching users with su and su -, and getting back with exit
  • The sudo lecture and "ravi is not in the sudoers file"
  • Where ec2-user's sudo comes from on Amazon Linux 2023
  • Giving sudo with the wheel group, and taking it away with gpasswd -d
  • Why group changes only count from the next login
  • Reading /etc/passwd: the seven fields and the UIDs
  • Deleting users: userdel vs userdel -r, and cleaning up leftovers
  • Tasks to try at home

1. Where we are

Why this chapter. In Linux Users, Groups and sudo for Beginners you made ravi with sudo adduser, saw that he gets a group with the same name, and logged in as him with an SSH key. In chmod in depth you saw that ec2-user can use sudo and ravi can't. This chapter goes through the full life of a user, step by step, as an admin does it on a real server.

The story. ec2-user is the server owner. The team lead keeps that login. Developers get normal users like ravi and ramesh, with no sudo at first.

Classroom line

He's the owner of the server; he can do anything.

The life of a Linux user, in six commands 1. Create sudo useradd ravi user, group, home ec2-user (the admin) makes the user. Amazon Linux also makesgroup ravi and /home/ravi (700). 2. Password sudo passwd ravi typing stays hidden Set or reset the password. The admin never needs the old one. 3. Switch su - ravi back with exit Become ravi. su - gives a full login in /home/ravi. Leave withexit or Ctrl+D. 4. Give sudo sudo usermod -aG wheel ravi works from the next login Any sudo user can add ravi to wheel. ravi then types his OWNpassword for sudo. 5. Take sudo sudo gpasswd -d ravi wheel old shells keep it Remove ravi from wheel. Shells that started earlier keep theold groups until they exit. 6. Delete sudo userdel -r ravi -r removes the home too Delete the user. Without -r, /home/ravi and the mailbox staybehind.

Figure 1. Animation: the six steps in a user's life. Create, set a password, switch, give sudo, take sudo away, delete.

All commands in this chapter were run on a test copy of Amazon Linux 2023 with the same sudo, login.defs and PAM settings as an EC2 server. Your hostname, dates and process numbers will differ.

2. Why a normal user can't just use sudo

The obvious question here is:

Classroom line

Sir, if a normal user can just use sudo, what's the point of having users?

Exactly. sudo means "do this as root", and root can do anything. If every user could use it, separate users would protect nothing. So on Amazon Linux:

  1. ec2-user can use sudo from the start.
  2. A new user like ravi can't, until an admin allows it.
  3. Each user works in their own home, and the others can't look inside.

How it looks. As ravi, try to make a file with sudo. The first time, sudo shows a short lecture, asks for ravi's own password, and then refuses:

[ravi@ip-172-31-xx-xx ~]$ sudo touch sudotest.txt

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

For security reasons, the password you type will not be visible.

[sudo] password for ravi:
ravi is not in the sudoers file.

Notice the message: it is not "Permission denied". It says ravi has no sudo rule at all. The lecture appears only the first time each user runs sudo.

Classroom line

We should respect other people's privacy.

On some older servers sudo adds a second line, "This incident will be reported." The sudo on Amazon Linux 2023 (version 1.9.15) prints only ravi is not in the sudoers file. Either way the meaning is the same: no sudo rule matches ravi.

3. What a normal user can and can't do

You need to know where the walls are before you start giving rights away.

/ and /home are open for everyone to enter and list (r-x). Homes are 700, so only the owner gets in. /etc/passwd is readable by all; /etc/shadow is not.

What ravi can and can't do without sudo Allowed Blocked cd / ; ls /look around the tree cd /home ; ls /homesee who has a home cat /etc/passwdread the user list touch ~/a.txtwork in /home/ravi ls /home/rameshanother home is 700 cat /etc/shadowpassword hashes, root only touch /home/x.txtwrite outside his home sudo useradd ...not in the sudoers file / and /home are r-x for everyone, so anyone may enter andlist them. Writing there, or entering a 700 home, is whatfails.

Figure 2. Without sudo, ravi can look around / and /home and read /etc/passwd, but he can't enter another home, read /etc/shadow, write outside his home or create users.

Example 1: looking around works. Run these as ravi:

[ravi@ip-172-31-xx-xx ~]$ cd /
[ravi@ip-172-31-xx-xx /]$ ls
bin  boot  dev  etc  home  lib  lib64  local  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[ravi@ip-172-31-xx-xx /]$ ls -ld /home /home/ec2-user /home/ravi
drwxr-xr-x. 4 root     root      35 Jan  5 10:15 /home
drwx------. 2 ec2-user ec2-user  62 Jan  5 10:15 /home/ec2-user
drwx------. 2 ravi     ravi      62 Jan  5 10:15 /home/ravi

Example 2: the walls. Still as ravi:

[ravi@ip-172-31-xx-xx ~]$ ls /home/ec2-user
ls: cannot open directory '/home/ec2-user': Permission denied
[ravi@ip-172-31-xx-xx ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[ravi@ip-172-31-xx-xx ~]$ useradd test1
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.

Classroom line

While you're ravi, you can't create a new user.

Handwritten board: lines starting with ~] for sudo useradd ravi and sudo passwd ravi, then ~]su ravi, then ec2-user] cd, then ravi] sudo useradd ravi with a cross after it; on the right /home/ravi with arrows to one, a.txt and two inside a large loop.

Whiteboard: sudo useradd ravi and sudo passwd ravi as ec2-user, then su ravi; the prompt still shows ec2-user's folder until cd. As ravi, sudo useradd is crossed out. On the right, /home/ravi with the folder one, a.txt and two inside.

Everyone can enter and list / and /home, because they are r-x for others. What a normal user can't do is write there, or enter another user's 700 home.

4. Step 1: create the user with useradd

A user is more than a folder. Linux needs an entry in /etc/passwd, a group, a home and a password entry. useradd makes all of them in one go. Never "create a user" by making a folder in /home.

On Amazon Linux 2023, useradd and adduser are the same program. One command creates:

  1. The user ravi, with the next free number (UID), here 1001.
  2. A group ravi with the same number.
  3. The home /home/ravi, with mode 700.
  4. A mailbox file /var/spool/mail/ravi.
  5. A locked password entry (!!), so nobody can log in with a password yet.

Example 3: create and check. As ec2-user:

[ec2-user@ip-172-31-xx-xx ~]$ sudo useradd ravi
[ec2-user@ip-172-31-xx-xx ~]$ id ravi
uid=1001(ravi) gid=1001(ravi) groups=1001(ravi)
[ec2-user@ip-172-31-xx-xx ~]$ grep ravi /etc/passwd
ravi:x:1001:1001::/home/ravi:/bin/bash
[ec2-user@ip-172-31-xx-xx ~]$ sudo grep ravi /etc/shadow | cut -d: -f1,2
ravi:!!

useradd prints nothing when it works. Silence means success.

Handwritten board: sudo useradd ravi, sudo passwd ravi, and lower down su ravi.

Whiteboard: The three commands, sudo useradd ravi, sudo passwd ravi, then su ravi.

5. Step 2: set the password with passwd

A new user's password is locked. Until you set one, su ravi can't work:

[ec2-user@ip-172-31-xx-xx ~]$ su ravi
Password:
su: Authentication failure

Classroom line

Did we set a password? No.

Set it as the admin, with sudo:

[ec2-user@ip-172-31-xx-xx ~]$ sudo passwd ravi
Changing password for user ravi.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

While you type, nothing appears: no stars, and the cursor doesn't move. That's on purpose, so nobody looking at your screen can count the letters.

Classroom line

The cursor won't move; that's why you see no stars and the cursor doesn't move.

Example 4: weak passwords. Linux checks the new password and warns you. Here are the real messages:

BAD PASSWORD: The password is shorter than 8 characters
BAD PASSWORD: The password contains the user name in some form
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word

When root (you, with sudo) sets the password, this is only a warning: retype it and it is saved. When ravi changes his own password with plain passwd, the same checks are enforced. After three weak tries he gets:

passwd: Have exhausted maximum number of retries for service

If the two entries don't match, you see Sorry, passwords do not match. and get asked again.

Example 5: someone forgot their password. The admin resets it. The old password isn't needed:

sudo passwd ravi

Classroom line

Now, some people forgot their password.

Example 6: what ravi may do with passwd. ravi can change only his own password, and must type the current one first. He can't touch anyone else's:

[ravi@ip-172-31-xx-xx ~]$ passwd ramesh
passwd: Only root can specify a user name.

Watch the order of the words. su passwd ravi runs su and asks it to switch to a user called passwd:

su passwd ravi
su: user passwd does not exist or the user entry does not contain all the required fields

The right command is sudo passwd ravi.

6. Step 3: switch users with su and su -

Check the user, home and working directory after switching

The current user, the current folder and the preferred home directory are separate. Record whoami, pwd and printenv HOME in your original shell. Start a login-style session with su - ravi on the lab server, then run the same three checks. Expect Ravi as the user and normally Ravi’s home as the directory. Run exit to return to your original shell.

A plain su ravi can keep the previous working directory. If a command fails after switching, check the current location as well as the username. Ravi may be unable to traverse a directory owned by the previous account even though the switch itself succeeded.

For practice we play several people on one server, so we switch users inside it. On a real team, everyone logs in directly with their own key (ssh -i key.pem ravi@SERVER_PUBLIC_IP, after their public key is in /home/ravi/.ssh/authorized_keys, as shown in the users chapter). su is the practice shortcut.

su = "switch user". It asks for the password of the user you switch to.

  1. su ravi: you become ravi, but you stay in the folder you were in.
  2. su - ravi: a full login as ravi. You land in /home/ravi with a fresh environment.

Example 7: su ravi keeps you in the old folder.

[ec2-user@ip-172-31-xx-xx ~]$ su ravi
Password:
[ravi@ip-172-31-xx-xx ec2-user]$ pwd
/home/ec2-user
[ravi@ip-172-31-xx-xx ec2-user]$ ls
ls: cannot open directory '.': Permission denied
[ravi@ip-172-31-xx-xx ec2-user]$ cd
[ravi@ip-172-31-xx-xx ~]$ pwd
/home/ravi
[ravi@ip-172-31-xx-xx ~]$ touch a.txt
[ravi@ip-172-31-xx-xx ~]$ mkdir myfolder

Read the prompt: ravi is the user, ip-172-31-xx-xx the server, ec2-user the folder you're in, and $ means a normal user (# would mean root). ls failed because /home/ec2-user is 700 and belongs to ec2-user. A plain cd takes ravi home, where he can work.

Classroom line

But ravi has no permission in that folder.

Example 8: su - ravi gives a full login.

[ec2-user@ip-172-31-xx-xx ~]$ su - ravi
Password:
Last login: Mon Jan  5 10:15:37 UTC 2026 on pts/1
[ravi@ip-172-31-xx-xx ~]$ pwd
/home/ravi
su ravi vs su - ravi su raviswitch the user only $ su raviPassword:[ravi@ip-172-31-xx-xx ec2-user]$ pwd/home/ec2-user$ lsls: cannot open directory '.'Permission denied You stay in the old folder, which is not yours. su - ravia full login as ravi $ su - raviPassword:Last login: ...[ravi@ip-172-31-xx-xx ~]$ pwd/home/ravi$ echo $HOME/home/ravi Fresh environment, starts in /home/ravi. Going back: exit or Ctrl+D. Ctrl+C only stops arunning command; you stay ravi.

Figure 3. su ravi switches the user but leaves you in /home/ec2-user. su - ravi is a full login that starts in /home/ravi.

Example 9: going back. Type exit, or press Ctrl+D. Both end ravi's shell and you are ec2-user again:

[ravi@ip-172-31-xx-xx ~]$ exit
logout
[ec2-user@ip-172-31-xx-xx ~]$ whoami
ec2-user

After su ravi (without the dash) the word printed is exit instead of logout. Same result.

Ctrl+C does not log you out. It only stops a command that is running, and you stay ravi:

[ravi@ip-172-31-xx-xx ~]$ sleep 30
^C
[ravi@ip-172-31-xx-xx ~]$ whoami
ravi

Use exit or Ctrl+D to leave a user's shell.

Ravindra Bagale's Tip

Prefer su - ravi when you test things as another user. You then see exactly what that user sees when they log in, including their home folder and their settings.

7. Users are isolated: add ramesh

Each person's files are private by default. Let's prove it with a second user.

Example 10: second user, next UID.

[ec2-user@ip-172-31-xx-xx ~]$ sudo useradd ramesh
[ec2-user@ip-172-31-xx-xx ~]$ sudo passwd ramesh
[ec2-user@ip-172-31-xx-xx ~]$ tail -3 /etc/passwd
ec2-user:x:1000:1000:EC2 Default User:/home/ec2-user:/bin/bash
ravi:x:1001:1001::/home/ravi:/bin/bash
ramesh:x:1002:1002::/home/ramesh:/bin/bash

Example 11: ramesh can't look into ravi's home.

[ec2-user@ip-172-31-xx-xx ~]$ su - ramesh
Password:
[ramesh@ip-172-31-xx-xx ~]$ cd /home/ravi
-bash: cd: /home/ravi: Permission denied
[ramesh@ip-172-31-xx-xx ~]$ ls /home/ravi
ls: cannot open directory '/home/ravi': Permission denied
[ramesh@ip-172-31-xx-xx ~]$ exit

Classroom line

Can't go in, permission denied. Every user's files are separate and isolated.

8. Reading /etc/passwd

Every user you made is one line in /etc/passwd. Reading it tells you the user's number, main group, home and shell.

The line has seven fields separated by :

ravi:x:1001:1001::/home/ravi:/bin/bash
  1. ravi: the user name.
  2. x: the password is not here; it is kept, as a hash, in /etc/shadow.
  3. 1001: the UID, the user's number. Linux really works with this number.
  4. 1001: the GID, the number of the main group (ravi).
  5. (empty): a comment, like a full name. ec2-user's says EC2 Default User.
  6. /home/ravi: the home folder.
  7. /bin/bash: the shell started at login.
One line of /etc/passwd, field by field ravi:x:1001:1001::/home/ravi:/bin/bash ravi 1. user name x 2. password is in /etc/shadow 1001 3. UID 1001 4. GID (main group) (empty) 5. comment (empty) /home/ravi 6. home folder /bin/bash 7. login shell UIDs on Amazon Linux 2023 0 root 1-999 system accounts 1000 ec2-user 1001 ravi 1002 ramesh

Figure 4. The seven fields of one /etc/passwd line, and the UIDs on Amazon Linux 2023: root 0, system accounts below 1000, ec2-user 1000, then 1001, 1002 and so on.

UIDs on Amazon Linux 2023. root is 0. Accounts for system services are below 1000. ec2-user is 1000, and each new user gets the next number: ravi 1001, ramesh 1002.

Example 12: just read it, no sudo needed.

[ec2-user@ip-172-31-xx-xx ~]$ ls -l /etc/passwd /etc/shadow
-rw-r--r--. 1 root root 1836 Jan  5 10:15 /etc/passwd
----------. 1 root root  998 Jan  5 10:15 /etc/shadow
[ec2-user@ip-172-31-xx-xx ~]$ grep ramesh /etc/passwd
ramesh:x:1002:1002::/home/ramesh:/bin/bash

/etc/passwd is readable by everyone. /etc/shadow has no permission bits at all, so only root can read it.

sudo nano passwd from the home folder opens a different file: a path without / means "in the current folder", so nano opens an empty [ New File ]. If you save it, you get a root-owned file ~/passwd that has nothing to do with users. The real file is /etc/passwd. Read it with cat /etc/passwd (no sudo needed) and don't edit it by hand: use useradd, usermod and userdel, which also keep /etc/shadow and /etc/group in step.

9. Where ec2-user's sudo comes from

Before giving sudo to ravi, look at how ec2-user got it.

Two rules on an Amazon Linux 2023 server can give sudo:

  1. In /etc/sudoers: %wheel ALL=(ALL) ALL. Every member of the group wheel may run anything as root, after typing their own password.
  2. In /etc/sudoers.d/90-cloud-init-users, written by cloud-init at first boot: ec2-user ALL=(ALL) NOPASSWD:ALL. ec2-user may run anything with no password.

Example 13: see both rules. As ec2-user:

[ec2-user@ip-172-31-xx-xx ~]$ id
uid=1000(ec2-user) gid=1000(ec2-user) groups=1000(ec2-user),4(adm),10(wheel),190(systemd-journal)
[ec2-user@ip-172-31-xx-xx ~]$ sudo -l
...
User ec2-user may run the following commands on ip-172-31-xx-xx:
    (ALL) ALL
    (ALL) NOPASSWD: ALL
[ec2-user@ip-172-31-xx-xx ~]$ sudo passwd -S ec2-user
ec2-user LK 2026-01-05 0 99999 7 -1 (Password locked.)

ec2-user is in wheel, but its password is locked (you log in with a key). So the wheel rule alone would ask for a password it doesn't have. The NOPASSWD rule from cloud-init is what lets ec2-user run sudo without being asked. We tested both on the box:

  1. ec2-user taken out of wheel, cloud-init rule kept: sudo whoami still prints root.
  2. ec2-user in wheel, cloud-init rule removed: sudo asks for a password, and after three tries says sudo: 3 incorrect password attempts.
Who may use sudo on Amazon Linux 2023 /etc/sudoers %wheel ALL=(ALL) ALL members of wheel, after typing their OWN password /etc/sudoers.d/90-cloud-init-users ec2-user ALL=(ALL) NOPASSWD:ALL ec2-user, with no password asked ec2-user in wheel and has the NOPASSWD rule sudo works, no password asked. Its own password is locked:the NOPASSWD rule does it. ravi in wheel matches %wheel sudo asks [sudo] password for ravi, then works. ravi, not in wheel no rule matches ravi is not in the sudoers file.

Figure 5. The two sudo rules on Amazon Linux 2023, and what happens for ec2-user, for ravi in wheel, and for ravi outside wheel.

Classroom line

Only ec2-user can use sudo; others can if you add them to wheel.

Check any user without becoming them:

[ec2-user@ip-172-31-xx-xx ~]$ sudo -l -U ravi
User ravi is not allowed to run sudo on ip-172-31-xx-xx.

10. Step 4: give sudo by adding to wheel

Sometimes a developer really needs root work, like installing a package with sudo yum install -y tree or restarting a service with sudo service nginx restart. Instead of sharing ec2-user, add that person to wheel.

Any user who already has sudo can do it. As ec2-user:

[ec2-user@ip-172-31-xx-xx ~]$ sudo usermod -aG wheel ravi
[ec2-user@ip-172-31-xx-xx ~]$ groups ravi
ravi : ravi wheel
[ec2-user@ip-172-31-xx-xx ~]$ sudo -l -U ravi
...
User ravi may run the following commands on ip-172-31-xx-xx:
    (ALL) ALL
  1. usermod: change an existing user.
  2. -G wheel: the extra group to set.
  3. -a: append, keep the groups ravi already has. Without -a, -G replaces his extra groups with only the ones you list.

Classroom line

Let's add ravi to the wheel group and see if he can use sudo.

Example 14: ravi uses sudo in a new login. He types his own password, not root's and not ec2-user's:

[ec2-user@ip-172-31-xx-xx ~]$ su - ravi
Password:
[ravi@ip-172-31-xx-xx ~]$ id
uid=1001(ravi) gid=1001(ravi) groups=1001(ravi),10(wheel)
[ravi@ip-172-31-xx-xx ~]$ sudo touch /home/suudowala.txt
[sudo] password for ravi:
[ravi@ip-172-31-xx-xx ~]$ ls -l /home/suudowala.txt
-rw-r--r--. 1 root root 0 Jan  5 10:15 /home/suudowala.txt

The file belongs to root, because sudo ran touch as root. For about five minutes after a correct password, sudo doesn't ask again in that terminal.

Example 15: a shell opened before the change doesn't get it. If ravi was already logged in when you ran usermod, that old shell still fails:

[ravi@ip-172-31-xx-xx ~]$ id
uid=1001(ravi) gid=1001(ravi) groups=1001(ravi)
[ravi@ip-172-31-xx-xx ~]$ sudo whoami
[sudo] password for ravi:
ravi is not in the sudoers file.

He has to exit and log in again. Section 12 explains why.

Any user who already has sudo can add someone to wheel, for example ec2-user with sudo usermod -aG wheel ravi. And a wheel member types their own password at the [sudo] password for ravi: prompt.

11. Step 5: take sudo away with gpasswd -d

Rights should last only as long as the job. When the developer finishes the install, or leaves the team, admin power must go, or anyone who gets that login gets root.

As ec2-user, remove ravi from wheel:

[ec2-user@ip-172-31-xx-xx ~]$ sudo gpasswd -d ravi wheel
Removing user ravi from group wheel
[ec2-user@ip-172-31-xx-xx ~]$ groups ravi
ravi : ravi

gpasswd -d = delete this user from this group. His other groups stay as they are.

Example 16: the surprise. In the shell ravi opened while he was still in wheel, sudo keeps working:

[ravi@ip-172-31-xx-xx ~]$ id
uid=1001(ravi) gid=1001(ravi) groups=1001(ravi),10(wheel)
[ravi@ip-172-31-xx-xx ~]$ sudo whoami
root

Example 17: a fresh login loses it.

[ravi@ip-172-31-xx-xx ~]$ exit
logout
[ec2-user@ip-172-31-xx-xx ~]$ su - ravi
Password:
[ravi@ip-172-31-xx-xx ~]$ sudo whoami
[sudo] password for ravi:
ravi is not in the sudoers file.

12. Why sudo kept working: groups are fixed at login

Verify offboarding from a new session

After changing Ravi’s groups, check two views. As the administrator, id ravi looks up the account’s configured groups. In Ravi’s existing shell, plain id prints the credentials of that running session. If they differ, the old process has kept the groups it got at login.

Open a new Ravi session and compare id again. sudo -l lists what the sudo policy permits for that account. Group membership is one input to that policy. Another rule could still grant access. If this is a company departure, follow the team’s offboarding process for existing sessions, SSH keys and ownership of working files before deleting the account.

It looks like a bug, but it's how Linux works, and you need to know it when you take rights away from someone.

When you log in, your shell gets a list of groups from /etc/group, and keeps that list until it ends. id with no name shows the list of your current shell. groups ravi and id ravi read the files on disk. sudo checks the groups of the shell that runs it.

Two explanations were possible, so we tested both on the box:

  1. The sudo password cache. After a correct password, sudo doesn't ask again for about five minutes. In the old shell we cleared it with sudo -k and ran sudo whoami again. It asked for ravi's password and still printed root. So the cache is not the reason; it only skips the password question.
  2. The shell's own group list. The old shell's id still showed 10(wheel) after gpasswd -d. A fresh su - ravi showed only 1001(ravi), and sudo failed there. This is the real reason.

It works the same way when you add a group: the shell from Example 15 didn't get wheel until a new login.

Group changes count from the next login Each step below runs as ec2-user, or in ravi's shells: $ sudo usermod -aG wheel ravi On disk (groups ravi): ravi, wheel · /etc/group is read at login Shell A (opened before the change) groups this shell got at login: ravi $ sudo whoami ravi is not in the sudoers file. A shell opened before the change still has only ravi: sudo fails. Shell B (a new login) $ su - ravi groups this shell got at login: ravi, wheel $ sudo whoami root A new login picks up wheel: sudo works after ravi types his ownpassword. $ sudo gpasswd -d ravi wheel wheel removed from /etc/group Shell B (still open) $ sudo -k; sudo whoami root Removed on disk, but shell B still carries wheel. Even aftersudo -k it works. Shell C (a fresh login) $ exit ; su - ravi $ sudo whoami ravi is not in the sudoers file. Only a fresh login drops wheel. Now sudo is really gone.

Figure 6. Animation: group changes count from the next login. An old shell keeps the groups it started with, so sudo keeps working after gpasswd -d until ravi logs in again.

Why sudo touch hh.txt still works right after gpasswd -d: group changes apply to new login sessions, and ravi's running shell still has wheel in its group list. Clearing the sudo cache with sudo -k doesn't stop it; only a fresh login does.

Ravindra Bagale's Tip

When you take sudo away from someone, also end their open sessions, for example with sudo pkill -KILL -u ravi, or they can keep using it until they log out. Check with who (from the users chapter) that they're gone.

13. Step 6: delete the user with userdel

When someone leaves the team, remove their login. The question is what happens to their files.

Classroom line

When we delete a user, its files should be deleted too, right?

Two ways:

  1. sudo userdel ravi: removes the user, but keeps /home/ravi and his mailbox /var/spool/mail/ravi.
  2. sudo userdel -r ravi: removes the user and his home and mailbox.

Classroom line

User ravi gets deleted, but the ravi folder doesn't.

Classroom line

Because data is always important.

Example 18: ravi is still logged in. userdel refuses while the user has running processes:

[ec2-user@ip-172-31-xx-xx ~]$ sudo userdel ravi
userdel: user ravi is currently used by process 2412

Ask him to log out (or end his sessions, as in the tip above), then try again.

Example 19: plain userdel keeps the home.

[ec2-user@ip-172-31-xx-xx ~]$ sudo userdel ravi
[ec2-user@ip-172-31-xx-xx ~]$ su ravi
su: user ravi does not exist or the user entry does not contain all the required fields
[ec2-user@ip-172-31-xx-xx ~]$ ls -l /home
total 0
drwx------. 2 ec2-user ec2-user 62 Jan  5 10:15 ec2-user
drwx------. 2 ramesh   ramesh   62 Jan  5 10:15 ramesh
drwx------. 3     1001     1001 78 Jan  5 10:15 ravi
-rw-r--r--. 1 root     root      0 Jan  5 10:15 suudowala.txt

The folder is still there, and the owner now shows as the bare number 1001, because no user has that number any more.

Example 20: -r must be used instead of userdel, not after it. Once the user is gone, -r has nothing to work with:

[ec2-user@ip-172-31-xx-xx ~]$ sudo userdel -r ravi
userdel: user 'ravi' does not exist

Example 21: the right way, in one step.

[ec2-user@ip-172-31-xx-xx ~]$ sudo userdel -r ramesh
[ec2-user@ip-172-31-xx-xx ~]$ ls /home
ec2-user  ravi  suudowala.txt
[ec2-user@ip-172-31-xx-xx ~]$ grep ramesh /etc/passwd
[ec2-user@ip-172-31-xx-xx ~]$

ramesh's line, home and mailbox are gone. (ravi is still the leftover from Example 19.)

userdel vs userdel -r sudo userdel ravi sudo userdel -r ravi ravi in /etc/passwd /home/ravi stays, now owned by 1001 /var/spool/mail/ravi stays ravi in /etc/passwd /home/ravi /var/spool/mail/ravi Kept on purpose: data is important. Use -r instead of plain userdel, not after it. Why clean up leftoversThe next new user can get UID 1001 again andwould then own the old /home/ravi.

Figure 7. userdel keeps /home/ravi and the mailbox, now owned by the number 1001. userdel -r removes the user, the home and the mailbox.

Why leftovers matter. Look at what can happen next. ramesh is gone and the leftover /home/ravi still belongs to 1001. Now create ramesh again. He gets UID 1001, the number ravi had, so the old folder is now his:

[ec2-user@ip-172-31-xx-xx ~]$ sudo useradd ramesh
[ec2-user@ip-172-31-xx-xx ~]$ id ramesh
uid=1001(ramesh) gid=1001(ramesh) groups=1001(ramesh)
[ec2-user@ip-172-31-xx-xx ~]$ ls -ld /home/ravi
drwx------. 2 ramesh ramesh 62 Jan  5 10:15 /home/ravi

Files belong to numbers, not names. useradd gives the next number after the highest one in use, so a number can come back when the newest users are deleted.

Example 22: cleaning up leftovers. If you already used plain userdel, the old UID still owns the folder and mailbox. That UID may already belong to a different account. On this disposable lab machine, first verify that the paths below are just the sample files and that you have any backup you intend to keep. Read the path twice before running the cleanup:

sudo rm -rf /home/ravi
sudo rm -f /var/spool/mail/ravi

Plain userdel keeps the home folder. userdel -r deletes the home folder and mailbox too. Use it instead of userdel, not after it: the second command fails with userdel: user 'ravi' does not exist.

Ravindra Bagale's Tip

In a company, first copy what you need from the person's home (code, notes, logs), then delete with userdel -r. Keeping a leftover home "just in case" means it will one day belong to someone else's UID.

14. The whole lifecycle on one page

Every command from this chapter, in order. All of them run as ec2-user (or another sudo user):

Step Command What it does
Create sudo useradd ravi user, group ravi, /home/ravi (700), mailbox
Password sudo passwd ravi set or reset; no old password needed
Switch su - ravi full login as ravi; back with exit or Ctrl+D
Check id ravi, groups ravi, sudo -l -U ravi groups and sudo rights on disk
Give sudo sudo usermod -aG wheel ravi counts from ravi's next login
Take sudo sudo gpasswd -d ravi wheel old shells keep it until they exit
Delete sudo userdel -r ravi user, home and mailbox

15. Hands-on lab: the full life of a Linux user

On a team server you will create users, give and take sudo, and delete users when people leave. This lab runs that whole lifecycle once, as ec2-user, with a check after every step.

  1. Create the user
    • Command: sudo useradd ravi
    • You should see: Nothing.
    • Check: id ravi → uid=1001(ravi) gid=1001(ravi) groups=1001(ravi); ls -ld /home/ravi starts with drwx------.
    • Why: useradd writes the user into /etc/passwd, makes the group and a private home (700). Only root may do this, hence sudo.
  2. Set a password
    • Command: sudo passwd ravi
    • You should see: New password:, Retype new password:, then passwd: all authentication tokens updated successfully.
    • Check: Nothing appears while you type: that is normal.
    • Why: A new user's password is locked. Without one, su ravi can't work.
  3. Switch to the user
    • Command: su - ravi
    • You should see: Password: then a prompt starting with [ravi@.
    • Check: pwd → /home/ravi
    • Why: - gives a full login: Ravi's home and settings. Plain su ravi would leave you in ec2-user's folder.
  4. Try sudo as ravi
    • Command: sudo whoami
    • You should see: After Ravi's password: ravi is not in the sudoers file.
    • Check: ls /home/ec2-user → Permission denied.
    • Why: A normal user gets no admin rights and can't see other homes. That is the point of separate users.
  5. Go back and give sudo
    • Do: Type exit to go back to ec2-user, then run the command.
    • Command: sudo usermod -aG wheel ravi
    • You should see: Nothing.
    • Check: groups ravi → ravi : ravi wheel
    • Why: On Amazon Linux, members of wheel may use sudo. -a adds without removing Ravi's other groups.
  6. Log in again and use sudo
    • Command: su - ravi
    • You should see: Ravi's prompt.
    • Check: sudo whoami → after Ravi's password, root.
    • Why: Groups are read at login. A new login picks up wheel; a shell opened before the change would still be refused.
  7. Take sudo away
    • Do: Type exit to go back to ec2-user, then run the command.
    • Command: sudo gpasswd -d ravi wheel
    • You should see: Removing user ravi from group wheel
    • Check: groups ravi → ravi : ravi
    • Why: Remove admin rights as soon as the job is done. Old shells keep wheel until they log out, so ask the user to log out too.
  8. Delete the user and the home
    • Command: sudo userdel -r ravi
    • You should see: Nothing (a note about the mail spool can appear; it is harmless).
    • Check: id ravi → id: 'ravi': no such user; ls /home → ec2-user.
    • Why: -r removes the home too. Without it, /home/ravi stays, owned by a bare number, and the next new user could get that number and Ravi's files.
  9. Read /etc/passwd with cat (do not nano it)
    • Command: cat /etc/passwd | tail -n 5
    • You should see: lines like ravi:x:1001:1001::/home/ravi:/bin/bash (UID/GID/home/shell). Never open this file in nano to “edit casually”.
    • Check: Fields are colon-separated; password hash lives in /etc/shadow (root-only).
    • Why: Class insisted on cat for reading — accidental edits to account files break logins.
  10. su to another user: wrong password / permission messages
    • Do: From ec2-user, su ravi (or su priya if you created that name). Enter a wrong password once.
    • You should see: authentication failure / su denied style message (wording varies). Correct password → that user’s prompt.
    • Check: su - loads the target user’s full login environment; plain su keeps more of your old environment.
    • Why: Switching users proves isolation before you grant wheel/sudo.
  11. Normal user cannot sudo useradd
    • Do: As ravi (not in wheel): sudo useradd demo2
    • You should see: refusal text such as ravi is not in the sudoers file or a password prompt that still fails — not a new account.
    • Check: Only wheel/sudoers members may escalate. Confirm with groups ravi.
    • Why: Interview question — “if every user had sudo, why have users?” Default is only ec2-user.
  12. After wheel: sudo touch proves privilege
    • Do: As ec2-user, add ravi to wheel (as in the main lab). Then as ravi: sudo touch /tmp/wheel-ok.txt && ls -l /tmp/wheel-ok.txt
    • You should see: file owned by root root.
    • Check: Same user who was denied earlier can now create root-owned files via sudo.
    • Why: Closes the lifecycle: create → password → su → deny sudo → grant wheel → prove → remove → userdel.

16. Try at home

Try at home

Task 1: create and switch

  1. sudo useradd ravi, then id ravi and ls -ld /home/ravi.
  2. Try su ravi before setting a password, then set one with sudo passwd ravi.
  3. Compare su ravi + pwd with su - ravi + pwd. Leave each with exit.

Task 2: walls

  1. As ravi: ls /, ls /home, cat /etc/passwd, then ls /home/ec2-user and cat /etc/shadow.
  2. Make ramesh and check his line in /etc/passwd. What is his UID?

Task 3: give and take sudo

  1. Open a su - ravi shell and keep it open. In another terminal, run sudo usermod -aG wheel ravi.
  2. In ravi's old shell, run id and sudo whoami. Then open a new su - ravi and try again.
  3. Run sudo gpasswd -d ravi wheel. Test the open shell (also after sudo -k) and a fresh login. Explain the results.

Task 4: delete

  1. sudo userdel ravi, then ls -l /home. Who owns /home/ravi now?
  2. Clean it up with sudo rm -rf /home/ravi and sudo rm -f /var/spool/mail/ravi.
  3. Delete ramesh the right way with sudo userdel -r ramesh.

When you're done, check ls /home shows only ec2-user, and stop the instance.

Common errors and fixes

Error / symptomFix
useradd: Permission deniedRun with sudo as a privileged user.
su: Authentication failureSet/reset with sudo passwd username; check Caps Lock.
Not in sudoersAs admin: add to wheel (usermod -aG wheel name on Amazon Linux) then re-login.
Home left behind after deleteUse userdel -r to remove home, or delete the folder carefully yourself.
Edited /etc/passwd in nano and broke loginRestore from backup / use vipw only if you know it — prefer useradd/usermod.

FAQ

Q. useradd vs adduser?

On Amazon Linux, adduser is often a friendly wrapper; useradd is the low-level tool. Class used both ideas — pick one style and be consistent.

Q. Why su -?

Dash form simulates a full login (home, PATH). Better when testing “what does ravi actually see?”

Q. Is wheel the same everywhere?

Name differs (sudo group on Ubuntu). Idea is the same: membership in an admin group.

Q. Can ravi SSH with the .pem?

Only if you put an authorized key in that user’s ~/.ssh. Password SSH may be disabled by AMI defaults — use su from ec2-user for labs.

Recap

In short

  1. Users keep people apart: own home (700), own files, no sudo by default.
  2. Everyone may enter and list / and /home; writing there, or entering another home, needs rights.
  3. sudo useradd ravi makes the user, a same-name group, /home/ravi and a mailbox, with a locked password.
  4. sudo passwd ravi sets or resets a password. Typing is invisible. Root gets BAD PASSWORD warnings; a user changing their own password is held to them.
  5. su ravi keeps your folder; su - ravi is a full login in /home/ravi. Leave with exit or Ctrl+D, not Ctrl+C.
  6. A user without a rule gets ravi is not in the sudoers file.
  7. ec2-user's sudo works because of the cloud-init NOPASSWD rule; wheel members use sudo with their own password.
  8. Any sudo user can give sudo: sudo usermod -aG wheel ravi. Take it away with sudo gpasswd -d ravi wheel.
  9. Group changes count from the next login. Old shells keep their groups, even after sudo -k.
  10. /etc/passwd: name, x (hash in /etc/shadow), UID, GID, comment, home, shell. ec2-user is 1000, then 1001, 1002.
  11. Read /etc/passwd, don't edit it; sudo nano passwd in your home is a different, new file.
  12. userdel keeps the home and mailbox; userdel -r removes them. Use -r instead of plain userdel, and clean up leftovers, because UIDs can come back.

Classroom line

Our Linux doesn't end here; it ends only when the whole course ends.


Ravindra Bagale, trainer: linkedin.com/in/ravindra-bagale. The user names (ravi, ramesh) and file names are examples for learning. The commands and messages were checked on a test copy of Amazon Linux 2023 (sudo 1.9.15, shadow-utils 4.9, passwd 0.80, libpwquality 1.4.4) with the same sudoers, PAM and login.defs files as an EC2 server, and against an EC2 server's settings read-only. The hostname in the prompt, the dates, sizes and process numbers are illustrative.

Worked example: verify a permission change in a fresh session

A teammate was taken out of the wheel group but still has an old terminal open. You need to tell a stale login from a separate sudo rule.

  1. Check id ravi from your admin shell. It reads the current account record. Have Ravi run id in the old login shell. Compare the two group lists.
  2. Close the old lab session and start a new one. Use sudo -l as Ravi to see what sudo still permits. If access remains, inspect the sudoers configuration and recognised groups as an admin. Removing one group does not remove an explicit user rule.
  3. For a departing employee, also review active sessions, account keys and service ownership. Removing a Linux group membership is one step in revoking access.
Interview question: why can an existing shell still have an old group?

Its processes inherited a supplementary group list when the session started. Editing the account database changes what a newly launched login receives. It does not update the credentials of every running process.

Reference 1 · Reference 2 · Reference 3

Practise this chapter

Interview questions and answers

What’s the difference between id ravi and id run inside Ravi’s existing shell?

The named lookup reports Ravi’s configured identity and groups. With no name, id describes the current process’s credentials. After a group change, an old session and the lookup can disagree until the person signs in again.

Which two things do you check before deleting a departed user who ran a scheduled job?

Check which jobs and files still depend on that identity, then hand off the work and ownership deliberately. Revoke their sign-in methods and handle existing sessions through the approved offboarding process before deleting the test account.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. You removed Ravi from wheel, but id inside his old shell still lists wheel. What should you do to verify the new groups?

Answer and explanation

A. Start a new Ravi session and compare id there

A process can retain the groups it got at login. A fresh session receives the current groups, and sudo -l can verify relevant policy.

2. Which change do you normally expect when switching with su - ravi?

Answer and explanation

C. A Ravi login-style shell starting in Ravi’s home

The dash requests a login-style environment, normally including the target account’s home. Check whoami, pwd and HOME.

3. Ravi is in no administrative group, but you want to see whether a separate sudoers rule allows a command. Which check helps?

Answer and explanation

D. sudo -l as Ravi

sudo -l reports the applicable sudo policy for the invoking user. Group membership is only one way a rule can match.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Bavaria Motors is a automobile business in Munich. Lukas Becker looks after its IT and cloud work. They count money in EUR and run their AWS resources in the eu-central-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 10

Easy Create a team group

Groups let you give the same access to many users at once.

Create a group named bavariamotors-dev.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Switch to another user

Testing as the real user shows what they can and cannot do.

Switch to user lukas with a full login environment.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Protect an SSH key file

SSH refuses to use a key file that other users can read.

Make bavariamotors-key.pem readable only by its owner and nobody else (no write).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Set normal web file permissions

Web files should be readable by everyone but writable only by the owner.

Give index.html the permissions: owner read+write, group read, others read (use numbers).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Set a user's password

A new user cannot log in with a password until one is set.

Set the password for user lukas.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Give a folder to the web server user

If nginx does not own the upload folder, uploads fail with "permission denied".

Make user nginx and group nginx the owners of /var/www/bavariamotors/uploads and everything inside.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Give a user sudo rights

On Amazon Linux, members of the wheel group can use sudo.

Add lukas to the wheel group without removing their other groups.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Fix permissions on a whole folder

Folders need the x bit so the web server can enter them. -R applies it to everything inside.

Set permissions 755 on /var/www/bavariamotors and everything inside it.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Create a user with a home folder

Each person should have their own login, with their own home folder and shell.

Create the user lukas with a home folder and /bin/bash as the login shell.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Permission number

Key files must be private.

What chmod number gives the owner read+write and nobody else anything?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Quick revision cards

Revise this lesson in two minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens its lesson.

Comparison card: su ravi vs su - ravi. su ravi: Switch to user ravi, keep your current setup. su - ravi: Switch to ravi with a full fresh login.
Comparison: su ravi vs su - ravi · Download PNG
Comparison card: userdel vs userdel -r. userdel: Deletes the user account. userdel -r: Deletes the account AND its home folder and mail.
Comparison: userdel vs userdel -r · Download PNG
Revision card: Create a user with useradd. Each person on a server should have their own login. useradd creates the user, a home folder and a group of the same name. passwd sets the password. Create, set a password, check with id. Commands:
Revision card: Create a user with useradd · Download PNG
Revision card: Switch user with su. You need to test what another user can do. su NAME switches user. su - NAME also loads their home and environment. Type their password; exit returns you. Switch:
Revision card: Switch user with su · Download PNG
Revision card: Give sudo with the wheel group. A new user sees "ravi is not in the sudoers file". On Amazon Linux, members of the wheel group may use sudo. Add with usermod -aG; remove with gpasswd -d. It counts from the next login. Give and take:
Revision card: Give sudo with the wheel group · Download PNG
Revision card: Read /etc/passwd fields. You need to see a user's ID, home and shell quickly. 7 fields split by colons: name, x, UID, GID, comment, home, shell. grep the user name; UID 0 is root, 1000+ are normal users. Line:
Revision card: Read /etc/passwd fields · Download PNG
Interview card: A new user gets "not in the sudoers file". How do you fix it?. Add the user to the wheel group (Amazon Linux) or sudo group (Ubuntu). Only members of those groups may run sudo. Then log out and in again:
Interview card: A new user gets "not in the sudoers file". How do you fix it? · Download PNG
Interview card: userdel vs userdel -r: what is left behind?. userdel removes the account but keeps /home/name. -r removes the home and mail too. Leftover files can be owned by a recycled UID later. Clean removal:
Interview card: userdel vs userdel -r: what is left behind? · Download PNG
Interview card: Why do group changes work only after the next login?. Groups are loaded into your session at login. The running shell still has the old group list. Check with id; log out and in, or use newgrp.
Interview card: Why do group changes work only after the next login? · Download PNG

More cards: Ravi Sir's Cards.