Users, groups and sudo
Give a new colleague an account, put you both in a group that can edit the website folder, and learn what sudo does, on a simulated Linux system.
Maya is joining the team. She needs her own account on this machine, and she and you both need to edit the website in /srv/site. On Linux that's three ideas: users, groups, and root, the administrator who may change anything. The system here is a simulation in your browser: no real accounts, passwords or software are involved.
Who you are
whoamiidls -ld /srv/siteid lists your groups. Every file has an owner and a group, and the middle three letters of its permissions (rwx) are what members of that group may do. /srv/site belongs to root, so only root may write there.
sudo: one command as root
You don't log in as root. Instead, sudo runs one command as root, if you're in the sudo group (you are). It asks for your own password, then remembers it for a few minutes:
sudo whoamiUse it only where it's needed. sudo -i opens a root shell (the prompt ends in #); exit leaves it.
Users and groups
| Command | Does |
|---|---|
sudo useradd -m -s /bin/bash maya | a new user, with a home folder (-m) and bash as her shell |
sudo passwd maya | sets her password |
sudo groupadd webteam | a new group |
sudo usermod -aG webteam maya | adds maya to webteam |
groups maya | lists her groups |
sudo chown root:webteam /srv/site | gives the folder the group webteam (owner:group) |
sudo chmod g+w /srv/site | lets the group write in it |
Careful:In usermod -aG, the a means append. Without it, -G replaces all of the user's groups with the ones you name. Do that to yourself and you leave the sudo group, and can't use sudo any more.
su - maya starts a shell as Maya, to check things from her side (after setting her password); exit comes back.
Your turn
Set things up for Maya: (1) create the user maya with a home folder and /bin/bash as her shell; (2) create the group webteam and add both maya and yourself (learner) to it, keeping your other groups; (3) give /srv/site the group webteam and let the group write in it; (4) as yourself, create the file /srv/site/index.html.
Your task
Set things up for Maya: (1) create the user maya with a home folder and /bin/bash as her shell; (2) create the group webteam and add both maya and yourself (learner) to it, keeping your other groups; (3) give /srv/site the group webteam and let the group write in it; (4) as yourself, create the file /srv/site/index.html.
- maya exists, with a home folder and bash (not done yet)
- maya and you are in webteam (you keep sudo) (not done yet)
- /srv/site belongs to webteam, and the group can write there (not done yet)
- You made /srv/site/index.html yourself (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
sudo- Run a command as the administrator (root) More on sudo
useradd- Create a user account More on useradd
groupadd- Create a group
usermod- Change a user account More on usermod
chown- Change who owns a file More on chown
id- Show a user’s ID and groups
Every everyday command on one page: the Linux commands cheat sheet.