,

Top 10 Commands with explanation of “chown” command

Posted by

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

The chown (Change Owner) command in Linux is used to change the owner and group ownership of files and directories. It is particularly useful when managing permissions for multiple users on a system.

Every file in Linux has an owner and a group, as seen in the ls -l output:

-rw-r--r-- 1 user1 group1 1234 Jan 30 12:00 file.txt
  • user1 โ†’ Owner
  • group1 โ†’ Group

Now, let’s explore 10 powerful chown commands with practical explanations.


1. Change the Owner of a File

chown newuser file.txt

โœ” What it does:

  • Changes the owner of file.txt to newuser.
  • The group remains unchanged.

๐Ÿ“Œ Example Before (ls -l file.txt):

-rw-r--r-- 1 olduser group1 1234 Jan 30 12:00 file.txt

๐Ÿ“Œ Example After:

-rw-r--r-- 1 newuser group1 1234 Jan 30 12:00 file.txt

๐Ÿ’ก Useful when transferring file ownership to another user.


2. Change the Owner and Group of a File

chown newuser:newgroup file.txt

โœ” What it does:

  • Changes owner to newuser and group to newgroup.

๐Ÿ“Œ Before:

-rw-r--r-- 1 olduser oldgroup file.txt

๐Ÿ“Œ After:

-rw-r--r-- 1 newuser newgroup file.txt

๐Ÿ’ก Ensures both owner and group are modified in one command.


3. Change Only the Group of a File

chown :newgroup file.txt

โœ” What it does:

  • The owner remains unchanged, but the group is changed.

๐Ÿ“Œ Before:

-rw-r--r-- 1 user1 oldgroup file.txt

๐Ÿ“Œ After:

-rw-r--r-- 1 user1 newgroup file.txt

๐Ÿ’ก Equivalent to the chgrp command.


4. Change Owner and Group for Multiple Files

chown newuser:newgroup file1.txt file2.txt

โœ” What it does:

  • Modifies both file1.txt and file2.txt.

๐Ÿ“Œ Before:

-rw-r--r-- 1 user1 group1 file1.txt
-rw-r--r-- 1 user1 group1 file2.txt

๐Ÿ“Œ After:

-rw-r--r-- 1 newuser newgroup file1.txt
-rw-r--r-- 1 newuser newgroup file2.txt

๐Ÿ’ก Saves time when modifying multiple files at once.


5. Change Owner for a Directory and Its Contents

chown -R newuser:newgroup mydir/

โœ” What it does:

  • -R โ†’ Applies changes recursively to all files and subdirectories in mydir/.

๐Ÿ“Œ Before:

drwxr-xr-x 1 user1 group1 mydir/
-rw-r--r-- 1 user1 group1 file1.txt
-rw-r--r-- 1 user1 group1 file2.txt

๐Ÿ“Œ After:

drwxr-xr-x 1 newuser newgroup mydir/
-rw-r--r-- 1 newuser newgroup file1.txt
-rw-r--r-- 1 newuser newgroup file2.txt

๐Ÿ’ก Useful when transferring ownership of a project directory.


6. Change Ownership Based on Another Fileโ€™s Ownership

chown --reference=referencefile targetfile

โœ” What it does:

  • Sets targetfile ownership to match referencefile.

๐Ÿ“Œ Example:

chown --reference=existing.txt newfile.txt
  • newfile.txt gets the same owner and group as existing.txt.

๐Ÿ’ก Quick way to match file ownership settings!


7. Change Ownership Using User ID (UID) and Group ID (GID)

chown 1001:1002 file.txt

โœ” What it does:

  • 1001 โ†’ User ID (UID)
  • 1002 โ†’ Group ID (GID)

๐Ÿ“Œ Before:

-rw-r--r-- 1 user1 group1 file.txt

๐Ÿ“Œ After (ls -n file.txt to check UIDs/GIDs):

-rw-r--r-- 1 1001 1002 file.txt

๐Ÿ’ก Helpful in scripts where UID/GID is known but not the username/group name.


8. Change Ownership for Files Owned by a Specific User

chown newuser:newgroup $(find /path -user olduser)

โœ” What it does:

  • Finds all files owned by olduser and transfers them to newuser:newgroup.

๐Ÿ“Œ Example: Change all files owned by john under /home/projects:

chown newuser:newgroup $(find /home/projects -user john)

๐Ÿ’ก Useful when deleting a user and reassigning files.


9. Prevent Symbolic Link Issues with -h

chown -h newuser:newgroup symlink

โœ” What it does:

  • Changes the owner of a symbolic link, not the target file.

๐Ÿ“Œ Without -h (ls -l symlink output):

lrwxrwxrwx 1 olduser oldgroup symlink -> targetfile

๐Ÿ“Œ After using -h:

lrwxrwxrwx 1 newuser newgroup symlink -> targetfile

๐Ÿ’ก Useful when dealing with symbolic links in /etc or /usr/local/bin/.


10. Restrict Ownership Transfer to Superuser Only

chown root file.txt

โœ” What it does:

  • Transfers file ownership to root.
  • Can only be executed by a superuser (root).

๐Ÿ“Œ Before:

-rw-r--r-- 1 user1 group1 file.txt

๐Ÿ“Œ After (ls -l output):

-rw-r--r-- 1 root group1 file.txt

๐Ÿ’ก Important for securing critical system files.


Final Thoughts

  • chown is essential for managing file ownership and permissions.
  • Always use -R carefully, especially on /etc/, /home/, or /var/ directories.
  • Changing ownership to root can restrict user access.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x