How to set permissions recursively, 700 for folders and 600 for files, without using find
Asked Answered
S

1

7

I'm trying to figure out a way to set permissions recursively 700 for dirs and subdirs on a specific path and 600 for files. I would use these commands:

find /path -type d -print0 | xargs -0 chmod 700
find /path -type f -print0 | xargs -0 chmod 600

But the user does not have permission to run the "find" command. As a workaround I tried to make a script that contains the above commands from the root user with setuid sticky bit set so it will run with root privileges (like passwd or sudo commands that normal users run with root privileges):

chmod 4755 script.sh

but i cannot execute the script from the limited user account, it still says that I don't have permission to run the find command.

Does anyone have any idea how i can accomplish this without having to use the find command?

Edit: OS: Centos 6.5

Sielen answered 11/4, 2016 at 16:1 Comment(0)
S
10

Apparently this is very easy to implement. There are 2 ways: using chmod only, or setting ACL (access control list) on the desired path:

  • Using chmod i would run:
    chmod -R 600 /path          # to remove executable permissions
    chmod -R u=rwX,g=,o= /path  # to make directories transversable
    

for the user owner i'm giving capital "X", so it does apply only to directories and not files.

  • Using ACL:

    setfacl -Rm u::rwX,g::0,o::0 /path
    setfacl -Rm d:u::rwX,g::0,o::0 /path
    

again using capital X so it applies only to directories and not files. The first command applies the ACL, the second one makes it default policy so newly created files will inherit the desired permissions.

Sielen answered 12/4, 2016 at 18:7 Comment(1)
The chmod version doesn't quite work properly, the X is a special execute that will grant execute permissions if at least one of the other modes has execute. That means if the file already had x in user group or other, then it will remain with an execute setting on the user. To do this with only chmod, we must first wipe out all execute settings in the beginning, then run your chmod command. For example chmod -R a-x /path; chmod -R u=rwX,g=,o= /path.Formicary

© 2022 - 2024 — McMap. All rights reserved.