Linux chown Command
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.
Change File Ownership
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.
Change File Ownership and Group
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.
Change Ownership Recursively
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.
Change Group Only
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.
Change Ownership Using UID and GID
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.
Preserve Root Ownership
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.
Best Practices for chown
- Use with Caution: Changing ownership can affect system security and functionality.
- Recursive Changes: Use
-Rcarefully to avoid unintended modifications. - Verify Ownership: Use
ls -lto check ownership before and after changes. - Preserve Root: Use
--preserve-rootto avoid accidental changes to system files.
Source
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.
Author
List all Linux tutorials.