Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Brief introduction of chgrp and newgrp commands under linux

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "introduction to chgrp and newgrp commands under linux". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "introduction to chgrp and newgrp commands under linux".

Use chgrp

Chgrp simply changes the group ownership of the file. This is the same as the chown: command. You can use:

$chown: alan mynotes

Or:

$chgrp alan mynotes recursion

Chgrp and some of its parameters can be used on the command line and in scripts. Like many other Linux commands, chgrp has a recursive parameter-R. As shown below, you need it to recursively manipulate the folder and its contents. I added the-v (detailed) parameter, so chgrp will tell me what it is doing:

$ls-l. Conf.:drwxrwxr-x 2 alan alan 4096 Aug 5 15:33 conf conf:-rw-rw-r-- 1 alan alan 0 Aug 5 15:33 conf.xml# chgrp-vR delta confchanged group of 'conf/conf.xml' from alan to deltachanged group of' conf' from alan to delta reference

When you want to change the group of files to match a specific configuration, or when you don't know the specific group (such as when you run a script), use the reference file (--reference=RFILE). You can copy another group of files (RFILE) as a reference. For example, in order to undo the above changes (please note, click. Represents the current working directory):

$chgrp-vR-reference=. Conf reports changes

Most commands have parameters to control their output. The most common is-v to enable details, and the chgrp command also has a detailed mode. It also has a-c (--changes) parameter that instructs chgrp to report only if changes are made. Chgrp also reports other things, such as when the operation is not allowed.

The parameter-f (--silent,-- quiet) is used to suppress the display of most error messages. I'll use this parameter and-c in the next section to show the actual changes.

Keep the root directory

The root directory (/) of the Linux file system should be highly valued. If the command makes a mistake at this level, the consequences can be dire and render the system unusable. Especially when running a command that will be recursively modified or even deleted. The chgrp command has a parameter that can be used to protect and maintain the root directory. It is-- preserve-root. If you use this parameter with recursion in the root directory, nothing happens, but a message appears:

[root@localhost /] # chgrp-cfR-- preserve-root aquiw / chgrp: it is dangerous to operate recursively on'/ 'chgrp: use-- no-preserve-root to override this failsafe

This option has no effect when not used in conjunction with recursion (- R). However, if the command is run by the root user, the permissions of / will be changed, but the permissions of other files or directories under it will not be changed:

[alan@localhost /] $chgrp-c-preserve-root alan / chgrp: changing group of'/': Operation not permitted [root@localhost /] # chgrp-c-preserve-root alan / changed group of'/ 'from root to alan

Surprisingly, it doesn't seem to be the default parameter. The option, no-preserve-root, is the default. If you run the above command without the keep option, it defaults to No hold mode and may change the permissions of files that should not be changed:

[alan@localhost /] $chgrp-cfR alan / changed group of'/ dev/pts/0' from tty to alanchanged group of'/ dev/tty2' from tty to alanchanged group of'/ var/spool/mail/alan' from mail to alan about newgrp

The newgrp command allows the user to override the current primary group. Newgrp is convenient when you operate in a directory where all files must have the same group ownership. Suppose you have a directory called share on your intranet server where different teams store photos of marketing events. The group name is share. When different users put files in a directory, the main groups of files may become confused. Whenever you add a new file, you can run chgrp to correct the messed up group to share:

$cd sharels-1 RW, RW, RMI, Rafe, Rafe. 1 alan share 0 Aug 7 15:35 pic13-rw-r--r--. 1 alan alan 0 Aug 7 15:35 pic1-rw-r--r--. 1 susan delta 0 Aug 7 15:35 pic2-rw-r--r--. 1 james gamma 0 Aug 7 15:35 pic3-rw-rw-r--. 1 bill contract 0 Aug 7 15:36 pic4

I introduced the setgid pattern in the article on the chmod command. It is one way to solve this problem. However, assume that the setgid bit is not set for some reason. The newgrp command is useful at this time. Before any user puts the file in the share directory, they can run the command newgrp share. This switches its primary group to share, so all files they put in the directory will have share groups instead of the user's own primary groups. When you are finished, you can switch back to the regular primary group (for example) with the following command:

Newgrp alan thank you for reading, the above is the content of "introduction to chgrp and newgrp commands under linux". After the study of this article, I believe you have a deeper understanding of the introduction of chgrp and newgrp commands under linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report