How to change the file owner and group in Linux?

LinuxOperating SystemOpen Source

To change the file owner and group, we use the chown command in the Linux operating system.

We know that Linux is a multiuser operating system so every file or directory belongs to an owner and group.

To change ownership of files or directories we use chown command in the Linux system. This command is also available in the IBM i operating system. The chgrp command is also used to change only the group ownership of the file in the Linux system.

Syntax

The general syntax of the chown command is as follows

chown [OPTION]... [OWNER] [: [GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...

A brief description of options available in the chown command -

Sr.No.option & Description
1-c, --changes
Gives a diagnosis for all the files that actually changed.
2.-f, --silent, --quite
It suppresses most of the error messages.
3-v, --verbose
Give a diagnosis for all the processed files.
4-R, --recursive
It changes files and directories recursively.
5--help
Displays a help message and then exits.
6--version
It gives info about the version and then exits.

Checking the ownership

To check the ownership of a file in the Linux system, we use the ls -l command as shown below.

$ ls -l <file name>

Changing the ownership of a file

To change ownership of a file in the Linux system we need to administrative permission or sudo privilege.

The general syntax for changing ownership of a file is as follows:

$ sudo chown <owner name> <file name>

Here, we will change the ownership of a file ‘file.txt’ Vikash to Gautam using the chown command.

First, we will check the ownership of ‘file.txt’ using the below command.

vikash@tutorialspoint:~/shadow$ ls -l file.txt
-rw-rw-r-- 1 vikash vikash 34 Jan 11 20:59 file.txt
vikash@tutorialspoint:~/shadow$

We can see that the owner of ‘file.txt’ is vikash and the group ownership of the ‘file.txt’ is vikash. To change the ownership, we will execute command as shown below.

vikash@tutorialspoint:~/shadow$ sudo chown gautam file.txt
[sudo] password for vikash:
vikash@tutorialspoint:~/shadow$

After changing the ownership of the file, we will again check again the ownership of files to ensure that ownership is changed or not.

vikash@tutorialspoint:~/shadow$ ls -l file.txt
-rw-rw-r-- 1 gautam vikash 34 Jan 11 20:59 file.txt
vikash@tutorialspoint:~/shadow$

To check more information and available options in the chown command, we use the --help option with the chown command as follows:

$ chown --help

Conclusion: In this article, we learned to change the ownership of files using the chown command in the Linux operating system with available options and suitable examples. To change only the group ownership of the file, we use the chgrp command in the Linux system.

raja
Published on 30-Jun-2021 10:24:30
Advertisements