Users, groups, and permissions

Use a shared file to test account access and calculate each digit in a chmod permission mode.

🎯 Introduction

Suppose you want to share a file on a server so team members can read it but only an administrator can change it. How does Linux identify group members and restrict them to reading? We will create a practice account, grant read access, and test it using that account.


🧱 Linux first checks who you are

The name you use to log in to Ubuntu identifies a user account. Linux uses accounts to track who initiates operations. root is the administrator account and can manage the entire machine. Use a regular account for everyday work and sudo for individual commands that require administrator privileges. The system may ask for your current account's password.

Use id to inspect your account and its group memberships.

id

Find groups= in the output. The names that follow are your groups. A user can belong to several groups. Adding accounts that need to read a file to one group lets you grant read access to that group together.


🛠 Prepare a reader account and group

Run these steps on your own Ubuntu practice machine. course-reader represents an account that reads files, and course-readers is the group with read access.

Privilege prompts and command output may vary by system version and language. If the practice account or group already exists, skip its creation command.

First create the reader account with useradd. -m creates its home directory.

sudo useradd -m course-reader

Next create the reader group with groupadd. You still need to add the account after creating the group.

sudo groupadd course-readers

usermod changes an existing account. -G specifies supplementary groups, and -a appends membership while preserving existing groups. Put the group name first, followed by the username.

sudo usermod -aG course-readers course-reader

Add a username after id to inspect that account. Confirm that the output includes course-readers before continuing.

id course-reader

🛠 Create the shared directory and file

First create the directory. /srv can hold files provided by a server. Use sudo for the privileges needed to create its subdirectory.

sudo mkdir -p /srv/linux-course-shared

Next use Vim, introduced earlier, to create a file. Prefixing the command with sudo lets Vim save in this directory.

sudo vim /srv/linux-course-shared/notes.txt

Press i, type Shared file, press Esc, type :wq, and press ↩ Return to save and quit.

Set the group for the directory and file

A file's owner is the account that owns it. Each file has one owner. Because sudo vim saved this file as root, root owns it. Directories also have owners.

The owning group determines which group receives group permissions, and it can contain multiple accounts. chown changes ownership. In owner:group, the two sides specify the account and group. Set the directory's owner to root and its group to course-readers.

sudo chown root:course-readers /srv/linux-course-shared

Set the file's group separately. A new file saved using sudo is already owned by root; use chgrp to change only its group.

sudo chgrp course-readers /srv/linux-course-shared/notes.txt

🧱 Read permissions before calculating the digits

Use ls -l to inspect the shared file's permissions, owner, and group.

sudo ls -l /srv/linux-course-shared/notes.txt

You will see a line similar to this one. Focus on -rw-r--r-- at the left: ten characters from the initial - to the first space.

-rw-r--r-- 1 root course-readers 12 Nov 11 09:30 /srv/linux-course-shared/notes.txt

Here, root is the file's owner and course-readers is its group.

Split those ten characters into - | rw- | r-- | r--. The initial - means a regular file; directories show d. The first triplet, rw-, gives permissions for the owner, root. The second, r--, applies to members of the reader group. The third, r--, applies to other accounts that are neither the owner nor members of that group. Your permission values may differ, but the grouping is the same.

Each triplet has positions for r, w, and x. A - means that permission is absent.

CharacterFor filesFor directoriesValue
rRead contentsList filenames inside4
wModify contentsTogether with x, create, delete, or rename entries2
xRun an executable fileEnter the directory or traverse it to access paths inside1

Set the file mode to 640

chmod changes permissions. We want the owner to read and write, the reader group to read only, and everyone else to have no access. Calculate permissions separately for each category.

DigitRequired permissionsCalculation
First: ownerRead and write, rw-4 + 2 = 6
Second: groupRead only, r--4
Third: othersNo access, ---0

For each digit, add only the permissions required by that category. Write 6, 4, and 0 in order to get 640. Use 7, calculated as 4 + 2 + 1, only when that category needs read, write, and execute permissions.

Now apply 640 to the shared file.

sudo chmod 640 /srv/linux-course-shared/notes.txt

Run ls -l again. The permissions should be -rw-r-----. The initial - is the file type; read the remaining permissions as rw- r-- ---. Also confirm that the group is course-readers.

Set the directory mode to 750

To open the file, the reader must also traverse its directory. Let the owner manage the directory, the reader group list filenames and enter it, and others have no access.

DigitRequired permissionsCalculation
First: ownerList, add or remove entries, and enter, rwx4 + 2 + 1 = 7
Second: groupList and enter, r-x4 + 1 = 5
Third: othersNo access, ---0

Write 7, 5, and 0 as 750, then apply it to the directory.

sudo chmod 750 /srv/linux-course-shared

Use ls -ld to inspect the directory itself. -d displays the directory's own information so you can confirm that its mode starts with drwxr-x---.

ls -ld /srv/linux-course-shared

🛠 Verify read access with the reader account

By default, sudo runs commands as root. Adding -u course-reader runs this command as the reader account instead. If sudo asks for a password, enter the password of your currently logged-in account. Use cat to test whether the reader can open the shared file.

sudo -u course-reader cat /srv/linux-course-shared/notes.txt

You should see Shared file. This uses two permissions: x on the directory lets the reader group reach the file, and r on the file lets it read the contents. Neither the file nor the directory grants the group w, so the reader cannot modify, delete, or replace the file.


❓ Knowledge check

Q1: Why do I get Permission denied even though the reader group can read the file?

First confirm group membership with id course-reader. Then check every directory from /srv down to the file: the account needs permission to traverse each one.

Q2: Can I fix permission errors with chmod 777?

Each digit in 777 is 7, granting read, write, and execute permissions to all three categories, including others. This exercise only requires the reader group to read the file and traverse the directory, so set each mode according to those needs.


🧪 Exercise

Temporarily change the shared file's mode to 600 with chmod. Explain which permissions each digit grants and to whom, then repeat cat as course-reader. You should now see Permission denied.

Restore 640 and read the file with the same command. The exercise is complete when you can see Shared file again and explain what access the reader gained when the second digit changed from 0 to 4.


🎉 You can now investigate permission problems by checking the account, directories, and file!