chown Command Tutorial

The chown command is used to change user and group ownership on Linux and Unix systems. The owner of the files, directories can be changed in different ways by using the chown command. The chown name is the short form of the change ownership . The ownership of a file or directory can be only changed by the superuser named “root”. A regular user can not change the ownership of a file or directory even its owns it.

chown Command Syntax

The chown command has the following syntax where the ownership information is provided before the file and folder names.

chown OPTIONS USER:GROUP FILE_FOLDER
  • OPTIONS is optional and used to provide some options like recursive, verbosity etc.
  • USER is the user which is set the ownweship of the specified FILE_FOLDER. USER is required but can be omitted if the GROUP is specified.
  • GROUP is the group which is set the ownership of the specified FILE_FOLDER. GROUP can be omitted if the USER is specified.
  • FILE_FOLDER is the files and folders those owerships will be changed.

List Group and User Ownership

The ls command is used to list files and folders in a Linux system. The -l option can be provided to the ls command which also lists the user and group ownership information.

$ ls -l
List Group and User Ownership

From the output, we can see that all files are owner by the user “ismail” and by the group “ismail” too. The first column “ismail” is about user ownership and the second column “ismail” is about group ownership.

Change User Ownership Of a File

One of the most popular usages of the chown command is changing the user ownership of a file. In the following example, we set the user ownership of the “log.txt” file as “ismail”.

$ chown ismail log.txt

In Linux, every user has their own numeric User ID. We can use this User ID with the chown command in order to specify the owner. In the following example, we use the user “ismail” User ID which is “1000” to set “log.txt” file user ownership.

$ chown 1000 log.txt

Change User Ownership of Multiple Files

the chown command can be also used to set multiple files ownership information about the user. The file names are added after the user name. In the following example, we set user ownership as “ismail” for files “a.txt”, “b.txt”, “c.txt“.

$ chown ismail a.txt b.txt c.txt

Change User Ownership of Files and Directories Recursively

Recursive usage is very useful with the chown command where multiple files and directories user and group ownerships can be changed with a single command. Recursive ownership change works over all files and folders. The -R option is used to change files and folders ownership recursively. In the following example, we set the user and group ownership as “www-data” for all files and folders located under the “/var/www“.

$ chown -R www-data:www-data /var/www

Change Group Ownership

The chown command can be also used to change the group ownership of the file without changing the user ownership of it. Only the group owner is provided by prefixing it with a double colon. In the following example, we set the group ownership as “dbadmins” for the file “database.txt“.

$ chown :dbadmins database.txt

Change User and Group Ownership

The chown command can be also used to change user and group ownership of files with a single command. The owner user name and group name are provided by separating them with a double colon. In the following example, we set the “main.c” file user owner as “ismail” and the group owner as “developers“.

$ chown ismail:developers main.c

Reference A File Ownership Information

All files have user and group ownership information. We can use a file user and group ownership information to set another file user and group ownership. Simply we reference one file for ownership information to set another file. The --reference option is used to specify the file we reference. In the following example, we set the file “a.txt” as the reference for the user and group ownership to set “b.txt“.

$ chown --reference="a.txt" b.txt

Change Ownership Verbosely

By default, the chown command does not provide output about operations unless there is an error. But we can make things a bit loud by using the verbose option --verbose in order to do things verbosely. This prints every ownership change operation to the command line interface.

$ chown -R -v "ismail" nmap/
ownership of 'nmap/FingerPrintResults.cc' retained as ismail
ownership of 'nmap/nse_openssl.cc' retained as ismail
ownership of 'nmap/NmapOps.cc' retained as ismail
ownership of 'nmap/TargetGroup.cc' retained as ismail
ownership of 'nmap/nse_nsock.cc' retained as ismail
ownership of 'nmap/portreasons.h' retained as ismail
ownership of 'nmap/timing.cc' retained as ismail
ownership of 'nmap/liblinear/tron.cpp' retained as ismail
ownership of 'nmap/liblinear/predict.c' retained as ismail
ownership of 'nmap/liblinear/README' retained as ismail
ownership of 'nmap/liblinear/train.c' retained as ismail
ownership of 'nmap/liblinear/COPYRIGHT' retained as ismail
...

Change Ownership Silently

By default, the chown command does not provide output with successful operations but if there is an error this error is printed to the command-line interface. We can prevent these messages by running chown silently with the -f option.

$ chown -R -f "ismail" /var/www
See also  How To Chown Recursively In Linux?

Leave a Comment