How to Change File Ownership in Linux

In Linux, managing file ownership is essential for system security and access control. Every file and directory is assigned an owner and an owning group, which dictate who can read, modify, or execute them. This guide explains how to quickly change user and group ownership of files and directories using the standard Linux command-line tool, chown.

Understanding the chown Command

The primary utility for modifying file ownership is chown (short for "change owner"). Because changing ownership typically affects system security, you usually need administrative privileges, meaning commands are often preceded by sudo.

The basic syntax is:

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

Changing the User Owner

To change only the user who owns a file, specify the new username followed by the file's path:

sudo chown alice document.txt

In this example, the user alice becomes the new owner of document.txt, while the file's existing group ownership remains unchanged.

Changing Both User and Group Ownership

To change both the owner and the group at the same time, separate the user and group names with a colon (:):

sudo chown alice:developers document.txt

This sets the owner to alice and the group to developers.

Changing Only the Group Owner

To modify only the group ownership using chown, prefix the group name with a colon and omit the username:

sudo chown :developers document.txt

Alternatively, you can use the dedicated chgrp command:

sudo chgrp developers document.txt

Changing Ownership Recursively for Directories

If you need to change the ownership of a directory and all files and subdirectories inside it, use the recursive flag -R:

sudo chown -R alice:developers /var/www/html

Apply caution when running recursive ownership changes, particularly in system directories, to avoid breaking operating system permissions.

Verifying the Changes

To verify that the ownership has been updated correctly, use the long-listing format with ls:

ls -l document.txt

The third and fourth columns of the output will display the current user owner and group owner, respectively.