Change the Group Owning Files and Directories


chgrp

By default on virtually every Linux system, when you create a new file (or directory), you are that file's owner and group. For instance, let's say you're going to write a new script that can be run on your system.

Note

To save space, I've replaced information you'd normally see with ls -l by an ellipses.


$ touch new_script.sh $ ls -l -rw-r--r-- 1 scott scott ... script.sh 


Note

The user name and group name happen to both be "scott" on this machine, but that's not necessarily the case on every machine. When a file is created, the user's UID (her User ID number) becomes the owner of the file, while the user's GID (her Group ID number) becomes the group for the file.


But what if you are part of an admins group on your machine and you want your script to be available to other members of your group so they can run it? In that case, you need to change your group from scott to admins using the chgrp command.

$ chgrp admins new_script.sh $ ls -l -rw-r--r-- 1 scott admins ... script.sh 


Note

Yes, the script still won't run because it's not executable. That process is covered later in this chapter with chmod.


You should know a couple of things about chgrp. When you run chgrp, you can use a group's name or the group's numeric ID. How do you find the number associated with a group? The easiest way is just to use cat on /etc/group, the file that keeps track of groups on your machine, and then take a look at the results.

$ cat /etc/group bind:x:118: scott:x:1001: admins:x:1002:scott,alice,bob [list truncated for length] 


The other point to make about chgrp involves security: You can only change permissions for a group if you are a member of that group. In other words, Scott, Alice, or Bob can use chgrp to make admins a group for a file or directory, but Carol cannot because she's not a member of admins.



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net