Linux chown command


The chown command in Linux is used to change the ownership of files and directories. Ownership includes the user (owner) and group associated with the file or directory, which determines who can access or modify them based on set permissions.

Basic Syntax

chown [options] user:group file_or_directory
  • user: Specifies the new owner.
  • group: Specifies the new group. (If omitted, only the owner is changed.)
  • file_or_directory: The target file or directory.

Examples with Output

Example 1: Changing the Owner of a File

The following command changes the owner of a file to newuser.

Command
sudo chown newuser file.txt ls -l file.txt
Output
-rw-r--r-- 1 newuser user 1024 Oct 29 10:00 file.txt

Explanation:

  • sudo chown newuser file.txt changes the owner to newuser.
  • After running ls -l, you can see that newuser is now the file’s owner.

Example 2: Changing the Owner and Group of a File

In this example, we change both the owner and the group of file.txt to newuser and newgroup respectively.

Command
sudo chown newuser:newgroup file.txt ls -l file.txt
Output
-rw-r--r-- 1 newuser newgroup 1024 Oct 29 10:00 file.txt

Explanation:

  • sudo chown newuser:newgroup file.txt changes the owner to newuser and the group to newgroup.
  • The ls -l output shows that both the owner and group have been updated.

Example 3: Changing the Group Only

To change only the group, you can specify :newgroup without mentioning a new user.

Command
sudo chown :newgroup file.txt ls -l file.txt
Output
-rw-r--r-- 1 user newgroup 1024 Oct 29 10:00 file.txt

Explanation:

  • sudo chown :newgroup file.txt changes only the group to newgroup, leaving the user unchanged.
  • The ls -l output shows the updated group.

Example 4: Recursively Changing Ownership of a Directory

The -R (recursive) option changes ownership for a directory and all its files and subdirectories.

Command
sudo chown -R newuser:newgroup /path/to/directory ls -l /path/to/directory
Output
drwxr-xr-x 2 newuser newgroup 4096 Oct 29 10:00 subdir -rw-r--r-- 1 newuser newgroup 1024 Oct 29 10:00 file1.txt -rw-r--r-- 1 newuser newgroup 2048 Oct 29 10:00 file2.txt

Explanation:

  • sudo chown -R newuser:newgroup /path/to/directory changes the ownership of /path/to/directory, all files inside, and subdirectories to newuser and newgroup.
  • The ls -l output shows that all contents now belong to newuser:newgroup.

Example 5: Verifying Ownership Changes

The ls -l command provides a quick way to verify changes. Alternatively, stat can show detailed ownership information.

Command
stat file.txt
Output
File: file.txt Size: 1024 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 393527 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1001/newuser) Gid: ( 1002/newgroup)

Explanation:

  • stat file.txt shows detailed information, confirming the UID (user ID) and GID (group ID) after ownership changes.

Summary of Common Commands

  • sudo chown newuser file.txt: Changes the owner to newuser.
  • sudo chown newuser:newgroup file.txt: Changes the owner to newuser and group to newgroup.
  • sudo chown :newgroup file.txt: Changes only the group to newgroup.
  • sudo chown -R newuser:newgroup directory: Recursively changes ownership for directory and all its contents.

The chown command is essential for managing file and directory ownership, providing security and ensuring appropriate access across users and groups.