21

For example, I want to give my colleagues write access to certain directory. Let's assume that subdirectories in it had access rights 775, files 664, and also there were some executable files in the dir - 775.

Now I want to add write permissions. With chmod, I could try something like

chmod o+w -R mydir/

But that's not cool, since I don't want to make the dir world-writable - I want give access only to certain users, so I want to use ACL. But is there an easy way to set those permissions? As I see it, I need to tackle at least three cases (dirs, files, executable files) separately:

find -type d -exec setfacl -m u:colleague:rwx {} \;
find -type f -executable -exec setfacl -m u:colleague:rwx {} \;
find -type f \! -executable -exec setfacl -m u:colleague:rw {} \;

It seems quite a lot of code lines for such a simple task. Is there a better way?

33

setfacl has a recursive option (-R) just like chmod:

  -R, --recursive
      Apply operations to all files and directories recursively. This
      option cannot be mixed with `--restore'.

it also allows for the use of the capital-x X permission, which means:

  execute only if the file is a directory or already has
  execute permission for some user (X)

so doing the following should work:

setfacl -R -m u:colleague:rwX .

(all quotes are from man setfacl for acl-2.2.52 as shipped with Debian)

  • 4
    setfacl does not have the '-R' flag in FreeBSD 9 – Aaron C. de Bruyn Aug 20 '14 at 6:50
  • 6
    then it's good that the OP tagged their question as [linux] – umläute Jul 28 '15 at 8:32
  • Missed that. ;) – Aaron C. de Bruyn Jul 28 '15 at 17:20
  • Does this automatically apply to new files/folders created within afterwards? – Enigma Feb 4 '18 at 8:10
  • Check the man page for your version of setfacl. The option -X doesn't work as expected in included in setfacl 2.2.49. – phyatt Apr 10 '18 at 16:45
3

As mentioned by umläute, the command setfacl -R with uppercase "X" is the way to go, like:

setfacl -R -m u:colleague:rwX .

However, for those who need to re-apply ACL recrusively (i.e like "re-apply permissions on sub-directories" à la Windows).

find . -mindepth 1 | xargs -n 50 setfacl -b --set-file=<(getfacl . | sed -e 's/x$/X/')

That command could be splited to avoid error like setfacl: foobar: Only directories can have default ACLs.

find . -mindepth 1 -type d| xargs -n 50 setfacl -b --set-file=<(getfacl . | sed -e 's/x$/X/')
find . -mindepth 1 -type f| xargs -n 50 setfacl -b --set-file=<(getfacl . | grep -v '^default:' | sed -e 's/x$/X/')

Note that the syntax <( something ) is Process Substitution, which is specific to bash. You may need to create a temporary file if you use another shell.

-1
for i in $(find /data -mindepth 0 -type d)
do setfacl -m  u:zabbix:r-x $i
    echo "ACL rules set for "$i
done
  • 2
    Do not use for i in $(find …). – muru Feb 24 '16 at 11:41
  • @muru Out of curiosity, why not? – Shadur Feb 24 '16 at 13:10
  • The whole comment is a link. Follow it and find out. – muru Feb 24 '16 at 13:10
  • If you want to go this approach use find /data -mindepth 0 -type d -exec setfacl -m u:zabbix:r-X {} \; – sibaz Apr 5 '18 at 9:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.