218

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
1
  • 1
    I don't think it's possible. Why don't you just add the user to the group? – Emil Jun 24 '14 at 10:50
216

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.

9
  • 1
    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 3 '16 at 17:17
  • 1
    Can I do this without sudo? – becko Jun 2 '16 at 13:36
  • 1
    @becko: You can if you drop to a root shell. – Charo Jun 2 '16 at 14:07
  • 1
    @Charo I meant without root – becko Jun 2 '16 at 14:28
  • 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 8:46
283

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.

EDIT: Highlighted points in comment

  1. setfacl = set File ACL, ACL = Access Control List hence for short "setFacl"
  2. If you want to apply it recursively to all the subdirectories: add the -R flag like this: setfacl -R -m u:username:rwx myfolder
7
  • 9
    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
  • 24
    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
  • 1
    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
13

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
2
  • 1
    useradd -G {group-name} username tells me that username already exists – Erdal G. Sep 20 '16 at 11:51
  • 1
    i tried this but my user still can't write to that folder – Sonic Soul Sep 9 '18 at 16:32
6

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

setfacl -R -m u:username:rwx myfolder

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