In a latest article, I launched the chown command, which is used for modifying possession of recordsdata on techniques. Recall that possession is the mixture of the consumer and group assigned to an object. The chgrp and newgrp instructions present extra assist for managing recordsdata that want to take care of group possession.
The chgrp command merely modifications the group possession of a file. It is similar because the chown :<group> command. You can use:
$chown :alan mynotes
$chgrp alan mynotes
Just a few extra arguments to chgrp will be helpful at each the command line and in a script. Just like many different Linux instructions, chgrp has a recursive argument, -R. You will want this to function on a listing and its contents recursively, as I am going to exhibit under. I added the -v (verbose) argument so chgrp tells me what it’s doing:
$ ls -l . conf
drwxrwxr-x 2 alan alan 4096 Aug 5 15:33 conf
-rw-rw-r-- 1 alan alan zero Aug 5 15:33 conf.xml
# chgrp -vR delta conf
modified group of 'conf/conf.xml' from alan to delta
modified group of 'conf' from alan to delta
A reference file (–reference=RFILE) can be utilized when altering the group on recordsdata to match a sure configuration or when you do not know the group, as is likely to be the case when working a script. You can duplicate one other file’s group (RFILE), known as a reference file. For instance, to undo the modifications made above (recall that a dot [.] refers back to the current working listing):
$ chgrp -vR --reference=. conf
Most instructions have arguments for controlling their output. The commonest is -v to allow verbose, and the chgrp command has a verbose mode. It additionally has a -c (–changes) argument, which instructs chgrp to report solely when a change is made. Chgrp will nonetheless report different issues, similar to if an operation shouldn’t be permitted.
The argument -f (–silent, –quiet) is used to suppress most error messages. I’ll use this argument and -c within the subsequent part so it would present solely precise modifications.
The root (/) of the Linux filesystem ought to be handled with nice respect. If a command mistake is made at this stage, the results will be horrible and go away a system fully ineffective. Particularly if you end up working a recursive command that may make any type of change—or worse, deletions. The chgrp command has an argument that can be utilized to guard and protect the foundation. The argument is –preserve-root. If this argument is used with a recursive chgrp command on the foundation, nothing will occur and a message will seem as an alternative:
[root@localhost /]# chgrp -cfR --preserve-root a+w /
chgrp: it's harmful to function recursively on '/'
chgrp: use --no-preserve-root to override this failsafe
The choice has no impact when it isn’t used along with recursive. However, if the command is run by the foundation consumer, the permissions of / will change, however not these of different recordsdata or directories inside it:
[alan@localhost /]$ chgrp -c --preserve-root alan /
chgrp: altering group of '/': Operation not permitted
[root@localhost /]# chgrp -c --preserve-root alan /
modified group of '/' from root to alan
Surprisingly, it appears, this isn’t the default argument. The choice –no-preserve-root is the default. If you run the command above with out the “preserve” choice, it would default to “no preserve” mode and presumably change permissions on recordsdata that should not be modified:
[alan@localhost /]$ chgrp -cfR alan /
modified group of '/dev/pts/zero' from tty to alan
modified group of '/dev/tty2' from tty to alan
modified group of '/var/spool/mail/alan' from mail to alan
The newgrp command permits a consumer to override the present main group. newgrp will be useful if you end up working in a listing the place all recordsdata should have the identical group possession. Suppose you’ve gotten a listing known as share in your intranet server the place totally different groups retailer advertising pictures. The group is share. As totally different customers place recordsdata into the listing, the recordsdata’ main teams would possibly turn out to be blended up. Whenever new recordsdata are added, you’ll be able to run chgrp to right any mix-ups by setting the group to share:
$ cd share
-rw-r--r--. 1 alan share zero Aug 7 15:35 pic13
-rw-r--r--. 1 alan alan zero Aug 7 15:35 pic1
-rw-r--r--. 1 susan delta zero Aug 7 15:35 pic2
-rw-r--r--. 1 james gamma zero Aug 7 15:35 pic3
-rw-rw-r--. 1 invoice contract zero Aug 7 15:36 pic4
I coated setgid mode in my article on the chmod command. This could be one solution to remedy this downside. But, suppose the setgid bit was not set for some cause. The newgrp command is beneficial on this state of affairs. Before any customers put recordsdata into the share listing, they will run the command newgrp share. This switches their main group to share so all recordsdata they put into the listing will robotically have the group share, relatively than the consumer’s main group. Once they’re completed, customers can swap again to their common main group with (for instance):
It is essential to grasp the way to handle customers, teams, and permissions. It can also be good to know just a few alternative routes to work round issues you would possibly encounter since not all environments are arrange the identical manner.