Lab 2 of 8 · Users, groups and permissions
Set up a team folder with the right permissions
Create users and a group, build a folder only that team can use, and give one user a single admin command through sudo.
- Time
- 45–60 minutes
- Cost
- Free.
What you learn
Every file and process belongs to a user and a group, and permissions decide who can read, change or run it. Getting this right is most of day-to-day Linux security.
- Creating users and groups, and expiring accounts
- Read, write and execute permissions, in letters and numbers
- A shared team folder with the setgid bit
- Giving one admin command through sudo, safely
In the AI era: AI assistants often suggest `chmod 777` or blanket sudo rights to make an error go away. A sysadmin finds the smallest permission that works, and proves it with the system's own checks.
Get set up
Commands use Homebrew in the Terminal app. Already have a tool? Skip it.
- A terminalOfficial install page ↗
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Use the Terminal app. The command installs Homebrew, which the other macOS commands use.
Before you start
Finish lab 1.
Start your server and open your repository: run
multipass start sysadmin-labandmultipass shell sysadmin-labon your computer, thencd linux-admin-labon the server.
Step by step
1 Create a team and two users
alicejoins thewebteamgroup;bobdoes not.sudo groupadd webteam sudo useradd --create-home --shell /bin/bash --groups webteam alice sudo useradd --create-home --shell /bin/bash bob id alice id bobYou should see:alicelistswebteamamong her groups;bobdoes not.2 Build a shared folder
2770means the owner and the group can read, write and enter; everyone else gets nothing. The leading2(setgid) makes new files belong to thewebteamgroup automatically.sudo mkdir -p /srv/webteam sudo chown root:webteam /srv/webteam sudo chmod 2770 /srv/webteam ls -ld /srv/webteamYou should see:drwxrws---with ownerrootand groupwebteam.3 Prove who can use it
sudo -uruns a command as another user, so you can test their access without their password.sudo -u alice touch /srv/webteam/plan.txt sudo ls -l /srv/webteam sudo -u bob ls /srv/webteamYou should see:plan.txtbelongs to groupwebteam, and bob getsPermission denied.4 Give one admin command with sudo
Never edit
/etc/sudoersdirectly. Put a small rule in/etc/sudoers.dand letvisudocheck it: a broken sudoers file can lock everyone out of admin rights.echo 'alice ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx' | sudo tee /etc/sudoers.d/alice-nginx sudo chmod 440 /etc/sudoers.d/alice-nginx sudo visudo -cf /etc/sudoers.d/alice-nginx sudo -l -U aliceYou should see:parsed OK, and alice may run only/usr/bin/systemctl restart nginxas root.5 Ask AI for a sudo rule, then check it
Ask your AI assistant: "Write a sudoers rule that lets bob read the system journal and nothing else." Save its answer with
nano /tmp/bob-rule, then check it before it goes live. Reject any rule that grantsALL, a shell or an editor: those give full root. Evenjournalctlcan open a pager that starts a shell, so a safe rule pins--no-pager.sudo visudo -cf /tmp/bob-rule # only if the check passes and the rule is narrow: sudo install -m 440 /tmp/bob-rule /etc/sudoers.d/bob-journal sudo -l -U bobYou should see:parsed OK, and bob's list shows one narrow command, such as/usr/bin/journalctl --no-pager.6 Expire an account
When someone leaves, expire their account instead of deleting it straight away, so their files stay for review.
sudo usermod --expiredate 1 bob sudo chage -l bob | grep "Account expires"You should see:Account expires : Jan 02, 1970: the account can no longer be used.7 Write your notes
Create the file with
nano notes/02-users.md.# Users, groups and permissions ## The team folder - Permissions on `/srv/webteam`: ... - What the setgid bit did: ... ## sudo rules - alice may run: ... - What the AI suggested for bob, and what I changed: ... ## Leavers - Why expire before deleting: ...8 Commit and push
Save your work to GitHub from the server.
git add notes git commit -m "Add users, groups and sudo notes" git push
You are done when
/srv/webteamisdrwxrws---and only members ofwebteamcan use it.Every sudo rule passed
visudo -cand grants single commands, neverALL.notes/02-users.mdis on GitHub.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.