How To Use Change Group Command in Linux for File Permissions

March 26, 2024 Off By admin
Shares

How To Use “chgrp Command” (Change Group) in Linux

In Linux, the change group or the

chgrp

command is mostly used to change the user group of

the system. The chgrp command works for both Linux and other Unix-like operating systems.

If you’re a system admin, you must know that the chgrp, chmod, and chown commands are the most powerful and used terminal commands.

The chgrp command is run with the sudo or root privilege in the system, but if you’re the owner of that particular le or directory, you can execute the change group command as well.

The most common usages of the change group command are installing a new package in the group, moving one le to another directory from the user group, and editing a non-read/writable

le. This chgrp command can be very handy and a life savior in critical situations.

Why

chgrp , When

chown

Can Do The Same?

You might have been wondering why you need to use the change group command when you can change the entire ownership with a chown command.

Here is the answer to why, no doubt, the chown command is a powerful command. Sometimes, the chown command can make changes that are unwanted and are not acceptable.

To protect your les from careless and accidental use of the chown command, it’s safe to use the chgrp command.

chgrp (Change Group) Command in Linux

The chgrp command is widely used to change the ownership of a single le or a group of les. If you’re the project owner and want to hand over your les or add a new team member to your team without changing the entire directory, the change group command might be very helpful for you.

Even if you’re not the main owner of the le, but you belong to the group and have sudo access, you can add members, change the group permission, and add more members to the main or supplementary groups.

This post will show the most common and most used chgrp commands on Linux.

  1. Check the Group Info with chgrp

At the beginning of the chgrp command, you might want to know the group name to ensure you’re

in the group. To do that, you can execute the

ls -l

command on your terminal shell. In return,

you will see the user’s name, group, and le details.

ls -l

  1. chgrp Command Syntax

The syntax of the change group command on Linux is easy and hassle-free. This cross-platform terminal command can be executed on all major Linux desktops, servers, and Unix-like operating systems.

Here is the basic syntax pattern of the chgrp command.

chgrp [OPTION]… GROUP path/FILE_NAME sudo addgroup ubuntupitdemo

sudo chgrp ubuntupitdemo test.txt ls -l

Add User To The Group

As I already mentioned, you can add more people to your working group or the directory group if you have sudo access.

Here is the command that you can execute on the terminal shell. For instance, I’ve added the user

ubuntupitdemo in the user group.

$ sudo useradd ubuntupitdemo # touch test.txt

# ls -lt test.txt

chgrp Command Ownership

If you need to change a le, folder, or directory ownership, you can easily execute the following chgrp command on your shell.

Here, for instance, I’m going to create a new directory, and I will change the group of this directory.

sudo mkdir New_Dir

sudo chgrp ubuntupitdemo New_Dir

Recursively Change the Group Privilege

In Linux, you can alter the ownership of a directory through the change group command. The

following chgrp command with a -R ag will allow you to de ne the elements inside the group

that you need to change for that active set of the directory.

sudo chgrp -R ubuntupitdemo New_Dir

chgrp Command with the Reference File

If you need to use a previous reference or refer or match with another group, you can use the following change group command on your shell.

Here, you will need to add the -R

ag with the reference syntax.

sudo chgrp -R –reference=test.txt New_Dir

chgrp Command With Soft Link

In Linux, there are many operations and folders inside the lesystem, where the soft link of a directory is used to indicate the original directory. However, if you need to change the group of that

folder, you can use the command.

–dereference

and

–no-dereference

syntax with the change group

sudo chgrp –dereference ubuntupitdemo symbolic_link

While you use the -dereference syntax, it will change the original name of that group. On the other hand, the no-dereference syntax will only change the name of the symbolic link or the soft link while keeping the original folder unchanged.

Display chgrp Execution Details

For system admins and Linux power users, this command will be helpful. If you want to see the log of changes in a le on your system, please execute the mentioned command.

sudo chgrp -c -R [GROUP_NAME] [DIRECTORY/FILE_NAME]

sudo chgrp -c -R ubuntupitdemo example

Hide chgrp Command Errors

If you only need to show the nal output of a command and hide all the errors from the terminal

shell, the below-mentioned change group command with a -f ag will hide all the errors.

sudo chgrp -f [GROUP_NAME] [DIRECTORY/FILE_NAME]

Add Multiple Files to the chgrp Command

If you need to change the group of multiple les at a time, you can use the below-mentioned change group command as an array argument in the shell.

sudo chgrp www-data file1 file2 dir1

Run chown and chgrp Command Together

If you’re thinking of combining the chown and chgrp in the same command, that is not impossible either. In a way, you can rst add the chown command and then de ne which user group you want to assign for that command.

The command below might help you understand commands and syntaxes.

$ chown someusername:somegroupname filename.ext

$ sudo chown user_name:group _name file_name

You can go through the manual pages of the chown and chgrp command on your Linux system.

man chown man chgrp

Extra Tip: Issues You May Face on chgrp Command

Here, I will share the answers to some of the most asked questions about the change group command on Linux. Please check if you need to solve any of the following situations.

If you face errors while executing the chgrp command on your Linux server or desktop, make sure that you’ve enclosed the directory path.

In most cases, it does not cause any issue on the desktop, but be careful while executing the chgrp command on Linux servers, Apache, Nginx, or SSH servers.

If you need to reset the change group rules on your lesystem, you can use the setfacl

command to restore the directory ownership. Both of the below-mentioned setfacl and getfacl commands are e ective in restoring group ownership.

setfacl –restore={dir}.facl getfacl -R {dir} >{dir}.facl

In some cases, you might also face issues executing the chgrp command on Linux. In that case, you can edit the sudoers le on your system. The sudoers le is the controller that de nes which user group can execute which commands. You can nd the sudoers les inside the /etc/sudoers directory.

Insights!

The change group command makes your Linux experience more professional and powerful. It is very safe to execute if you know what you’re doing. Please be careful while executing the chgrp command to avoid the lesystem getting abandoned.

Proper knowledge of the change group command can be the Swiss army knife for a Linux user. In the entire post, we have seen the basic and powerful commands of the chgrp command on Linux.

If you nd this post useful and informative, please share it with your friends and the Linux community. You can also write your opinions about this post in the comment section.

 

Shares