chown and chgrp commands to change ownership of files and directories in Linux

The commands chown and chgrp are extensively used in linux systems to update the ownership and organize the file structure.

The chown command can be implemented in the following two types of notations.
1) User and group owner Symbolic notation
2) Reference operator

The options/filters will be common to them for the two kinds on notations, We will look at the practical and important options.
The chown command syntax:

chown <username> <data-directory> <files>
chown <username>:<groupname> <data-directory>
chown --reference <Source-reference-file| Source-reference-Directory> <data-directory>

The ownership of the files is a privileged operation and it requires a sudo access to the system
Lets start by running the chown command on the files you want to change the ownership
We have a file called jdk-8u141-linux-x64.rpm and its owned by root user.

[vamshi@linuxcent ~]$ ls -l jdk-8u141-linux-x64.rpm
-rw-r--r--. 1 root root 169980729 Jul 13 2017 jdk-8u141-linux-x64.rpm

We intend to change the ownership to user vamshi and we use chown command

[vamshi@linuxcent ~]$ sudo chown -v vamshi jdk-8u141-linux-x64.rpm 
changed ownership of ‘jdk-8u141-linux-x64.rpm’ from root to vamshi
[vamshi@linuxcent ~]$ ls -l jdk-8u141-linux-x64.rpm
-rw-r--r--. 1 vamshi root 169980729 Jul 13  2017 jdk-8u141-linux-x64.rpm

By this only the user part of ownership was modified but the group was uneffected.

NOTE: We are using the option -v to print the command output information in verbose mode.

We can use chgrp command to modify the group ownership as follows:

[vamshi@linuxcent ~]$ sudo chgrp -v vamshi jdk-8u141-linux-x64.rpm 
changed group of ‘jdk-8u141-linux-x64.rpm’ from root to vamshi

To change both the user owner and group at once, chown command can be used, as demonstrated below:

[vamshi@linuxcent ~]$ sudo chown -v vamshi:vamshi jdk-8u141-linux-x64.rpm 
changed ownership of ‘jdk-8u141-linux-x64.rpm’ from root:root to vamshi:vamshi

[vamshi@linuxcent ~]$ ls -l jdk-8u141-linux-x64.rpm
-rw-r--r--. 1 vamshi vamshi 169980729 Jul 13 2017 jdk-8u141-linux-x64.rpm

How do you recursively change the permissions within the destination sub-directories and files ?

chown command when used with -R applies the updated ownership recursively till the directory depth on destination.

[vamshi@linuxcent data]$ ls -ld redmine/
drwxr-xr-x. 3 root root 17 Apr 17 16:31 redmine/
[vamshi@linuxcent data]$ sudo chown vamshi:vamshi /mnt/data/redmine/ -R
[vamshi@linuxcent data]$ ls -ld /mnt/data/redmine/
drwxrwxr-x. 3 vamshi vamshi 17 Apr 17 16:31 redmine-4.0.6

How to apply reference permissions to chown command in Linux ?

We shall be using the option --reference which takes the reference from the existing file and applies the permission

Suppose we directory called djangoproject1.
The root user copied the contents from elsewhere and placed  them in the location /home/alice .
Now the user alice has to have the user ownership to them in-order to work with djangoproject1.

[root@node02 alice]# ls -ld djangoproject1
drwxrwxr-x. 3 root root 108 Apr 16 11:45 djangoproject1

We now file called requirements.txt and the file is owner by the user alice and has the owner following permissions

[root@node02 alice]# ls -lth alice-requirements.txt
-rw-rw-r--. 1 alice alice 130 Apr 17 15:54 alice-requirements.txt

So we can simply reference this file and apply the same ownership permissions to djangoproject1, Lets see the demonstration.

[root@node02 alice]# sudo chown --reference alice-requirements.txt djangoproject1/ -R

As a result we have successfully modified the user and group ownership to alice using the --reference operator; Applying the same ownership permissions as our reference file alice-requirements.txt.

[root@node02 alice]# ls -lthr djangoproject1/*
-rw-rw-r--. 1 alice alice 405 Apr 16 11:44 djangoproject1/
-rw-rw-r--. 1 alice alice 756 Apr 16 11:44 djangoproject1/
-rw-rw-r--. 1 alice alice 3.1K Apr 16 11:44 djangoproject1/
-rw-rw-r--. 1 alice alice 0 Apr 16 11:44 djangoproject1/
-rw-rw-r--. 1 alice alice 405 Apr 16 11:44 djangoproject1/

total 8.0K
-rw-rw-r--. 1 alice alice 2.3K Apr 16 11:45 settings.cpython-36.pyc
-rw-rw-r--. 1 alice alice 159 Apr 16 11:45 __init__.cpython-36.pyc

Thus we can use the chown command to update the ownership information to files and directories and also use the chgrp command as per requirement.

How do I use chgrp in Linux?

chgrp command in Linux is used to change the group ownership of a file or directory. All files in Linux belong to an owner and a group. You can set the owner by using “chown” command, and the group by the “chgrp” command.

Who can use the chgrp command?

You must use sudo with chgrp . Groups are not owned by users, so whether a file or directory is moved from one group to another is not a decision that sits with the average user. That’s a job for someone with root privileges.

What is the difference between chown and chgrp?

The chown command is used to change file or directory ownership. … Actually the chown command can be used to change both user and group ownership, while the chgrp command can only be used to change group ownership.

Is chgrp recursive?

To recursively change the group ownership of all files and directories under a given directory, use the -R option. Other options that can be used when recursively changing the group ownership are -H and -L . If the argument passed to chgrp command is a symbolic link, the -H option will cause the command to traverse it.

What does Chgrp mean in Linux?

The chgrp (from change group) command may be used by unprivileged users on Unix-like systems to change the group associated with a file system object (such as a file, directory, or link) to one of which they are a member.

How do you give ownership in Linux?

How to Change the Owner of a File

  1. Become superuser or assume an equivalent role.
  2. Change the owner of a file by using the chown command. # chown new-owner filename. new-owner. Specifies
  3. the user name or UID of the new owner of the file or directory. filename. …
    Verify that the owner of the file has changed. # ls -l filename.

What is G’s permission in Linux?

chmod g+s .; This command sets the “set group ID” (setgid) mode bit on the current directory, written as . . This means that all new files and subdirectories created within the current directory inherit the group ID of the directory, rather than the primary group ID of the user who created the file.

How do you ping on Linux?

Click or double-click the Terminal app icon—which resembles a black box with a white “>_” in it—or press Ctrl + Alt + T at the same time. Type in the “ping” command. Type in ping followed by the web address or IP address of the website you want to ping.

Who can access a file with permission 000?

File with 000 permission can be read / written by root. Everybody else cannot read / write / execute the file.

What are the chown and chgrp commands used for?

The chown command changes the owner of a file, and the chgrp command changes the group. On Linux, only root can use chown for changing ownership of a file, but any user can change the group to another group he belongs to.

How do you chown chgrp?

Stupid simple command to change ownership (chown) and change group (chgrp) at the same time. To simultaneously change both the owner and group of files or directories in linux use the following command structure: chown someusername:somegroupname filename.

What do you mean by chmod chown and chgrp commands give example for each?

These permissions read, write and execute permission for owner, group, and others. … Example: Give read/write/execute permission to the user, read/execute permission to the group, and execute permission to others. $ chmod 751 file1. #2) chown: Change ownership of the file.

Leave a Comment