Lab 09: chown and chgrp — Ownership

Objective

Change file and directory ownership with chown and chgrp: assign individual owners, set group ownership, apply recursively, and understand why ownership matters for security.

Time: 25 minutes | Level: Foundations | Docker: docker run -it --rm ubuntu:22.04 bash


Step 1: Setup — Create Users and Groups

useradd -m alice
useradd -m bob
groupadd devteam
echo "Users and group created"
id alice

📸 Verified Output:

Users and group created
uid=1000(alice) gid=1000(alice) groups=1000(alice)

Step 2: Viewing Current Ownership

touch /tmp/myfile.txt
ls -la /tmp/myfile.txt

📸 Verified Output:

The format is: permissions links owner group size date name


Step 3: Changing Owner with chown

📸 Verified Output:

📸 Verified Output:

💡 chown user:group file changes both in one command. chown user: file (trailing colon) changes owner and sets group to user's primary group.


Step 4: Changing Only the Group

📸 Verified Output:

📸 Verified Output:


Step 5: Recursive Ownership Change

📸 Verified Output:

💡 -R (recursive) changes ownership of the directory and all files inside it. Use carefully — applying wrong ownership recursively to /etc can break your system.


Step 6: Why Ownership Matters — Access Control

📸 Verified Output:

💡 Root (uid=0) bypasses all permission checks. This is why running services as root is dangerous — a compromised root process can read any file on the system. Use dedicated service accounts instead.


Step 7: Service Account Pattern

📸 Verified Output:


Step 8: Capstone — Fix Misconfigured Service Directory

📸 Verified Output:


Summary

Command
Effect

chown user file

Change owner

chown user:group file

Change owner and group

chown :group file

Change group only

chown -R user:group dir

Recursive ownership change

chgrp group file

Change group only

ls -la

View owner and group

Last updated