26 C
Jaipur
Friday, October 23, 2020

How to Change File/Group Owner with chown Command in Linux

Must read

huawei: Huawei ekes out third-quarter revenue growth as US restrictions bite – Latest News

Huawei Technologies Co Ltd eked out a gain in third-quarter revenue as the impact of the COVID-19 pandemic added to supply-chain difficulties brought about...

MEE Audio launches new range of Bluetooth audio accessories

MEE Audio has released a new range of audio products at different price points. There’s a new ‘MEE Audio Connect

Accenture along with SAP aims to take businesses into cloud-based open industry solutions

Accenture has announced it will be working side-by-side with SAP to help companies change their business operations with industry-specific solutions based on SAP’s...

huawei: Italy vetoes 5G deal between Fastweb and China’s Huawei: Sources – Latest News

Italy has prevented telecoms group Fastweb from signing a supply deal with Huawei for its 5G core network, two sources close to the matter...

Short for change ownership, Chown command is a command-line utility that is used to change the user or group ownership of a file or directory and even links. The Linux philosophy is such that every file or directory is owned by a specific user or group with certain access rights.

Using different examples, we will try and see the various use cases of the chown command. Chown command employs quite a simple and straight forward syntax.

$ chown OPTIONS USER: GROUP file(s)

Let’s briefly flesh out the parameters:

The attribute USER refers to the username of the user that will own the file. You can specify either the username or the UID ( User ID). Meanwhile, the GROUP option indicates the name of the new group that the file will acquire after running the command. The file option represents a regular file or a directory or even a symbolic link. These are the three entities whose permissions can be altered.

A few points to note:

1) When the  USER option is specified alone, ownership of the file/directory changes to that of the specified user while the group ownership remains unchanged. Here’s an example:

$ chown john file1.txt

In the above command, user ownership of the file file1.txt changes from the current user to the user john.

2) If the  USER option is proceeded by a full colon i.e. USER: and the group name is not provided, then the user takes ownership of the file but the file’s group ownership switches to the user’s login group. For example:

$ chown john: file1.txt

In this example, the user john takes ownership of the file file1.txt, but the group ownership of the file changes to john’s login group.

3) When both the user and group options are specified separated by a colon i.e USER:GROUP  – without any spaces therein – the file takes ownership of the new user and the group as specified

$ chown john:john file1.txt

In the above example, the file takes the user and group ownership of user john.

4)  When the USER option is left out and instead the group option is preceded by the full colon :GROUP then, only the group ownership of the file changes.

How to view file permissions

To view file permissions, simply use the ls -l command followed by the file name

$ ls -l filename

For example:

$ ls -l file1.txt

From the output, we can see that the file is owned by user linuxtechi which and belongs to the group linuxtechi in the 3rd and 4th columns respectively.

How to change file owner with chown command

Before changing permissions, always invoke sudo if you are not working as the root user. This gives you elevated privileges to change user and group ownership of a file.

To change file ownership, use the syntax:

$ sudo chown user filename

For example,

$ sudo chown james file1.txt

From the output, you can clearly see that the ownership of the file has changed from linuxtechi to user james.

Alternatively, instead of using the username, you can pass the UID of the user instead. To get the UID, view the /etc/passwd file.

$ cat /etc/passwd | grep username

From the example below, we can see that the UID of user linuxtechi is 1002

To change the file ownership back to linuxtechi user, we shall execute the command:

$ sudo chown 1002 file1.txt

How to change the group owner with chown command

As earlier discussed, to change the group owner of a file, omit the user and simply prefix the group name with a full colon.

$ sudo chown :group file

For example, to change the group owner of file1.txt from linuxtechi to docker, we executed the command:

$ sudo chown :docker file1.txt

How to change both file owner and group owner of a file

If you want to change both the owner and group that a file belongs to, specify both the user and group options separated by a full colon as shown in the syntax below. Be sure that there are no spaces between the options and the colon.

$ sudo chown user:group filename

For example, the following command changes the ownership of the file file1.txt  to user james and group redis as verified using the ls command.

$ sudo chown james:redis file1.txt

How to recursively change file ownership

When applying permissions to directories, you might want to apply changes recursively i.e make the ownership changes to descend and apply to files and sub-directories. To achieve this, user the recursive option -R or –recursive directive.

$ sudo chown -R user:group directory

For example, the command below assigns all files and folders in the /var/www directory ownership to the www-data group.

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

The example below assigns ownership of the directory reports alongside all the files and folders in the directory to the user linuxtechi.

$ sudo chown -R linuxtechi reports

How to change ownership using a reference file

Lastly, there’s a nifty way that you can use to change ownership of a file, and that is by using a reference file. Using the chown command, you can change the user and group ownership of a file using another file as the point of reference.

The syntax is shown below:

$ chown –reference=ref_file file

Suppose you want to assign user and group ownership of file1.txt to another file file2.txt. How would you go about it? This is illustrated in the command below.

$ chown --reference=file1.txt file2.txt

The output above confirms that file2.txt inherits the user and group ownership of file1.txt.In the command, file1.txt is the reference file.

Conclusion

Chown command is a powerful tool that is used for managing file and directory ownership.  For additional information, check out the chown man pages.

$ man chown

Also Read : Linux Zip and Unzip Command with Examples

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

huawei: Huawei ekes out third-quarter revenue growth as US restrictions bite – Latest News

Huawei Technologies Co Ltd eked out a gain in third-quarter revenue as the impact of the COVID-19 pandemic added to supply-chain difficulties brought about...

MEE Audio launches new range of Bluetooth audio accessories

MEE Audio has released a new range of audio products at different price points. There’s a new ‘MEE Audio Connect

Accenture along with SAP aims to take businesses into cloud-based open industry solutions

Accenture has announced it will be working side-by-side with SAP to help companies change their business operations with industry-specific solutions based on SAP’s...

huawei: Italy vetoes 5G deal between Fastweb and China’s Huawei: Sources – Latest News

Italy has prevented telecoms group Fastweb from signing a supply deal with Huawei for its 5G core network, two sources close to the matter...

huawei: Huawei reports 9.9% revenue growth in first 3 quarters of 2020 – Latest News

Huawei on Friday said that it generated a revenue of 671.3 billion yuan ($98.57 billion) in the first three quarters of this year, an...