Oct 21, 2023 4 min read

Chgrp Command in Linux (Change Group)

Use chgrp command in Linux with our steep-by-step tutorial. The chgrp command is used to change the group ownership of files and directories.

Chgrp Command in Linux
Table of Contents

Introduction

Before we discuss chgrp command in Linux (Change Group), let's briefly understand-What is chgrp Command ?

The chgrp command in Linux is used to change the group ownership of files and directories. Ownership in Linux includes both the user and group associated with a particular file or directory. The chgrp command allows users to modify the group ownership by specifying the new group name or group ID.

Each file in Linux has an owner and a group, as well as permissions that control who can read, write, and execute the file.

In this tutorial, you will modify the group ownership of files using the chgrp command. We will also address a few FAQs on chgrp command in Linux (Change Group).

chgrp Command Syntax

The chgrp command is written as follows:

chgrp [OPTIONS] GROUP FILE..
  • GROUP, the new group's name, or the group ID (GID). The + symbol must precede a numeric GID.
  • FILE.., is the name of a single or more files.

Unlike the chown command, which affects both user and group ownership, chgrp only affects group ownership.

Use the ls -l command to see which group the file belongs to.

Regular users can only modify the file's group if they own it, and only to a group they are a part of. All files can have their group ownership changed by administrative users.

How to Change the Ownership of a File Group

Use the chgrp command with the new group name and the target file as arguments to change the group ownership of a file or directory.

To change the group of the file filename to www-data, type:

chgrp www-data filename

You'll get an "Operation not permitted" error if you run the command as an unprivileged user. Invoke the command with the -f argument to disable the error message. chgrp does not produce any output and returns 0 by default when it succeeds.

You may also use the chgrp command to pass several files as arguments:

chgrp www-data file1 file2 dir1

To acquire information about the files being processed, use the -v option:

chgrp -v www-data file1 file2
Output

changed group of 'file1' from nginx to www-data
group of 'file2' retained as www-data

Instead of -v, use -c to print information just about the files that have changed.

Instead of a username, a numeric group ID (GID) might be used. The following example assigns ownership of the file to a new group with a GID of 1000:

chgrp +1000 filename

When not used recursively, the chgrp command changes the group ownership of the symbolic link targets rather than the symbolic links themselves.

If you try to modify the group of the symbolic link symlink1 that points to /opt/file1, for example, chgrp will change the ownership of the file or directory to which the symlink points:

chgrp www-data symlink1

You'll probably get a "cannot dereference'symlink1': Permission refused" error instead of updating the target group.

The problem occurs because symlinks are protected by default on most Linux versions, and you cannot operate on target files. /proc/sys/fs/protected symlinks specify this option. 1 indicates that the item is enabled, whereas 0 indicates that it is disabled. We do not advocate disabling the symlink protection.

Use the -h option to modify the group ownership of the symlink:

chgrp -h www-data symlink1

How to Change Group Ownership in a Recursive Way

Use the -R option to recursively update the group ownership of all files and directories under a particular directory.

For example, the following command will give the www-data group ownership of all files and directories in the /var/www directory:

chgrp -R www-data /var/www

When the recursive option is used, chgrp skips over symbolic links and makes no changes to them. Pass the -h option to modify the group ownership of the symbolic links:

chgrp -hR www-data /var/www

The -H and -L options can also be used to recursively change the group ownership.

If the chgrp command is given a symbolic link as an input, the -H option will cause the command to traverse it. -L instructs chgrp to follow each symlink to a directory it encounters. In most circumstances, you should avoid using these choices because they may cause your system to malfunction or pose a security risk.

FAQs on Chgrp Command in Linux

How does the chgrp command work?

chgrp modifies the group ownership of specified files and directories. It requires the user to provide the new group name or group ID.

How do I use the chgrp command to change group ownership?

To change the group ownership using chgrp, specify the new group name or group ID, followed by the files or directories you wish to modify. For example, chgrp group_name file.txt will change the group ownership of file.txt to the specified group.

Can I change the group ownership of multiple files using chgrp?

Yes, chgrp accepts multiple file or directory names as arguments, allowing you to change the group ownership of multiple items simultaneously. For example, chgrp group_name file1 file2 dir1 will change the group ownership of file1, file2, and dir1 to the specified group.

What are some common use cases for the chgrp command?

The chgrp command is often used in scenarios where group ownership needs to be modified, such as when sharing files among a group of users or when granting specific access rights to a defined group of users.

Can I recursively change group ownership of directories and their contents?

Yes, you can use the -R or --recursive option with chgrp to recursively change group ownership for directories and their contents. This option ensures that ownership modifications are applied to all subdirectories and files within the specified directory.

Can chgrp change only the group ownership without modifying the user owner?

Yes, the chgrp command focuses solely on changing the group ownership of files and directories. It does not modify the user or owner of the files.

What happens if I use chgrp with root privileges?

When using chgrp with root privileges (sudo), you have the capability to modify the group ownership of files or directories that belong to other users. Root access allows you to change group ownership without any restrictions.

Conclusion

The -H and -L options can also be used to modify group ownership in a recursive manner.

The -H option will cause the chgrp command to traverse a symbolic link if it is specified as an input. -L tells chgrp to follow each symlink it finds to a directory.

In most cases, these options should be avoided since they may cause your system to malfunction or offer a security risk.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.