chown

 Here's a complete guide to the chown command in Linux, covering:

  • What it is

  • How it works

  • Syntax

  • Hands-on examples

  • Edge cases

  • 20 FAANG-level interview Questions & Answers


🔧 What is chown?

The chown (change owner) command changes the ownership of a file, directory, or symbolic link.

It can change:

  • User ownership

  • Group ownership

  • Or both at the same time.


🧠 Syntax

chown [OPTIONS] USER[:GROUP] FILE...


Examples:

  • chown prakash file.txt → change owner to prakash

  • chown prakash:admin file.txt → change owner to prakash, group to admin

  • chown :admin file.txt → change group only


🧪 Practical Hands-On Examples

1. Change ownership of a file

chown prakash report.txt


Sets the user owner of report.txt to prakash.


2. Change user and group

chown prakash:devops script.sh


Changes the file script.sh to be owned by user prakash and group devops.


3. Change group only

chown :devops log.txt



4. Change ownership recursively

chown -R prakash:devops /home/prakash/project/


Changes ownership for all files and directories inside /home/prakash/project.


5. Use --from to change owner only if current owner matches

chown --from=root prakash file.txt


Changes owner only if the current owner is root.


6. Symbolic link behavior

chown prakash symlink


Changes the ownership of the target file, not the link.

To change the link itself:

chown -h prakash symlink



7. Change ownership using UID and GID

chown 1001:1002 file.txt



⚠️ Edge Cases and Pitfalls

Situation

Behavior

File doesn’t exist

Returns error

Non-existent user/group

Returns: invalid user or invalid group

Lack of sudo/root

Only root can change ownership to another user

Symbolic links

By default, it follows the link and changes the target

Files in use

Can still change ownership, but service behavior may vary

Immutable files

Ownership cannot be changed unless the immutable bit is removed (chattr -i)


🔍 Ownership Check Before and After

ls -l file.txt



🔐 Permissions Required

Only root or a user with CAP_CHOWN capability can:

  • Change file ownership

  • Change to another user


🧠 20 FAANG-Level Questions & Answers on chown


🔸 Q1. What’s the difference between chown and chmod?

A:

  • chown: Changes owner and group

  • chmod: Changes file permission bits (read/write/execute)


🔸 Q2. How do you change only the group of a file?

chown :groupname file



🔸 Q3. Can a non-root user change ownership of their own file to another user?

A: No, only root can change ownership to another user.


🔸 Q4. How to recursively change ownership of a directory and its contents?

chown -R user:group /path/



🔸 Q5. What happens when you run chown -h symlink?

A:
Changes the ownership of the symbolic link itself, not the target.


🔸 Q6. How can chown --from help in scripts?

A:
Ensures you only change ownership if the current owner matches, avoiding accidental overwrites.


🔸 Q7. How can you prevent chown from following symlinks recursively?

chown -hR user:group directory



🔸 Q8. What happens if chown is run on a mounted NFS filesystem?

A:

  • May fail depending on NFS server settings.

  • root_squash may map root to nobody.


🔸 Q9. How can find and chown be used together to change specific files?

find . -type f -name "*.log" -exec chown prakash:logs {} \;



🔸 Q10. Can you use chown with wildcards?

chown prakash:devops *.sh


✅ Yes, if the shell expands the pattern before passing it to chown.


🔸 Q11. How to log what chown is changing?

chown -v prakash file.txt



🔸 Q12. Why might chown silently fail in a Docker container?

A:

  • You may lack capabilities

  • Files are owned by read-only volume

  • UID/GID mismatch in base image


🔸 Q13. Can chown be used to remove group ownership?

A:
No. But you can set to a neutral group or use chmod to restrict group access.


🔸 Q14. How to debug chown failures on immutable files?

lsattr file.txt

chattr -i file.txt

chown prakash file.txt



🔸 Q15. Explain a real-world CI/CD scenario for using chown

A:
When building Docker containers, changing ownership of /app can prevent permission issues for apps running under a non-root user.


🔸 Q16. How does chown affect ACLs (Access Control Lists)?

A:
It changes the owning user but does not affect extended ACLs unless explicitly modified.


🔸 Q17. How do you use chown to match UID/GID of a remote system?

chown 501:1000 file.txt



🔸 Q18. Why use sudo chown in automation scripts?

A:
Ensure ownership is correct for system processes (like nginx writing to /var/www), and prevent permission denied issues.


🔸 Q19. Can you use chown inside a GitHub Action or Jenkins pipeline?

A:
Yes, to ensure build artifacts are owned by the runner user.


🔸 Q20. What’s the security risk of misusing chown?

A:
Changing ownership to a less privileged user might leak sensitive files, or allow malicious users to tamper with system binaries.


✅ Summary

Feature

Command

Change owner

chown user file

Change owner & group

chown user:group file

Change group only

chown :group file

Recursive

chown -R user:group dir/

Symlink target

chown user link

Symlink itself

chown -h user link

Verbose

chown -v user file

Safe match

chown --from=root user file


Would you like a PDF cheat sheet, Notion doc, or interactive terminal challenge set for this?


Distributed by Gooyaabi Templates | Designed by OddThemes