Linux chgrp command change the group of the file directory

In Linux each file is associated with an owner and a group, and has the right to determine which users can read

3 min read
By myfreax
Linux chgrp command change the group of the file directory
Linux chgrp command change the group of the file directory

In Linux each file is associated with an owner and a group, and has the right to determine which users can read, write, or execute the file. The chgrp command is used to change the ownership of a specific file group.

This tutorial describes how to use the chgrp command  to change a file directory ownership group on Linux. The syntax of the chgrp command is included.

Change filegroup ownership, symbolic link group ownership, recursively reshuffled ownership, and reshuffled ownership using GID or group name.

Syntax of the chgrp command

The syntax of the chgrp command is chgrp [OPTIONS] GROUP FILE.. . GROUP is the name or ID of the group, that is, GID. The GID must be prefixed with a + symbol. FILE.. The name of one or more files.

Unlike chown, which allows you to change the owner of a file directory, chgrp changes the ownership of a file directory group.

To find out the owning group of the file, run the ls command ls -al. Only the root user or users with sudo permission can change the owning group of the file directory.

chgrp command changes the ownership of the file directory group

To change all groups in the file directory, run the chgrp command, followed by the name of the user group and the target file as parameters.

For example, the sudo chgrp www-data filename command changes the owning group of the filename file to www-data.

If you run a command as a user without sudo permission, you will receive an error message saying that the Operation is not permitted.

By default, chgrp does not produce any output on success and returns exit code 0. To verify the successful modification, run the ls command ls -al.

sudo chgrp www-data filename
ls -al filename

You can also pass multiple files or directories as parameters to the chgrp command and use the -v option to get information about the files being processed.

You can also use the group ID/GID instead of the group name. For example, run the sudo chgrp +1000 filename command to change the ownership of a file group to GID 1000.

chgrp www-data file1 file2 dir1
chgrp -v www-data file1 file2
chgrp +1000 filename
changed group of 'file1' from nginx to www-data
group of 'file2' retained as www-data

When not recursively modifying the array, the default behavior of the chgrp command is to modify the owning group of the target file of the symlink, not the symlink itself.

For example, run the sudo chgrp www-data symlink1 command to change the owning group of the target file of the soft link symlink1. The owning group of the soft link file is not changed.

sudo chgrp www-data symlink1

When modifying the owning group of the file directory, you will most likely receive the error message cannot dereference 'symlink1' : Permission denied.

The error occurred because symlinks on most Linux distributions are protected and you cannot operate on the object file.

To enable or disable the protection of target symlinks, you can specify this in the /proc/sys/fs/protected_symlinks file. 1 indicates enable, 0 indicates disable. We recommend not disabling symbolic link protection.

When you encounter a protected symlink, you can try using the -h option of the chagrp command, which will try to change the ownership of the symlink file and then modify the array of targets.

sudo chgrp -h www-data symlink1

chgrp command recursively changes the owning group of the file directory

To recursively change the ownership of all file and directory groups, use the -R option. If the -R option is specified, the chgrp command does not traverse the symbolic link and does not change the symbolic link object file.

However, you can change the ownership of the symbolic link target file group by using the -h option of the chgrp command. If the target file for the symbolic link is a directory, you also need to specify the -H option to traverse the directory.

In addition, you need to use the -L option to traverse each symbolic link to the directory. In most cases, the -L and -H options are not recommended because they may cause confusion or pose a security risk.

chgrp -R www-data /var/www
chgrp -hR www-data /var/www #include symlinks

conclusion

The chgrp command changes the array of file, directory, and symbolic link target file directories. Although you can use the more popular chown command to change the owning group of the file directory.

But the chgrp command has a simple syntax that is easy to remember. If you have any questions or feedback, please feel free to comment.