Week 6: Users, permissions, file systems and quotas
/etc/passwd
and passwords (hashes) in /etc/shadow
.Why multiple users?
Isolation and security
Accountability (who did what)
Least-privilege principle
Tip
amsa
.root
root
(UID 0) can read/write anything and change system config.root
.sudo
to run one command with elevated privileges.Important
sudo <cmd>
runs <cmd>
as root.
sudo
work?Are this commands familiar? This a situation among many where we need admin rights, and so we run the command with sudo
at the beginning.
Caution
If you run tools that create files (e.g., ssh-keygen
) with sudo
, those files will belong to root and land in root’s home.
A group (identified by GID) is a set of users sharing permissions on files/dirs.
Each user has a primary group and may also belong to other groups.
Useful commands:
groups [user]
– list which groups is user
part ofid [user]
– the same but also shows UID and GIDs/etc/passwd
(users)/etc/shadow
(passwords)Let’s create and configure a test user
# Create user 'test' with a home dir
sudo useradd -m test
# Set a password
sudo passwd test
# Add to 'wheel' group (if desired)
sudo usermod -aG wheel
Tip
-m
creates home dir at /home/<user>
.
Note
Which groups will a new user have?
Each user can have a home dir, which by default is located at /home/<username>
.
Root user’s home dir is not located on /home/root
but on /root
Important
It is important to understand when to run commands with root and when not to, do not prepend sudo
unless you truly want it for root. As a rule of thumb, avoid using root if you can.
Tip
Running the command cd
without specifying which directory do we want to go to will take us to our home dir.
To switch to another user, we do not need to log out and log back in as the other user. Instead, it is possible to switch user temporarily from the terminal.
# Switch to test user (will ask for `test` user password)
su test
# Using sudo (will ask for our user password)
sudo su test
Note
Why does the first command ask for test
user password and the second one ask for our password?
Tip
Use the whoami
command to check which is your current user.
To modify permissions we are going to use chmod
command.
Just so you know, instead of changing permissions by using their letters, they can also be referred to using numbers.
Permission | Value |
---|---|
Read | 4 |
Write | 2 |
Execute | 1 |
No perm | 0 |
So for example, this two commands are equivalent:
You may have noticed there that before the three groups of permissions, there is a character that can be a d
or a -
. This indicates if the file is a directory or not.
For directories, r
means you can list files, w
means you can create/delete files, and x
means you can enter the directory.
setgid
is set on a directory, files created inside inherit the group of the directory. setuid
has no effect on directories.Note
If both bits are set on a file, the process runs with both user and group of the file.
Aqui fem un fitxer amb el setuid i tal amb C com vam fer amb el pablo l’altre dia i que printi el effective user id
We’ll create a little c program that prints the user that is owning the process when executed.
We’ll run it with sudo, which will make the process be owned by root
.
sudo
actually doesIf we take a look at the sudo
binary, which we can find by running which sudo
, we can see that it has the setuid bit set and is owned by root.
So, if sudo is a process that will be owned by root, if it executes another process, that process will also be owned by root.
/etc/sudoers
file, where we’ll usually find a line like this:wheel
group can run any command as any user on any host.Note
Ubuntu does not use wheel
group, but sudo
or adm
instead. The name wheel
comes from early Unix systems, and it was chosen because it was a “big wheel” group with special privileges, “big wheel” being a slang term for an important person.
Filesystem | Use case | Notes |
---|---|---|
ext4 | General Linux systems | Most common Linux filesystem |
ext2 | Older Linux systems | No journaling |
xfs | High-performance systems | Good for large files |
btrfs | Advanced features (snapshots, etc) | Still maturing |
vfat | USB drives, cross-platform | Compatible with Windows and macOS |
ntfs | Windows systems | Read/write support in Linux |
tmpfs | Temporary files in RAM | Very fast, data lost on reboot |
We’ll format a USB drive with ext4 filesystem. First of all We’ll use lsblk
to identify the device. Make sure you identify it correctly, as using the wrong device can lead to data loss.
Then, we’ll use mkfs.ext4
to format it, and mount
to mount it.
Assuming the partition is /dev/sdb1
, we would do:
Warning
Formatting a drive will erase all data on it. Make sure to back up any important data before proceeding.
Let’s merge what we have learned so far to set up disk quotas.
Note
Quotas are per-filesystem. You must enable them when mounting the filesystem.
We’ll assume you have an ext4 file system for the device /dev/sdb1
mounted at /mnt/amsa
. If your device is different (which will probably be the case), please change it accordingly.
# Make sure the filesystem is unmounted
sudo umount /mnt/amsa
# Permanently enable quotas on the filesystem
sudo tune2fs -O quota /dev/sdb1
# Mount the filesystem with user and group quotas enabled
sudo mount -o usrquota,grpquota /dev/sdb1 /mnt/amsa
Tip
To verify it has been mounted with quotas enabled, run mount | grep /mnt/amsa
and check for usrquota
and grpquota
in the options list.
Now we’re redy to configure quotas on the filesystem
We’re going to use setquota
command.
We’ll need to specify the disk usage limits (we can use K, M, G and T for Kilobytes, Megabytes, …).
To limit the max amount of files that can be created we’ll set the desired amount.
Tip
Check current quotas: quota -u amsa -v
(per user), quota -g root -v
(per group).
Note
To turn quotas off sudo quotaoff /mnt/amsa
Tip
To test quotas, use the following commands to create files until you exceed the limits:
sudo ssh-keygen
, where do keys go and why?-rwxr-xr--
.drwxr-sr-x
?/etc/passwd
contain?/etc/passwd
file: https://www.cyberciti.biz/faq/understanding-etcshadow-file/Ready to have some fun? Check out the second part of the third AMSA activity here!
AMSA 2025-2026 - 🏠 Home