Bash Ownership

Master file and directory ownership with chown command

👤 What is File Ownership?

File ownership in Linux defines which user and group control a file or directory. The chown command changes ownership, allowing you to manage access control and security by assigning files to appropriate users and groups.


# Change file owner
chown user file.txt
                                    

Output:

Ownership changed successfully

Key Ownership Concepts

👤

User Owner

Change file owner user

chown john file.txt
👥

Group Owner

Change file group ownership

chown :developers file.txt
🔄

Both

Change user and group together

chown john:developers file.txt
📁

Recursive

Apply to directories recursively

chown -R user folder/

🔹 Change User Ownership

Use chown to change the user owner of a file or directory. Only the root user or file owner can change ownership. Specify the new username followed by the file path to transfer ownership to a different user account.

# Change owner to specific user
chown john document.txt

# Change owner with verbose output
chown -v alice report.pdf

# Change owner of multiple files
chown bob file1.txt file2.txt file3.txt

Output:

changed ownership of 'document.txt' from root to john

🔹 Change Group Ownership

Change only the group ownership by prefixing the group name with a colon. This assigns the file to a different group while keeping the user owner unchanged, useful for collaborative projects where multiple users need shared access.

# Change group only
chown :developers project.txt

# Change group with verbose output
chown -v :staff document.pdf

# Change group for multiple files
chown :admins file1.txt file2.txt

Output:

changed group of 'project.txt' from users to developers

🔹 Change User and Group Together

Change both user and group ownership simultaneously using the format user:group. This is efficient when setting up new files or transferring complete ownership to another user and their primary group in a single command operation.

# Change both user and group
chown john:developers file.txt

# Change with verbose output
chown -v alice:staff report.pdf

# Change for multiple files
chown bob:admins file1.txt file2.txt

Output:

changed ownership of 'file.txt' from root:root to john:developers

🔹 Recursive Ownership Change

Recursively changing ownership with chown -R applies modifications to a directory and all its contents. This is indispensable when transferring entire project folders between users, fixing ownership after a bulk file operation, or preparing a software installation directory for a specific service account. For example, chown -R www-data:www-data /var/www ensures a web server can access all site files. Use this powerful option with care, as incorrect ownership can render files inaccessible to legitimate users or processes, causing service failures.

# Change ownership recursively
chown -R john:developers project/

# Recursive with verbose output
chown -Rv alice:staff documents/

# Change entire directory tree
chown -R bob:admins /var/www/html/

Output:

changed ownership of 'project/' from root:root to john:developers
changed ownership of 'project/file1.txt' from root:root to john:developers
changed ownership of 'project/subfolder/' from root:root to john:developers

🔹 Copy Ownership from Reference File

Copy ownership settings from one file to another using the --reference option. This ensures consistent ownership across related files by matching the user and group from a reference file, useful when replicating permission structures.

# Copy ownership from reference file
chown --reference=original.txt newfile.txt

# Copy with verbose output
chown -v --reference=template.txt document.txt

# Apply reference to multiple files
chown --reference=master.txt file1.txt file2.txt

Output:

changed ownership of 'newfile.txt' to match 'original.txt'

🔹 View Current Ownership

Check current file ownership using the ls -l command. The output displays the owner and group in the third and fourth columns, helping you verify ownership before making changes or troubleshooting permission issues in your file system.

# View file ownership
ls -l file.txt

# View directory ownership
ls -ld folder/

# View detailed ownership info
ls -lh document.pdf

Output:

-rw-r--r-- 1 john developers 1024 Oct 10 14:30 file.txt

🔹 Change Ownership with Sudo

Most ownership changes require root privileges. Use sudo before chown commands to execute them with administrative rights. This is necessary when changing ownership of system files or files owned by other users for security and system integrity.

# Change ownership with sudo
sudo chown john:developers /var/www/file.txt

# Recursive change with sudo
sudo chown -R alice:staff /opt/application/

# Change system file ownership
sudo chown root:root /etc/config.conf

Output:

[sudo] password for user:
changed ownership of '/var/www/file.txt' to john:developers

🧠 Test Your Knowledge

Which option applies ownership changes recursively to directories?