Chapter 46: Bash Ownership
Bash Ownership (or more correctly, File & Folder Ownership in Linux/Bash)! ☕
This topic is super important because permissions (which we learned last time — rwx) only work together with ownership. Without understanding who owns what, you’ll keep wondering:
- Why can’t I delete this file even though I’m logged in?
- Why does my script say “Permission denied” when I try to run it?
- Why can my friend read my file but not change it?
- Why does nginx complain it can’t access /var/www/html/index.html?
In Linux, every file and every folder has two owners:
- User owner (the person/user who created it or last took ownership)
- Group owner (a group that the user belongs to — can be used to share access with teammates)
These two pieces of info decide who gets which set of permissions (the rwx we learned).
How Ownership Looks in Real Life (ls -l)
Type this right now:
|
0 1 2 3 4 5 6 7 8 |
ls -l # or better (shows human-readable sizes + group): ls -lh |
Typical output:
|
0 1 2 3 4 5 6 7 8 |
-rw-r--r-- 1 webliance webliance 2.3K Feb 26 09:15 report.pdf drwxr-xr-x 2 webliance developers 4.0K Feb 25 14:30 team_project -rwxr-xr-x 1 root root 456 Feb 26 10:00 /usr/bin/ls |
Breakdown of columns 3 and 4:
- Column 3 → User owner (the person)
- Column 4 → Group owner (the group)
So in the first line:
- User owner = webliance (you)
- Group owner = webliance (your default personal group — most home users have this)
In the second line:
- User owner = webliance
- Group owner = developers (maybe you changed it so your team can work together)
In the third line:
- User owner = root
- Group owner = root → System file — only superuser can change it.
Who Decides Ownership When a File is Created?
When you create a file or folder:
- User owner = the user who ran the command (usually you — whoami)
- Group owner = the primary group of that user (most home users: same as username)
Check your groups:
|
0 1 2 3 4 5 6 7 8 |
groups # or id -Gn |
Example output for a normal user:
|
0 1 2 3 4 5 6 |
webliance adm cdrom sudo dip plugdev lpadmin sambashare |
First group listed = primary group (usually same as username).
How to Change Ownership – chown & chgrp
chown = change owner (user and/or group)
chgrp = change group only (less used nowadays — chown can do both)
1. Change user owner (only root can usually do this)
|
0 1 2 3 4 5 6 |
sudo chown rahul report.pdf |
→ Now rahul owns the file (you need sudo because you can’t give away ownership unless you’re root).
2. Change group owner
|
0 1 2 3 4 5 6 7 8 |
chown :developers team_project/ # or chgrp developers team_project/ |
→ Now group = developers (team members in “developers” group can use group permissions)
3. Change both user and group at once
|
0 1 2 3 4 5 6 |
sudo chown rahul:developers important_file.txt |
4. Change ownership of everything inside a folder (Recursive – Very Common!)
Use -R (capital R):
|
0 1 2 3 4 5 6 |
sudo chown -R www-data:www-data /var/www/html/ |
→ Makes all files & subfolders owned by web server user/group (needed for nginx/Apache to write/upload)
|
0 1 2 3 4 5 6 |
chown -R webliance:developers ~/team_project/ |
→ You and your team now have proper access
Real-Life Examples You’ll See in Hyderabad Life
Example 1: Website on server not working?
|
0 1 2 3 4 5 6 7 |
ls -l /var/www/html/ # see: -rw-r--r-- 1 root root index.html |
→ Nginx (runs as www-data) can read but not write → fix:
|
0 1 2 3 4 5 6 7 |
sudo chown -R www-data:www-data /var/www/html/ sudo chmod -R 755 /var/www/html/ |
Example 2: Team project folder
|
0 1 2 3 4 5 6 7 8 |
mkdir ~/shared_code chown -R :developers ~/shared_code chmod -R 775 ~/shared_code |
→ Anyone in developers group can read/write/execute inside
Example 3: Secure your .ssh folder
|
0 1 2 3 4 5 6 7 8 |
chmod 700 ~/.ssh chown -R $USER:$USER ~/.ssh chmod 600 ~/.ssh/id_rsa |
→ Only you can access your keys
Example 4: You downloaded a file as root by mistake
|
0 1 2 3 4 5 6 7 8 |
sudo wget https://example.com/script.sh -O /home/webliance/script.sh # now owned by root:root sudo chown webliance:webliance /home/webliance/script.sh |
Quick Reference Table – Ownership Commands
| Goal | Command Example | Who can run it? | Notes |
|---|---|---|---|
| Change user owner | sudo chown rahul file.txt | Usually root/sudo | Rare for normal users |
| Change group owner | chown :developers folder/ | Owner or sudo | Common for teams |
| Change both | sudo chown user:group file | sudo | — |
| Recursive (folder + contents) | sudo chown -R www-data:www-data /var/www/ | sudo | Very common for servers |
| Check your groups | groups or id -Gn | Anyone | See primary group |
| See current ownership | ls -l or stat file.txt | Anyone | stat shows more detail |
Teacher’s Golden Rules
- Normal users can only change group to groups they belong to (or take away group write if they want)
- Only root/sudo can change user owner to someone else
- Always use -R when changing folders — otherwise only top level changes
- After chown, you often need to fix permissions too (chmod)
- Never make important system files owned by normal users — can break things
Got it, boss? Ownership + permissions = the full security system of Linux. Ownership says who gets to apply the rwx rules.
Any confusion? Want next: “Teacher, explain umask (default permissions when creating files)” or “setuid/setgid/sticky bit” or “how groups work in detail” or practice exercises?
Just say — teacher is ready in Hyderabad! Keep checking ls -l and whoami every day! 🐧👤🔑 😄
