The chown
command in Linux is used to modify the owner and group of files, directories and links. By default, the owner of a filesystem object is the user that created it. The group is a set of users that share the same access permissions (i.e., read, write and execute) for that object.
Syntax
Syntax of chown command is
chown [OPTIONS] USER[:GROUP] FILE(s)
- OPTIONS – Optional argument
- USER – Username or the numeric user ID (UID) of the new owner
- :GROUP – Used only when changing group.
- FILE – List of file
Option -R
operates on filesystem objects recursively. That is, when used on a directory, it can change the ownership and/or group of all objects within the directory tree beginning with that directory rather than just the ownership of the directory itself.
To get the user id, execute below command in terminal
id -u USERNAME
Change Owner
Following command change the ownership of a file hello.txt from root to the user test.
chown test hello.txt
Similarly following would transfer the ownership of a file named file1 and a directory named dir1 to a new owner named test:
chown test file1 dir1
In order to perform the above command, most systems are configured by default to require access to the root account.
Change Group
To change only the group of a file use the new group name followed by a colon (:). Following command will change the owning group of a file named file1 to www-data:
chown :www-data file1
The owner and group can be changed simultaneously by combining owner and group in single command. Specify the name or UID of the new owner followed directly by colon. Next specify the name or numeric ID of the new group after colon. For example, below command will change the owner of a file named file2 to the user with the user name test and change its group to group2:
chown test:group2 file2
Transfer Ownership
To use the owner and a group of a reference file, add the --reference
option. Now chown
command will copy the settings from one file (ReferenceFILE) to another file (FILE):
chown --reference=ReferenceFILE FILE