{"id":5923,"date":"2024-06-27T01:45:50","date_gmt":"2024-06-27T05:45:50","guid":{"rendered":"https:\/\/www.both.org\/?p=5923"},"modified":"2024-06-16T21:10:31","modified_gmt":"2024-06-17T01:10:31","slug":"intro-to-the-linux-chgrp-and-newgrp-commands","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=5923","title":{"rendered":"Intro to the Linux chgrp and newgrp commands"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"5923\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>In a recent article, I introduced the <a href=\"https:\/\/www.both.org\/wp-admin\/post.php?post=5937\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>chown<\/strong> command<\/a>, which is used for modifying ownership of files on systems. Recall that ownership is the combination of the user and group assigned to an object. The <strong>chgrp<\/strong> and <strong>newgrp<\/strong> commands provide additional help for managing files that need to maintain group ownership.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using chgrp<\/h2>\n\n\n\n<p>The <strong>chgrp<\/strong> command simply changes the group ownership of a file. It is the same as the <strong>chown :&lt;group&gt;<\/strong> command. You can use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$chown :alan mynotes<\/code><\/pre>\n\n\n\n<p>or:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$chgrp alan mynotes<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Recursive<\/h2>\n\n\n\n<p>A few additional arguments to chgrp can be useful at both the command line and in a script. Just like many other Linux commands, chgrp has a recursive argument, <strong>-R<\/strong>. You will need this to operate on a directory and its contents recursively, as I&#8217;ll demonstrate below. I added the <strong>-v<\/strong> (<strong>verbose<\/strong>) argument so chgrp tells me what it is doing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -l . conf\n.:\ndrwxrwxr-x 2 alan alan 4096 Aug  5 15:33 conf\n\nconf:\n-rw-rw-r-- 1 alan alan 0 Aug  5 15:33 conf.xml\n# chgrp -vR delta conf \nchanged group of 'conf\/conf.xml' from alan to delta\nchanged group of 'conf' from alan to delta<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p>A reference file (<strong>&#8211;reference=RFILE<\/strong>) can be used when changing the group on files to match a certain configuration or when you don&#8217;t know the group, as might be the case when running a script. You can duplicate another file&#8217;s group (<strong>RFILE<\/strong>), referred to as a reference file. For example, to undo the changes made above (recall that a dot [<strong>.<\/strong>] refers to the present working directory):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ chgrp -vR --reference=. conf<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Report changes<\/h2>\n\n\n\n<p>Most commands have arguments for controlling their output. The most common is <strong>-v<\/strong> to enable verbose, and the chgrp command has a verbose mode. It also has a <strong>-c<\/strong> (<strong>&#8211;changes<\/strong>) argument, which instructs chgrp to report only when a change is made. Chgrp will still report other things, such as if an operation is not permitted.<\/p>\n\n\n\n<p>The argument <strong>-f<\/strong> (<strong>&#8211;silent<\/strong>, <strong>&#8211;quiet<\/strong>) is used to suppress most error messages. I will use this argument and <strong>-c<\/strong> in the next section so it will show only actual changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preserve root<\/h2>\n\n\n\n<p>The root (<strong>\/<\/strong>) of the Linux filesystem should be treated with great respect. If a command mistake is made at this level, the consequences can be terrible and leave a system completely useless. Particularly when you are running a recursive command that will make any kind of change\u2014or worse, deletions. The chgrp command has an argument that can be used to protect and preserve the root. The argument is <strong>&#8211;preserve-root<\/strong>. If this argument is used with a recursive chgrp command on the root, nothing will happen and a message will appear instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;root@localhost \/]# chgrp -cfR --preserve-root a+w \/\nchgrp: it is dangerous to operate recursively on '\/'\nchgrp: use --no-preserve-root to override this failsafe<\/code><\/pre>\n\n\n\n<p>The option has no effect when it&#8217;s not used in conjunction with recursive. However, if the command is run by the root user, the permissions of <strong>\/<\/strong> will change, but not those of other files or directories within it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;alan@localhost \/]$ chgrp -c --preserve-root alan \/\nchgrp: changing group of '\/': Operation not permitted\n&#091;root@localhost \/]# chgrp -c --preserve-root alan \/\nchanged group of '\/' from root to alan<\/code><\/pre>\n\n\n\n<p>Surprisingly, it seems, this is not the default argument. The option <strong>&#8211;no-preserve-root<\/strong> is the default. If you run the command above without the &#8220;preserve&#8221; option, it will default to &#8220;no preserve&#8221; mode and possibly change permissions on files that shouldn&#8217;t be changed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;alan@localhost \/]$ chgrp -cfR alan \/\nchanged group of '\/dev\/pts\/0' from tty to alan\nchanged group of '\/dev\/tty2' from tty to alan\nchanged group of '\/var\/spool\/mail\/alan' from mail to alan<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">About newgrp<\/h2>\n\n\n\n<p>The <strong>newgrp<\/strong> command allows a user to override the current primary group. newgrp can be handy when you are working in a directory where all files must have the same group ownership. Suppose you have a directory called <em>share<\/em> on your intranet server where different teams store marketing photos. The group is <strong>share<\/strong>. As different users place files into the directory, the files&#8217; primary groups might become mixed up. Whenever new files are added, you can run <strong>chgrp<\/strong> to correct any mix-ups by setting the group to <strong>share<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cd share\nls -l\n-rw-r--r--. 1 alan share 0 Aug  7 15:35 pic13\n-rw-r--r--. 1 alan alan 0 Aug  7 15:35 pic1\n-rw-r--r--. 1 susan delta 0 Aug  7 15:35 pic2\n-rw-r--r--. 1 james gamma 0 Aug  7 15:35 pic3\n-rw-rw-r--. 1 bill contract  0 Aug  7 15:36 pic4<\/code><\/pre>\n\n\n\n<p>I covered <strong>setgid<\/strong> mode in my article on the\u00a0<a href=\"https:\/\/www.both.org\/wp-admin\/post.php?post=5926\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>chmod<\/strong> command<\/a>. This would be one way to solve this problem. But, suppose the setgid bit was not set for some reason. The newgrp command is useful in this situation. Before any users put files into the <em>share<\/em> directory, they can run the command <strong>newgrp share<\/strong>. This switches their primary group to <strong>share<\/strong> so all files they put into the directory will automatically have the group <strong>share<\/strong>, rather than the user&#8217;s primary group. Once they are finished, users can switch back to their regular primary group with (for example):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>newgrp alan<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>It is important to understand how to manage users, groups, and permissions. It is also good to know a few alternative ways to work around problems you might encounter since not all environments are set up the same way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a recent article, I introduced the chown command, which is used for modifying ownership of files on<\/p>\n","protected":false},"author":429,"featured_media":2704,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[100,90,5],"tags":[444,104,445],"class_list":["post-5923","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-in-depth","category-linux","tag-chgrp","tag-command-line","tag-newgrp"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/429"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5923"}],"version-history":[{"count":4,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5923\/revisions"}],"predecessor-version":[{"id":5976,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5923\/revisions\/5976"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2704"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}