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:

  1. User owner (the person/user who created it or last took ownership)
  2. 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:

Bash

Typical output:

text

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:

Bash

Example output for a normal user:

text

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)

Bash

→ Now rahul owns the file (you need sudo because you can’t give away ownership unless you’re root).

2. Change group owner

Bash

→ Now group = developers (team members in “developers” group can use group permissions)

3. Change both user and group at once

Bash

4. Change ownership of everything inside a folder (Recursive – Very Common!)

Use -R (capital R):

Bash

→ Makes all files & subfolders owned by web server user/group (needed for nginx/Apache to write/upload)

Bash

→ You and your team now have proper access

Real-Life Examples You’ll See in Hyderabad Life

Example 1: Website on server not working?

Bash

→ Nginx (runs as www-data) can read but not write → fix:

Bash

Example 2: Team project folder

Bash

→ Anyone in developers group can read/write/execute inside

Example 3: Secure your .ssh folder

Bash

→ Only you can access your keys

Example 4: You downloaded a file as root by mistake

Bash

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! 🐧👤🔑 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *