last modified March 3, 2025
The chown command in Linux is used to change the ownership of files
and directories. It allows you to modify the user and group associated with a
file or directory. This tutorial covers basic and advanced usage of
chown with practical examples.
chown is commonly used for managing file permissions and ensuring
proper access control in multi-user environments.
This example demonstrates how to change the owner of a file.
chown newowner filename.txt
The chown command changes the owner of filename.txt to
newowner.
This example shows how to change both the owner and group of a file.
chown newowner:newgroup filename.txt
The chown command changes the owner to newowner and the
group to newgroup for filename.txt.
This example demonstrates how to change ownership for all files in a directory.
chown -R newowner:newgroup /path/to/directory
The -R option applies the ownership change recursively to all files
and subdirectories within /path/to/directory.
This example shows how to change only the group of a file.
chown :newgroup filename.txt
The chown command changes the group of filename.txt to
newgroup without altering the owner.
This example demonstrates how to change ownership using user and group IDs.
chown 1001:1002 filename.txt
The chown command changes the owner to the user with UID
1001 and the group to the group with GID 1002.
This example shows how to prevent changing ownership of root-owned files.
chown --preserve-root newowner:newgroup /path/to/directory
The --preserve-root option prevents chown from
modifying the ownership of the root directory.
-R carefully to avoid unintended modifications.ls -l to check ownership before and after changes.--preserve-root to avoid accidental changes to system files.
In this article, we have explored various examples of using the chown
command for changing file and directory ownership, including recursive changes
and preserving root ownership.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Linux tutorials.