324

I know I can assign the permission to write to an owner/group/others like this:

chmod u+w myfolder

Can I specify the specific user here? Some like this:

chmod username u+w myfolder
Charo
  • 3,631
Max Koretskyi
  • 3,691
  • 4
  • 17
  • 19

5 Answers5

423

You could use setfacl:

setfacl -m u:username:rwx myfolder

This sets permissions for specific users, without changing the ownership of the directory.

Check out the man page for further details and examples.

Note:

  • setfacl is short for set File ACL (Access Control List)

  • If you want to apply it recursively to all the subdirectories: add the -R flag like this:

    setfacl -R -m u:username:rwx myfolder
    
Zanna
  • 70,465
DrG
  • 4,330
  • 13
    Perfect answer! Also setfacl is available on CentOS (yum -y install acl) & FreeBSD as well. – Viet Apr 20 '17 at 12:21
  • 9
    just a note: setfacl = set File ACL, ACL = Access Control List hence for short "setFacl" - easier way to remember i think. – Yawar Murtaza Aug 22 '17 at 10:29
  • 28
    If you want to apply it recursively to all the subdirectories: add the -R flag like this: setfacl -R -m u:username:rwx myfolder – Jose A Mar 14 '18 at 21:01
  • easiest way to give permission for a new user – Rajesh Mbm Mar 17 '18 at 17:03
  • 2
    Is there a way to grant a user rw without changing any existing x permission, if operating on multiple files or recursively? – Qi Fan Apr 23 '18 at 21:58
  • Sure, just leave the x out in the command above. – DrG Apr 26 '18 at 08:02
  • This is much safer than 'chown' and 'chmod' commands that can easily break a server if you're still learning the ropes on Linux distros. – Wachaga Mwaura Jun 18 '19 at 18:33
  • 1
    @DrG that does in fact unset the x flag! I tried it and lost access to the directory and all subdirs which apparently require the x flag in order to access them. – PhillipM Sep 22 '21 at 09:15
  • Will this work for newly created files as well, or do we need to re-apply permissions each time new files are created? – Eliezer Berlin Oct 25 '21 at 09:11
  • 2
    I get Operation not supported – Wassadamo Jan 21 '22 at 17:26
  • 1
    if you get a "Operation not supported" this means its most likely an external mount that is not mounted with acl support. in the fstab add the acl option and remount it – Ganesh Krishnan Nov 15 '22 at 19:17
261

If you want to change the user owning this file or directory (folder), you will have to use the command chown. For instance, if you run

sudo chown username: myfolder

the user owning myfolder will be username. Then you can execute

sudo chmod u+w myfolder

to add the write permission to the username user.

But if you want to add this user to the group associated with "myfolder", you can run

sudo usermod -a -G groupname username

and then execute

sudo chmod g+w myfolder

to add the write permission to the group.

jokerdino
  • 41,320
Charo
  • 3,631
  • 2
    There is a way in which both users will have access to the folder, and a group is not needed; it was suggested in an answer for the same question here: superuser.com/a/235398/191720. – Luan Nico Feb 03 '16 at 17:17
  • 1
    Can I do this without sudo? – a06e Jun 02 '16 at 13:36
  • 1
    @becko: You can if you drop to a root shell. – Charo Jun 02 '16 at 14:07
  • 1
    @Charo I meant without root – a06e Jun 02 '16 at 14:28
  • @becko: You must have root privileges to change permissions associated to users or groups. – Charo Jun 02 '16 at 15:24
  • +1 for chmod g+w myfolder . That's what I've been missing. – Arda Jul 30 '16 at 23:10
  • Why is there a colon after username? – AER Apr 18 '18 at 06:56
  • 1
    @AER: In the first command, if a colon follows username the owning group is also changed to username (if you omit such a colon, the owning group will not change). – Charo Apr 18 '18 at 08:46
  • You can add the -R for recursive option. For more options type chown --help – Rahal Kanishka Aug 07 '19 at 20:52
  • Could you expand on "groupname" is a group name supposed to already exist? How do we see it? How do we see who is in it? Or should I create it? Can I add someone to the group just for one directory or will this affect all other permissions related to this group? – Kvothe May 10 '23 at 13:22
16

No this is not possible. You can either change the owner of the file with

[sudo] chown username: foldername

or you can add the user to the group that owns the file with

usermod -a -G {group-name} username
David Foerster
  • 36,264
  • 56
  • 94
  • 147
Pabi
  • 7,401
  • 3
  • 40
  • 49
11

If you want to apply it recursively to all the subdirectories: add the -R flag like this:

setfacl -R -m u:username:rwx myfolder
Akitha_MJ
  • 291
  • 2
  • 4
7

In Ubuntu recursively (folder and it's all sub folders) giving permission to a specific user:

sudo chown -R <username>: <folderName>
ranaFire
  • 81
  • 1
  • 2