Chown command in Linux - Change Owner (Files and Directories)+ Examples

Channel: Linux
Abstract: error". The following chown command change the owner and group for the symbolic link named symlink1 and its target file it's pointing to. sudo chown -

In UNIX / Linux systems, all files - including directories - bear specific permissions rights and are associated with a user and a group.

Chown stands for change ownership, is a command that is used in Linux systems for setting or modifying the ownership of files and directories, including symbolic links.

In this tutorial, we learn about chown command in Linux and how to use it with a few examples.

Prerequisites
  • A Linux or UNIX-like system.
  • Basic understanding of the Linux command line.
  • A login user with sudo privileges.
How to use chown command

chown command is used to change owner or group or both for the file system files or directories. You may also use chgrp which is specifically used to change group ownership. Whereas the chmod is used to change file permissions.

The Linux chown command takes the following syntax.

chown OPTIONS USER{:GROUP} file(s)
USERThis points to the username or userID ( UID ) of the new regular user that owns the file or directory.GROUPThis is the new group name or group ID ( GID ) that will be associated with the file or directory.FILEIs the name of the files(s) or directories or links that will take up the new ownership.

A few points to note:

  1. When you specify the user only without including the group, then the specified user assumes ownership of the file(s) and the group ownership remains unchanged.
  1. When the username only is specified followed by a full colon ( USER: ) and the group name is left out, the user takes ownership of the file(s), and the file’s group changes to that of the user's login group.
  1. If both the username and group are specified ( USER:GROUP) The user ownership of the file changes to that of the new user while the group ownership changes to the specified new group.
  1. When the username is omitted and the group name is preceded by a full colon ( :GROUP ), the user ownership of the file remains unchanged while the group ownership changes to that of the specified new group.
  2. When only a full colon is specified (:) Nothing changes.

For example to change the owner of the file to a specific user named as tom and the group as developers for the file program.php, type:

chown tom:developers program.php

You cannot change ownership of a file even if you own that file without root or sudo access. You may change the group to another group if you are a member of it.

You may get an "Operation not permitted" error message if you try to chown a file as a normal user.

For instance, if user jack is a member of two groups ( jack and users ), he can change a file belonging to the group named 'jack' to the 'users' group and not any other group. 

That's said, a privileged user ( sudo user or root )  can change both the user and group ownership of a file(s) and directory.

How to check file or directory ownership

Type ls -l command to view files and directory ownership as well as Linux permissions.

ls -l filename

To demonstrate this, we will create a sample file.

touch file1.txt

To check the file owner and group owner, we will run the command:

ls -l file1.txt

In the ls -l output, the second column shows the file owner and the third column shows the group owner of the file.

By default, a file created by the user takes the user and group ownership that the user belongs to. 

Change File Ownership

To change the owner of a file,  type chown command as follows followed by the user ( or numeric user id ) and the filename.

sudo chown USER FILE

For example, to change the current owner the file file1.txt  to a regular user called jack,  execute the command:

sudo chown jack file1.txt

Type ls -l command to confirm the file owner.

To change the owner of given files or directories, simply list them in one line separated by a space.

For example, to change the owner of the file file1.txt and directory mydir1 to a user named named jack, type:

sudo chown jack file1.txt mydir1

You can also use the UID ( User ID ) in the place of the username. Here, the UID 1003 corresponds to the owner or user called jack.

sudo chown 1003 file1.txt
Change only Group Ownership

To change only the group (or numeric group id) of a file while retaining user ownership, omit the user and only provide the new group name prefixed by a full colon with no spaces in between, followed by the name of the file(s). 

sudo chown :GROUP FILE

For example, the command shown sets the group of file file2.txt  to users group.

sudo chown :users file2.txt

An alternative command to change the group ownership of a file is chgrp command.

Change Both Owner and Group Ownership

To change both the user and the group of a file,  specify both the user and group separated by a full colon ( : ) without any spaces followed by the file name.

sudo chown  USER:GROUP file

In the following example, the chown command  changes the ownership of the file called file2.txt to a new owner named as as jack and a new group named users.

sudo chown jack:users file2.txt

If the group name is left out or not provided, the file’s group ownership is automatically set to the user’s login group.

sudo chown jack: file2.txt
Chown Recursively

Chown use -R (--recursive) option to recursively change file and directories ownerships of the user, group, or both under a directory.

sudo chown -R  USER:GROUP directory

This will effectively change ownership for multiple files and subdirectories to a new owner and group name.

For example to recursively change owner to www-data and group to webadmins for all the files and directories under /var/www/html:

sudo chown -R www-data:webadmins /var/www/html

If there are symbolic links in the directory, use the -h option can change the ownership of a symlink:

sudo chown -Rh www-date: /var/www/html
How to use the owner and a group of a reference

Using the --reference=ref_file option, you can modify the user and group ownership of certain files to correspond to those of a specified referenced file (ref_file). If the reference file is a symlink, then chown will use the target file's user and group ownership.

sudo chown --reference=REFERENCE_FILE FILE

Here, the user and group ownership of the file1.txt has been assigned to the file file2.txt.

sudo chown --reference=file1.txt file2.txt
Chown Symbolic Links

By default, chown changes the target of the symbolic link. That means it changes the file or directory the link is pointing to.

If you want to change the ownership of the link itself, use the option -h with chown. If you attempt to change the user or group of the actual link without -h option, the system gives the following error "cannot dereference ‘symlink1’: Permission denied」 error".

The following chown command change the owner and group for the symbolic link named symlink1 and its target file it's pointing to.

sudo chown -h www-data:www-data symlink1
Conclusion

In this tutorial, we learned about chown command to change the file's ownership to both user and group. For more information, you can visit chown manual pages or simply type from your terminal man chown or chown --help.

Thanks for reading, please leave your suggestions and feedback.

Ref From: linuxopsys

Related articles