CareerByteCode
ByteBlogby CareerByteCode

Linux Users, Groups and sudo Explained

What you build: a shared Linux server for 3 people on a free VM: users, a developers group, a sudo rule that allows one job only, visudo catching a typo, the sudo log, and an account locked and remove

Linux Users, Groups and sudo Explained

✍️ Translate this blog into Hindi, Tamil or Telugu

In this lab you set up one Linux server for three people, the way a small team really shares a machine: two developers who may restart the web server and nothing else, and an intern who gets no admin rights and is removed when the internship ends. You create users and groups, write a sudo rule in the safe place, watch visudo catch a typo before it can lock you out, read the log of who used sudo, and lock and remove an account.

Who it is for: beginners who log in to Linux servers and want to understand users, groups and sudo instead of handing out full admin rights. Time: about 1 hour. Cost: free. An e2-micro VM in us-east1 is in Google Cloud's Always Free tier (one per account), and we delete it at the end.

Every command below was run for real by the CareerByteCode Team on a fresh Google Cloud project, and every output and screenshot is the real result we got. In the outputs, you stands for your own user name.

🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterβ‚Ή499Build the projects →

Part A: users, groups and sudo in plain English

  • User: an account with a name, a number (the UID) and a home folder. Programs run as a user too: nginx, sshd and root.
  • root: the user with UID 0, the administrator. Root skips every permission check, so nobody should work as root all day.
  • Group: a named list of users (with a number, the GID). Give rights to a group once instead of to each person.
  • sudo: "do as superuser". It runs one command as root (or another user), but only if a rule in the sudoers files allows it, and it logs every use.
  • sudoers rules live in /etc/sudoers and in small files in /etc/sudoers.d/. You check them with visudo, because a broken sudoers file can stop sudo from working for everyone.
Users, groups and sudo in one picture
A person is a user, users sit in groups, a sudo rule gives a group exactly one job as root, and every use is logged

Real-life example: an office. Each employee has an ID card (user), cards belong to a department (group), and the building's rule book (sudoers) says "the IT department may open the server room, nobody else". The guard (sudo) checks the rule book and writes every opening in a register (the log).

Where Linux keeps it all, as plain text: /etc/passwd (users), /etc/group (groups), /etc/shadow (password hashes, readable by root only).

Before you start

  1. A Google Cloud account (the free trial works) and a project.
  2. Open Cloud Shell (the >_ icon at the top right of the Google Cloud console) and set your project:
export PROJECT_ID="your-project-id"
gcloud config set project $PROJECT_ID
gcloud services enable compute.googleapis.com

Part B: the lab

Step 1: a free e2-micro VM

Why: a server where you are the admin, so you can create users safely. --no-service-account --no-scopes gives it no Google Cloud permissions; this lab needs none.

gcloud compute instances create users-lab --zone=us-east1-b --machine-type=e2-micro \
  --image-family=debian-12 --image-project=debian-cloud --boot-disk-size=10GB --boot-disk-type=pd-standard \
  --no-service-account --no-scopes 2>&1 | tail -2
until gcloud compute ssh users-lab --zone=us-east1-b --tunnel-through-iap --command="echo ssh ok" 2>/dev/null | grep -q ok; do sleep 10; done; echo "SSH works"
NAME       ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP  STATUS
users-lab  us-east1-b  e2-micro                   10.142.0.23  EXTERNAL_IP  RUNNING
SSH works

Connect with Compute Engine > VM instances > SSH next to users-lab, or from Cloud Shell gcloud compute ssh users-lab --zone=us-east1-b. Everything from Step 2 runs inside the VM in ~/lab278.

Step 2: who am I, and where are users kept?

Why: id shows your UID, your main group and all your groups. getent passwd and getent group read the user and group lists. We also install nginx, a web server the developers will be allowed to restart.

# Who am I, and where does Linux keep users and groups?
whoami; id
getent passwd root $USER | sed "s/$USER/you/g"
getent group sudo google-sudoers | sed "s/$USER/you/g"
sudo apt-get update -qq && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nginx > /dev/null && systemctl is-active nginx
you
uid=1000(you) gid=1003(you) groups=1003(you),4(adm),30(dip),44(video),46(plugdev),1000(google-sudoers),1001(docker),1002(lxd)
root:x:0:0:root:/root:/bin/bash
you:x:1000:1003::/home/you:/bin/bash
sudo:x:27:
google-sudoers:x:1000:you
active

A line of /etc/passwd has 7 fields split by :: name, x (the password is in /etc/shadow), UID, GID, comment, home folder and shell. Your own user is in the group google-sudoers: Google Cloud adds that group (with a sudo rule) when you connect with SSH from the console or gcloud, and that is why you can use sudo.

Step 3: three users and a group

Why: groupadd developers makes the group. useradd -m makes a user with a home folder, -s /bin/bash sets the shell, -c a comment (the full name) and -G developers adds the user to the group.

# Three people share this server: ana and ravi (developers) and an intern
sudo groupadd developers
sudo useradd -m -s /bin/bash -c "Ana, developer" -G developers ana
sudo useradd -m -s /bin/bash -c "Ravi, developer" -G developers ravi
sudo useradd -m -s /bin/bash -c "Summer intern" intern
getent group developers
for u in ana ravi intern; do id $u; done
getent passwd ana
sudo ls -ld /home/ana /home/ravi /home/intern
developers:x:1004:ana,ravi
uid=1001(ana) gid=1005(ana) groups=1005(ana),1004(developers)
uid=1002(ravi) gid=1006(ravi) groups=1006(ravi),1004(developers)
uid=1003(intern) gid=1007(intern) groups=1007(intern)
ana:x:1001:1005:Ana, developer:/home/ana:/bin/bash
drwxr-xr-x 2 ana    ana    4096 Nov 18 20:20 /home/ana
drwxr-xr-x 2 intern intern 4096 Nov 18 20:20 /home/intern
drwxr-xr-x 2 ravi   ravi   4096 Nov 18 20:20 /home/ravi
Users and groups
Your own user, then ana and ravi in developers and an intern in no team group, each with their own home folder

Each user also got a private group with the same name (ana, ravi, intern). That is normal on Debian and Ubuntu.

Break it: nobody new can use sudo

ana tries to restart nginx. sudo -u ana runs the command as ana, and sudo -n means "never ask for a password, fail instead" (these lab users have no passwords; they would sign in with SSH keys).

# Break it: nobody new can use sudo yet
sudo -u ana sudo -n systemctl restart nginx
sudo: a password is required

sudo found no rule for ana that allows this without a password, so it refused.

Fix it: a sudoers.d rule, checked by visudo first

Why: the safe way to add a rule:

  1. Write it in a separate file (never edit /etc/sudoers directly).
  2. Check it with visudo -cf <file> before it goes live.
  3. Install it into /etc/sudoers.d/ owned by root with mode 0440 (read-only), which sudo requires.

The rule, word by word: %developers (the % means a group) ALL= (on any host) (root) (may act as root) NOPASSWD: (without a password) followed by the exact commands, with full paths.

# Fix it the safe way: a small rule file in /etc/sudoers.d, checked by visudo BEFORE it goes live
cat > developers.sudo <<'RULE'
# developers may restart and check nginx, nothing else
%developers ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx, /usr/bin/systemctl status nginx
RULE
sudo visudo -cf developers.sudo
sudo install -m 0440 -o root -g root developers.sudo /etc/sudoers.d/developers
sudo -l -U ana | tail -2
developers.sudo: parsed OK
User ana may run the following commands on users-lab:
    (root) NOPASSWD: /usr/bin/systemctl restart nginx, /usr/bin/systemctl status nginx

sudo -l -U ana lists what ana may do: exactly two commands.

A checked sudo rule
Refused at first; the rule is parsed OK by visudo, installed with mode 0440, and sudo -l shows the two commands

Step 4: test the rule

Why: a rule is only proven when you test what it allows and what it refuses.

# Test the rule: allowed, not allowed, and the intern
sudo -u ana sudo -n systemctl restart nginx && echo "ana restarted nginx"
sudo -u ravi sudo -n systemctl status nginx | head -3
sudo -u ravi sudo -n systemctl status nginx --no-pager | head -1
sudo -u ana sudo -n cat /etc/shadow
sudo -u intern sudo -n systemctl restart nginx
echo "done"
ana restarted nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Wed 2026-11-18 20:21:24 UTC; 39ms ago
sudo: a password is required
sudo: a password is required
sudo: a password is required
done
sudo rule tested
ana may restart nginx, ravi may check it; an extra argument, /etc/shadow and the intern are refused

Read each line:

  • ana restarted nginx, and ravi saw it active (running): the rule works for both developers.
  • ravi's second try added --no-pager. That is not the exact command in the rule, so sudo refused it. sudo compares the whole command line, arguments included.
  • ana asking for /etc/shadow was refused: it is not in the rule.
  • The intern is not in developers, so nothing is allowed.

Break it: a typo in a sudoers rule

One missing colon after NOPASSWD is enough. If this file were copied straight into /etc/sudoers.d/, sudo would report a parse error on every use until root fixed it, and on a server where sudo is the only way to become root, that can lock you out.

# Break it: a typo in a sudoers rule. visudo catches it, so it never reaches /etc/sudoers.d
printf '%%developers ALL=(root) NOPASSWD /usr/bin/systemctl restart nginx\n' > bad.sudo
cat bad.sudo
sudo visudo -cf bad.sudo; echo "visudo exit $? (1 = refused, nothing installed)"
sudo visudo -c | tail -2
%developers ALL=(root) NOPASSWD /usr/bin/systemctl restart nginx
bad.sudo:1:65: syntax error
%developers ALL=(root) NOPASSWD /usr/bin/systemctl restart nginx
                                                                ^
visudo exit 1 (1 = refused, nothing installed)
/etc/sudoers.d/developers: parsed OK
/etc/sudoers.d/google_sudoers: parsed OK

visudo points at the exact column of the error and installs nothing. visudo -c without a file checks every sudoers file that is live: all parsed OK. That is why every rule goes through visudo first.

visudo
visudo points at the missing colon after NOPASSWD and refuses the file

Step 5: the sudo log (who did what)

Why: every sudo call is written to the system journal, with who ran it, from which folder, as which user, and the full command. This is your audit trail.

# Who used sudo? Every sudo command is logged
sudo journalctl _COMM=sudo --no-pager -o cat --since "-10min" > sudo.log
grep "COMMAND" sudo.log | grep -E "ana|ravi|intern" | sed -E 's/^ +//; s/ +; +/ ; /g' | tail -5
🎓⭐ Recommended by CareerByteCode · Live trainingCloud & DevOps Live Cohort Internshipβ‚Ή2,360See the training →
you : PWD=/home/you/lab278 ; USER=ravi ; COMMAND=/usr/bin/sudo -n systemctl status nginx
ravi : PWD=/home/you/lab278 ; USER=root ; COMMAND=/usr/bin/systemctl status nginx
you : PWD=/home/you/lab278 ; USER=ravi ; COMMAND=/usr/bin/sudo -n systemctl status nginx --no-pager
you : PWD=/home/you/lab278 ; USER=ana ; COMMAND=/usr/bin/sudo -n cat /etc/shadow
you : PWD=/home/you/lab278 ; USER=intern ; COMMAND=/usr/bin/sudo -n systemctl restart nginx

You can see our own user acting as ravi, and ravi running systemctl status nginx as root. On a team server this answers "who restarted the web server at 3 a.m.?".

sudo log
Every sudo call in the journal: who, from which folder, as which user, which command

Step 6: lock an account when someone leaves

Why: the internship is over. usermod -L locks the password and -e 1 sets the account to "expired" (day 1 = 2 January 1970, long ago). passwd -S shows the status (L = locked) and chage -l the dates. Then we test a login, even from root.

# The intern's internship ends: lock the account and set an expiry date
sudo usermod -L -e 1 intern
sudo passwd -S intern
sudo chage -l intern | head -4
sudo su - intern -c 'echo logged in as intern'
intern L 2026-11-18 0 99999 7 -1
Last password change					: Nov 18, 2026
Password expires					: never
Password inactive					: never
Account expires						: Jan 02, 1970
Your account has expired; please contact your system administrator.
su: Authentication failure

An expired account cannot start a session, even through su from root: Your account has expired. Locking first (instead of deleting at once) keeps the person's files for a handover.

Step 7: remove a user for good

Why: userdel -r removes the user and their home folder. Afterwards the name no longer exists, and the developers group is unchanged.

# Remove the intern for good (with the home folder), and see who is left
sudo userdel -r intern 2>&1 | grep -v "mail spool"
id intern 2>&1
getent group developers
id: β€˜intern’: no such user
developers:x:1004:ana,ravi
Lock and remove
The intern is locked (L) and expired, refused even from root, then removed with the home folder

Step 8: clean up the lab users

# Clean up the lab users, group and rule
sudo rm /etc/sudoers.d/developers
for u in ana ravi; do sudo userdel -r $u 2>&1 | grep -v "mail spool"; done
sudo groupdel developers && echo "group developers removed"
group developers removed

POC: prove it works

  • Three users and a developers group were made, each with a home folder.
  • A sudo rule in /etc/sudoers.d/ let developers restart and check nginx and nothing else, with the exact command.
  • visudo refused a rule with a typo and installed nothing.
  • The journal logged every sudo call with user and command.
  • An expired account was refused even from root, and userdel -r removed it.

Quiz task: check what you learned

  1. Add carol to developers without removing her other groups. *(Answer: sudo usermod -aG developers carol.)*
  2. Write a rule that lets the group ops run /usr/bin/systemctl restart app as root without a password. *(Answer: %ops ALL=(root) NOPASSWD: /usr/bin/systemctl restart app, checked with visudo -cf.)*
  3. A rule allows systemctl status nginx but sudo systemctl status nginx -l is refused. Why? (Answer: sudo matches the whole command with its arguments.)
  4. Where do you see who used sudo? *(Answer: sudo journalctl _COMM=sudo, or /var/log/auth.log on systems that keep it.)*
  5. Someone leaves the team today. Lock or delete? *(Answer: lock and expire first (usermod -L -e 1), hand over the files, then userdel -r.)*

Clean up (delete everything)

Exit the VM (exit), then in Cloud Shell:

gcloud compute instances delete users-lab --zone=us-east1-b --quiet 2>&1 | tail -1
echo "users-lab VMs left: $(gcloud compute instances list --filter="name=users-lab" --format="value(name)" | wc -l)"
echo "users-lab disks left: $(gcloud compute disks list --filter="name~^users-lab" --format="value(name)" | wc -l)"
Deleted [https://www.googleapis.com/compute/v1/projects/your-project-id/zones/us-east1-b/instances/users-lab].
users-lab VMs left: 0
users-lab disks left: 0
Clean up
The users-lab VM and its disk are deleted: 0 left

FAQ

**Why not just add everyone to the sudo group? That group may run any** command as root. One mistake or one stolen laptop then means a fully compromised server. Give each group only the commands it needs.

**What is the difference between sudo and su?** su switches to another user (root by default) with that user's password. sudo runs one command with your own identity checked against the rules, and logs it. Teams prefer sudo.

How do Google Cloud VMs decide who gets sudo? With the console or gcloud compute ssh, Google adds your user to google-sudoers. With OS Login, IAM roles decide: roles/compute.osAdminLogin gives sudo, roles/compute.osLogin does not.

**Is NOPASSWD safe?** For a short list of exact commands, yes, and it is common on cloud servers where users have SSH keys and no passwords. Never use NOPASSWD: ALL for a group of people.

**What do useradd and adduser differ in?** useradd is the low-level tool on every Linux. adduser (Debian and Ubuntu) is a friendlier wrapper that asks questions. Scripts use useradd.

How do I see who is logged in now? who or w, and last for past logins.

Troubleshooting

You seeWhyFix
user is not in the sudoers fileNo rule for that user or groupAdd a rule in /etc/sudoers.d/ with visudo
sudo: a password is requiredNo NOPASSWD rule matches the exact commandCheck sudo -l -U <user> and the arguments
>>> /etc/sudoers.d/x: syntax errorA typo in a rule filesudo visudo -cf /etc/sudoers.d/x, fix the line
A sudoers.d file is ignoredIts name has a . or ends in ~, or the mode is not 0440Rename it (for example developers) and chmod 0440
A new group member is still refusedGroups load at loginLog out and in again
userdel: user is currently used by processThe user still runs programssudo pkill -u <user>, then userdel -r

What next

Prepared by the CareerByteCode Team from our tested lab, with AI help for the write-up.

Read next →Linux File Permissions Explained with Real Mistakes⏱ 14 min

⭐ Keep learning with CareerByteCode

Hand-picked to go deeper on this topic
News, guides and updates from the CareerByteCode team
🏅 Expert writer

The CareerByteCode team writes about what is new on careerbytecode.in: courses, labs, exams, events and the community, which is free to join for influencers and content creators in tech.

Get CareerByteCode Team's next blog by email

One email a week when they publish. Add the weekly Best of ByteBlog too. No spam, unsubscribe with one click.

We email you once to confirm. By subscribing you agree to the privacy policy.

💬 Comments (0)

Top · Newest

Sign in to comment

Related blogs

🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterβ‚Ή499Build the projects →