kazmax - Home Server on Linux

Change ownership and groups of files and directories - chown

To change the owner and group of a file or directory, use the "chown" command. This page describes how to use the "chown" command.

Last Update : October 06, 2020

Change ownership and groups of files and directories - chown

  1. How to use the chown command
  2. Example of using the chown command

1. How to use the chown command

Usage

chown [option]... OWNER[:[GROUP]] FILE...

Main Options

Options Meaning
-R, --recursive Recursively changes the ownership of a file or directory; executes the command to all files and folders under the directory specified in FILE.
-c, --changes The actual behavior of the change is shown in detail.

The chown command allows you to change the owner or group of the file or directory specified in the FILE.

The OWNER section can be either a user name or a user ID (numerical value).

You can use ": (colon)" to separate them and specify GROUP at the same time.

2. Example of using the chown command

Change the owner from user1 to user2

$ ls -l
total 4
-rw-r--r-- 1 user1 group1 0 Jul 22 08:50 test.txt

$ chown user2 test.txt
$ ls -l
total 4
-rw-r--r-- 1 user2 group1 0 Jul 22 08:50 test.txt ← The owner changed to user2

Change the owner to user2 and the group to group2

$ chown -c user2:group2 test.txt
changed ownership of `hoge.txt' to user2:group2 ← The "-c" option displays the changes.

$ ls -l
total 4
-rw-r--r-- 1 user2 group2 0 Jul 22 08:50 test.txt ← The owner has changed to user2 and the group has changed to group2.

Notes

About The delimiters ": (colon)" and ". (Dot)" between OWNER and GROUP

The separator between OWNER and GROUP is ": (colon)", but ". (dot)" is also possible. In the old days, ". (dot) was more standard.

Since it is now possible to include ". (Dot)" in the user name or group name, "chown" will behave strangely when changing to such a user or group. It is better to use ": (colon)" if possible, because it can be changed successfully.

Notes

With the chown command, you can change only the group if you omit OWNER and write it from ": (colon)".

 ls -l
total 4
-rw-r--r-- 1 user1 group1 0 Jul 22 08:50 test.txt

$ chown :group2 test.txt ← Omit the OWNER part.
$ ls -l
total 4
-rw-r--r-- 1 user1 group2 0 Jul 22 08:50 test.txt ← Only the groups are changed.

Notes

chown user1: test.txt 

If there is a colon or dot following the user's name but no group name, the group in the file will be changed to the user's primary group, as shown above. The user will be the one specified.

$ ls -l
total 4
-rw-r--r-- 1 user1 group1 0 Jul 22 08:50 test.txt

$ id user2
uid=503(user2) gid=505(group2) groups=505(group2)
↑ The primary group for "user2" is "group2".

$ chown user2: test.txt ← Omit the ": (colon)" after it.
$ ls -l
total 4
-rw-r--r-- 1 user2 group2 0 Jul 22 08:50 test.txt
↑ The group of the file becomes the primary group of user2.