How to specify group with chmod?

34

8

I was asked to add group-wrx permissions to a directory in another user's home folder.

I believe what I should do is run chmod 771 -R directoryname in the parent directory. What I can't find anywhere on the tubes is how to specify which group I want to give these permissions to. I'm personally in a number of groups, and in a bunch I don't know about as root.

In case it matters, the system is running Redhat 5.4.

malenkylizards

Posted 2011-09-30T17:42:31.527

Reputation:

Answers

38

The chmod() system call, and by extension the chmod program, does not affect the group of a file or directory (or other type of file: block special, character special, socket, ... symlink is something of a special case). So, the group to which the permission is given will be the group to which the file or directory belongs.

To add group rwx permissions, you should use:

chmod -R g+rwx DirectoryName

However, this adds the permissions to every file as well as every directory, and not all files should be executable. Personally, I'd be very unhappy if someone provided group write permission on all (or any) of my directories, but that's another story.

To affect directories only, use find instead:

find DirectoryName -type d -exec chmod g+rwx {} +

(The + notation is POSIX 2008; not all Unix systems support it, though Linux does.)

Jonathan Leffler

Posted 2011-09-30T17:42:31.527

Reputation: 4 526

1

If you use +X (capital X) you will only add execute (or 'search') permission if the file is a directory or already had execute permission for some user. http://linux.die.net/man/1/chmod

– Dave – 2016-04-06T18:03:59.673

@Dave: chmod +X is supported on BSD (Mac OS X) too, it seems. Careful scrutiny of POSIX chmod indicates that +X is a POSIX feature after all: The perm symbol X shall represent the execute/search portion of the file mode bits if the file is a directory or if the current (unmodified) file mode bits have at least one of the execute bits (S_IXUSR, S_IXGRP, or S_IXOTH) set. It shall be ignored if the file is not a directory and none of the execute bits are set in the current file mode bits. Note the Rationale below.

– Jonathan Leffler – 2016-04-06T18:17:41.353

12

Every file on an ext filesystem has:

  1. An owner user
  2. An owner group
  3. Permissions for this user, this group and everybody else.

If you're setting rwx group permissions on a file, only the owner group of that file can read/write/execute it. You can, though, change user and owner with:

chown username file1 file2 ...
chown -R username somedir
chgrp groupname file1 file2 ....
chgrp -R groupname somedir
chown username:groupname file1 file2 ...

There are different implementations for extended filesystem privileges, i.e. ACLs (Access Control Lists) on Mac OS X, but since I'm no Linux expert, you should probably ask in Server Fault for that.

Marian Theisen

Posted 2011-09-30T17:42:31.527

Reputation: 561

"so if you're setting rwx group permissions on a file, only the owner group of that file can read/write/execute it" i realize, that may be misleading, first of all if you are the owner user, of course the owner user rules take precedence, if your neither the owner user, nor in the owner group, the rules for others apply. – None – 2011-09-30T17:53:03.857