Skip to content

Users, groups and sudo

AdvancedLesson 41 of 6610 min

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

whoami
id
ls -ld /srv/site

id 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 whoami

Use it only where it's needed. sudo -i opens a root shell (the prompt ends in #); exit leaves it.

Users and groups

CommandDoes
sudo useradd -m -s /bin/bash mayaa new user, with a home folder (-m) and bash as her shell
sudo passwd mayasets her password
sudo groupadd webteama new group
sudo usermod -aG webteam mayaadds maya to webteam
groups mayalists her groups
sudo chown root:webteam /srv/sitegives the folder the group webteam (owner:group)
sudo chmod g+w /srv/sitelets 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.

Next