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
useraddand checking what it made - Setting and resetting passwords with
passwd, and theBAD PASSWORDwarnings - Switching users with
suandsu -, and getting back withexit - The
sudolecture and "ravi is not in the sudoers file" - Where
ec2-user'ssudocomes from on Amazon Linux 2023 - Giving
sudowith thewheelgroup, and taking it away withgpasswd -d - Why group changes only count from the next login
- Reading
/etc/passwd: the seven fields and the UIDs - Deleting users:
userdelvsuserdel -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.
Class मधलं वाक्य
"Toch malak ahe server cha, to kahi bi karnar."
तोच मालक आहे server चा, तो काहीही करणार.
Classroom line
He's the owner of the server; he can do anything.
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?
Class मधलं वाक्य
सर, normal user असाच sudo वापरून काम करायला लागला, तर users चा अर्थच काय उरला?
Classroom line – हिंदी
"Sir agar aise hi normal user sudo use karke kaam karne laga toh user ka meaning kya reh gaya?"
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:
ec2-usercan usesudofrom the start.- A new user like
ravican't, until an admin allows it. - 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.
Class मधलं वाक्य
"Dusryancha aadar karayla pahije privacy cha."
दुसऱ्यांच्या privacy चा आदर करायला पाहिजे.
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.
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.
Class मधलं वाक्य
"Tumi ravi madhe astana, tumi navin user create karu shakat nahi."
तुम्ही ravi मध्ये असताना, तुम्ही नवीन user create करू शकत नाही.
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.](../../assets/img/aws/linux-user-management-useradd-passwd-su-wheel-userdel-02-su-ravi-cd-and-sudo-useradd-fails.webp)
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:
- The user
ravi, with the next free number (UID), here1001. - A group
raviwith the same number. - The home
/home/ravi, with mode700. - A mailbox file
/var/spool/mail/ravi. - 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.

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.
Class मधलं वाक्य
"Password set kela ka apan? Nahi na."
Password set केला का आपण? नाही ना.
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.
Class मधलं वाक्य
Cursor हलणार नाही... म्हणून ना star दिसतो ना cursor हलतो.
Classroom line – हिंदी
"Cursor move nahi hoga... iske liye na star dikhta hai na cursor move hota hai."
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.
Class मधलं वाक्य
"Ata kahi jananni password visarle."
आता काही जणांनी password विसरले.
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.
su ravi: you becomeravi, but you stay in the folder you were in.su - ravi: a full login asravi. You land in/home/raviwith 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.
Class मधलं वाक्य
"Pan actually tya folder madhe tyala permission nasto ravila."
पण actually त्या folder मध्ये ravi ला permission नसते.
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
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.
Class मधलं वाक्य
"Nahi jau shakat, permission denied. So saglya user che files he separated and isolated astat."
नाही जाऊ शकत, permission denied. म्हणजे सगळ्या users च्या files separate आणि isolated असतात.
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
ravi: the user name.x: the password is not here; it is kept, as a hash, in/etc/shadow.1001: the UID, the user's number. Linux really works with this number.1001: the GID, the number of the main group (ravi).- (empty): a comment, like a full name.
ec2-user's saysEC2 Default User. /home/ravi: the home folder./bin/bash: the shell started at login.
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:
- In
/etc/sudoers:%wheel ALL=(ALL) ALL. Every member of the groupwheelmay run anything as root, after typing their own password. - In
/etc/sudoers.d/90-cloud-init-users, written by cloud-init at first boot:ec2-user ALL=(ALL) NOPASSWD:ALL.ec2-usermay 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:
ec2-usertaken out ofwheel, cloud-init rule kept:sudo whoamistill printsroot.ec2-userinwheel, cloud-init rule removed:sudoasks for a password, and after three tries sayssudo: 3 incorrect password attempts.
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.
Class मधलं वाक्य
"Fakt ec2-user sudo vaparu shakto. Dusre vaparu shaktat jar tyana wheel madhe add kela tar."
फक्त ec2-user sudo वापरू शकतो. दुसरे वापरू शकतात जर त्यांना wheel मध्ये add केलं तर.
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
usermod: change an existing user.-G wheel: the extra group to set.-a: append, keep the groups ravi already has. Without-a,-Greplaces 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.
Class मधलं वाक्य
"Wheel group madhe ravi ya user la add karun baghuya to sudo use karu shakto ka."
Wheel group मध्ये ravi या user ला add करून बघूया तो sudo वापरू शकतो का.
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:
- The sudo password cache. After a correct password,
sudodoesn't ask again for about five minutes. In the old shell we cleared it withsudo -kand ransudo whoamiagain. It asked for ravi's password and still printedroot. So the cache is not the reason; it only skips the password question. - The shell's own group list. The old shell's
idstill showed10(wheel)aftergpasswd -d. A freshsu - ravishowed only1001(ravi), andsudofailed 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.
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?
Class मधलं वाक्य
"Apan user delete kelyavar pan te content delete vhayla pahije na."
आपण user delete केल्यावर पण ते content delete व्हायला पाहिजे ना?
Classroom line
When we delete a user, its files should be deleted too, right?
Two ways:
sudo userdel ravi: removes the user, but keeps/home/raviand his mailbox/var/spool/mail/ravi.sudo userdel -r ravi: removes the user and his home and mailbox.
Classroom line
User ravi gets deleted, but the ravi folder doesn't.
Class मधलं वाक्य
"Ravi ha user delete hoil pan ravi ha folder delete nahi honar."
ravi हा user delete होईल पण ravi हा folder delete नाही होणार.
Classroom line
User ravi gets deleted, but the ravi folder doesn't.
Classroom line
Because data is always important.
Class मधलं वाक्य
"Karan nehmi data mahatvacha asto."
कारण नेहमी data महत्त्वाचा असतो.
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.)
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.
- 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/ravistarts withdrwx------. - Why:
useraddwrites the user into/etc/passwd, makes the group and a private home (700). Only root may do this, hencesudo.
- Command:
- Set a password
- Command:
sudo passwd ravi - You should see:
New password:,Retype new password:, thenpasswd: 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 ravican't work.
- Command:
- 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. Plainsu raviwould leave you in ec2-user's folder.
- Command:
- 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.
- Command:
- Go back and give sudo
- Do: Type
exitto 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
wheelmay usesudo.-aadds without removing Ravi's other groups.
- Do: Type
- 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.
- Command:
- Take sudo away
- Do: Type
exitto 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
wheeluntil they log out, so ask the user to log out too.
- Do: Type
- 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:
-rremoves the home too. Without it,/home/ravistays, owned by a bare number, and the next new user could get that number and Ravi's files.
- Command:
- 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
catfor reading — accidental edits to account files break logins.
- Command:
- su to another user: wrong password / permission messages
- Do: From
ec2-user,su ravi(orsu priyaif 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; plainsukeeps more of your old environment. - Why: Switching users proves isolation before you grant wheel/sudo.
- Do: From
- 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 fileor 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.
- Do: As
- After wheel: sudo touch proves privilege
- Do: As
ec2-user, addravito wheel (as in the main lab). Then asravi: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.
- Do: As
16. Try at home
Try at home
Task 1: create and switch
sudo useradd ravi, thenid raviandls -ld /home/ravi.- Try
su ravibefore setting a password, then set one withsudo passwd ravi. - Compare
su ravi+pwdwithsu - ravi+pwd. Leave each withexit.
Task 2: walls
- As
ravi:ls /,ls /home,cat /etc/passwd, thenls /home/ec2-userandcat /etc/shadow. - Make
rameshand check his line in/etc/passwd. What is his UID?
Task 3: give and take sudo
- Open a
su - ravishell and keep it open. In another terminal, runsudo usermod -aG wheel ravi. - In ravi's old shell, run
idandsudo whoami. Then open a newsu - raviand try again. - Run
sudo gpasswd -d ravi wheel. Test the open shell (also aftersudo -k) and a fresh login. Explain the results.
Task 4: delete
sudo userdel ravi, thenls -l /home. Who owns/home/ravinow?- Clean it up with
sudo rm -rf /home/raviandsudo rm -f /var/spool/mail/ravi. - Delete
rameshthe right way withsudo userdel -r ramesh.
When you're done, check ls /home shows only ec2-user, and stop the instance.
Common errors and fixes
| Error / symptom | Fix |
|---|---|
useradd: Permission denied | Run with sudo as a privileged user. |
su: Authentication failure | Set/reset with sudo passwd username; check Caps Lock. |
| Not in sudoers | As admin: add to wheel (usermod -aG wheel name on Amazon Linux) then re-login. |
| Home left behind after delete | Use userdel -r to remove home, or delete the folder carefully yourself. |
| Edited /etc/passwd in nano and broke login | Restore 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
- Users keep people apart: own home (
700), own files, nosudoby default. - Everyone may enter and list
/and/home; writing there, or entering another home, needs rights. sudo useradd ravimakes the user, a same-name group,/home/raviand a mailbox, with a locked password.sudo passwd ravisets or resets a password. Typing is invisible. Root getsBAD PASSWORDwarnings; a user changing their own password is held to them.su ravikeeps your folder;su - raviis a full login in/home/ravi. Leave withexitor Ctrl+D, not Ctrl+C.- A user without a rule gets
ravi is not in the sudoers file. ec2-user'ssudoworks because of the cloud-initNOPASSWDrule;wheelmembers usesudowith their own password.- Any
sudouser can givesudo:sudo usermod -aG wheel ravi. Take it away withsudo gpasswd -d ravi wheel. - Group changes count from the next login. Old shells keep their groups, even after
sudo -k. /etc/passwd: name,x(hash in/etc/shadow), UID, GID, comment, home, shell.ec2-useris1000, then1001,1002.- Read
/etc/passwd, don't edit it;sudo nano passwdin your home is a different, new file. userdelkeeps the home and mailbox;userdel -rremoves them. Use-rinstead of plainuserdel, 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.
Class मधलं वाक्य
"Linux sampat nahiye apla. Jevha purna course sampat tevha ch linux sampat."
आपलं Linux संपत नाहीये. जेव्हा पूर्ण course संपतो तेव्हाच Linux संपतं.
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.
- Check
id ravifrom your admin shell. It reads the current account record. Have Ravi runidin the old login shell. Compare the two group lists. - Close the old lab session and start a new one. Use
sudo -las 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. - 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.
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.
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 scenarioBavaria 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.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- 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.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- 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).
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- 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).
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- 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.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- 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.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- 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.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- 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.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- 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.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- 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?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- 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.









More cards: Ravi Sir's Cards.