DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

Linux & BashLesson 8 of 16
Your progress · 0%
On this page
  1. Who you are
  2. Where accounts live
  3. Creating, changing and removing users
  4. Groups
  5. Service accounts
  6. sudo rights: exactly what is needed
  7. A shared team folder
↖ Course roadmap
Lesson 8 · Users & groups

Users and groups: who can do what

Create, change, lock and remove accounts, use groups to share access, give exactly the sudo rights someone needs, and run services as accounts nobody can log in to.

35 min read · beginner · hands-on

By the end of this lesson you'll be able to

0 of 6 complete

Who you are

Before a server had separate accounts, people could share a login and its access. That made it impossible to tell which person changed a file or to remove one person's access without changing everyone else's. Linux identities assign each process a user ID and groups, which the kernel checks against file permissions and other access controls.

Every process runs as a user, and every file belongs to a user and a group. The primary group is used for newly created files; supplementary groups add access such as sudo or log reading. Start by checking the identity of the shell before changing a file or running an administrative command:

$ id
uid=1000(rajeev) gid=1000(rajeev) groups=1000(rajeev),4(adm),27(sudo),998(docker)
$ getent passwd rajeev
rajeev:x:1000:1000:Rajeev Kumar:/home/rajeev:/bin/bash

Your primary group (gid) is given to the files you create. Your supplementary groups give extra access: sudo lets you use sudo, docker lets you talk to Docker, adm lets you read many logs.

Where accounts live

Three plain-text files hold every account. Read them with getent; change them with the tools.© Diagram: DevOps Academy

Three plain-text files hold every account. Read them with getent; change them with the tools.© Diagram: DevOps Academy
FileHoldsReadable by
/etc/passwdnames, ids, home folders, shellseveryone
/etc/shadowpassword hashes, password ageingroot only
/etc/groupgroups and their memberseveryone
$ getent group devops                # a group and its members
devops:x:1005:asha,ravi
$ awk -F: '$3 >= 1000 {print $1}' /etc/passwd   # human accounts (ids from 1000)

User ids below 1000 belong to the system and to services (www-data, postgres, sshd). Don't edit these files by hand: the tools below lock them, validate the fields and keep the three files in step. getent also asks LDAP or other directories if the server uses them.

Creating, changing and removing users

Use the account-management tools rather than editing /etc/passwd or /etc/shadow directly. useradd creates the account and can create its home directory; passwd sets credentials; usermod changes account settings; and userdel removes it. These tools validate fields and update related account files safely.

$ sudo useradd -m -s /bin/bash -c "Asha Verma" asha   # -m home folder, -s shell, -c full name
$ sudo passwd asha                                    # set her first password
$ sudo chage -d 0 asha                                # must choose a new one at first login
$ sudo chage -l asha                                  # password ageing details
$ sudo usermod -s /bin/zsh asha                       # change her shell
$ sudo usermod -L asha                                # lock: no password logins
$ sudo usermod -U asha                                # unlock
$ sudo chage -E 2026-12-31 contractor1                # the account expires on that date
$ sudo userdel -r olduser                             # delete the user and their home folder
sudo useradd -m -s /bin/bash -c "Asha Verma" asha
  • sudorun the next command as root

New home folders are copied from /etc/skel. Put a default .bashrc there and every new user gets it.

On Debian and Ubuntu, adduser asha is a friendlier wrapper that asks questions and creates the home folder for you. Scripts and other distributions use useradd, so learn that one.

When someone leaves the team

Lock first, delete later: sudo usermod -L the account, remove their SSH key from ~/.ssh/authorized_keys, and check crontab -l -u name for jobs that still run as them. Delete the account once you're sure nothing depends on it. A locked password doesn't stop SSH key logins, which is why the key must go too.

Linux Crash Course - Managing UsersLearn Linux TV · Watch on YouTube ↗

Groups

A group gives several users the same access without repeating permission changes for every person. Files can belong to a group, and supplementary group membership can grant access to devices, logs or administrative commands. Keep groups aligned with team responsibilities and review membership when people change roles.

$ sudo groupadd devops                  # create a group
$ sudo usermod -aG devops asha          # ADD asha to devops (keeps her other groups)
$ id asha
uid=1001(asha) gid=1001(asha) groups=1001(asha),1005(devops)
$ sudo gpasswd -d asha devops           # remove her from one group
$ sudo groupmod -n platform devops      # rename the group
$ sudo groupdel platform                # delete it
sudo groupadd devops
  • sudorun the next command as root
-a, every time

usermod -G docker asha (without -a) replaces all her supplementary groups with just docker. If that removed her sudo group on a server where she was the only admin, nobody can use sudo there any more. Type -aG as one word, every time.

Group changes apply at the next login. The person has to log out and back in (or run newgrp devops) before id shows the new group in their session.

Use group membership for shared access and remove membership when the job changes. Check the user's full group list before editing it, and use -aG with usermod to append one group without replacing the others. The new group list applies to new login sessions, not processes that are already running. A common mistake is assuming a user has the new permission immediately inside an existing SSH shell.

Linux Crash Course - Managing GroupsLearn Linux TV · Watch on YouTube ↗

Service accounts

If every service runs as root, a bug or compromise in one service can change any file on the host. A service account is a dedicated non-human user that owns or accesses only the files the application needs. Give it no interactive login and no unnecessary supplementary groups.

$ sudo useradd --system --no-create-home --shell /usr/sbin/nologin myapp
$ sudo chown -R myapp:myapp /srv/myapp
$ sudo -u myapp /srv/myapp/bin/server     # run something AS that user
sudo useradd --system --no-create-home --shell /usr/sbin/nologin myapp
  • sudorun the next command as root
sudo chown -R myapp:myapp /srv/myapp
  • chownchange owner

If the app is broken into, the attacker gets only what myapp can touch, and nologin means nobody can open a shell as it. Package installs already do this: www-data for nginx, postgres for PostgreSQL.

Use systemd's User= and Group= settings or the package's service account to run the process with limited rights. Keep writable directories separate from executable code when practical. A common mistake is creating a service user but still launching the process as root, so verify the effective user in the service status or process list.

sudo rights: exactly what is needed

Members of the sudo group (wheel on Red Hat systems) may run anything as root. That's right for administrators and too much for most people. Give everyone else exactly the commands they need, in a separate file per team:

Who, where, as whom, and exactly which commands. Nothing else is allowed.© Diagram: DevOps Academy

Who, where, as whom, and exactly which commands. Nothing else is allowed.© Diagram: DevOps Academy
$ sudo visudo -f /etc/sudoers.d/devops
sudo visudo -f /etc/sudoers.d/devops
  • sudorun the next command as root
# devops may manage nginx, nothing else
%devops ALL=(root) /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx
$ sudo visudo -c                         # check every sudoers file
/etc/sudoers: parsed OK
/etc/sudoers.d/devops: parsed OK
$ sudo -l -U asha                        # what may asha run?
User asha may run the following commands on web-01:
    (root) /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx
sudo visudo -c
  • sudorun the next command as root

Always use full paths in rules (which systemctl tells you), and always edit with visudo: it refuses to save a file with a mistake in it. Be careful with NOPASSWD: and with programs that can start a shell (editors, less, find -exec, any script the user can edit): allowing them is the same as allowing everything.

su - asha switches to another account and needs her password. sudo -iu asha does the same with your sudo rights, which is how admins check a user's environment without knowing their password.

Linux Crash Course - sudoLearn Linux TV · Watch on YouTube ↗

A shared team folder

When several people maintain one deployment tree, personal ownership can block teammates from updating files. Give the directory a shared group, enable the setgid bit so new entries inherit that group, and choose a umask that grants the intended group access without opening files to every user.

Combine groups with the setgid bit from the previous lessons:

$ sudo mkdir -p /srv/app
$ sudo chgrp devops /srv/app
$ sudo chmod 2775 /srv/app               # 2 = setgid, 775 = rwx for owner and group
$ ls -ld /srv/app
drwxrwsr-x 2 root devops 4096 Oct 13 12:10 /srv/app
sudo mkdir -p /srv/app
  • sudorun the next command as root
  • mkdirmake a directory
  • -pcreate missing parents, no error if it exists
sudo chmod 2775 /srv/app
  • chmodchange permissions
ls -ld /srv/app
  • lslist files

The s in the group set is setgid. New entries inherit the directory's devops group, regardless of who creates them. Group inheritance alone does not make every file writable; the creating process's umask still controls its permission bits.

Use a shared group and setgid on team directories, then choose a group-friendly umask for the users or services writing there. Verify new files with ls -l before relying on the setup. A common mistake is assuming setgid grants write access by itself; it selects the group, while mode bits decide what that group can do.

⌘
Mini mission

Onboard a developer and a service

Asha joins the devops team. She needs a login, membership of devops, permission to restart and reload nginx with sudo (nothing else), and write access to /srv/app. The app itself must run as its own account that can't log in.

Input: A Linux VM where you have sudoOutput: Users asha and myapp, /etc/sudoers.d/devops, /srv/app
Reveal one safe solution
$ sudo groupadd devops
$ sudo useradd -m -s /bin/bash -c "Asha Verma" -G devops asha
$ sudo passwd asha && sudo chage -d 0 asha
$ echo '%devops ALL=(root) /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx' \
    | sudo EDITOR='tee' visudo -f /etc/sudoers.d/devops > /dev/null
$ sudo visudo -c
$ sudo useradd --system --no-create-home --shell /usr/sbin/nologin myapp
$ sudo mkdir -p /srv/app && sudo chgrp devops /srv/app && sudo chmod 2775 /srv/app
$ id asha
uid=1001(asha) gid=1001(asha) groups=1001(asha),1005(devops)
$ sudo -l -U asha | tail -n 1
    (root) /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx
$ getent passwd myapp
myapp:x:998:998::/home/myapp:/usr/sbin/nologin
sudo groupadd devops
  • sudorun the next command as root
sudo passwd asha && sudo chage -d 0 asha
  • &&run the next command only if this one worked
echo '%devops ALL=(root) /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx' \
  • echoprint text
sudo mkdir -p /srv/app && sudo chgrp devops /srv/app && sudo chmod 2775 /srv/app
  • mkdirmake a directory
  • -pcreate missing parents, no error if it exists
  • chmodchange permissions
sudo -l -U asha | tail -n 1
  • |pipe: send output to the next command
  • tailshow the last lines
  • -nhow many lines

EDITOR='tee' visudo -f … writes the rule through visudo, so it's still syntax-checked, without opening an editor: handy in scripts. getent shows myapp with nologin, and its user id below 1000 marks it as a system account.

Try it yourself

0 of 6 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 5

You run sudo usermod -G docker asha. What happens to her other groups, like sudo and devops?

Go deeper

Official reading for this lesson

  • Docsuseradd(8) manualUbuntu manpages ↗
  • Docsusermod(8) manualUbuntu manpages ↗
  • Docssudoers(5): rules, aliases and examplesUbuntu manpages ↗
  • Docsshadow(5): the password file formatUbuntu manpages ↗
  • GuideLPIC-1 exam 102, objectives 107.1 and 110.1Linux Professional Institute ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousProcesses: see them, control them, stop themNext lesson →Services with systemd: run, restart, read the logs