0

I have two users, user1 and user2. I also have a file in /path/to/file. user1 should be able to have read-write access, while user2 should only have read access.

I know that I can change permissions with chmod u=r /path/to/file to read-only, however this changes the permissions for everyone? When executing the command as user1 the access changes for user1 and user2 as well. I haven't found a option to specify a user. Is this something where I'd have to use the groups? Or chown? How would I go about doing this?

Is this also possible to do for a whole directory full of files? If there is a dir /path/to/dir that contains n files and m subdirs, to change the permissions of every file and every file in the subdirs?

0

You can use next solution:

  • change the ownership of the file: chown user1 /path/to/file
  • change permission for the owner, group and other: chmod 644 /path/to/file

This will give rw to user1 and r to user2

For directories you must add x to give the option to the user to change in this directory:

chmod 755 /path/to/file

Be careful with -R because this will change also the subdirectories

To automate the work you can use something like. Be very carefull for the start directory because those commands can change permissions of files you do not want to touch

find /path/to/file -type f -exec chmod 644 {} \;

for files

find /path/to/dir -type d -exec chmod 755 {} \; 

for directories

6
  • I just edited the question: is this possible for whole dirs as well? I just did chown user1 ./ and chmod -R 644 ./ and now I don't have any access to the dir, even as user1.
    – Legatio
    May 19 at 7:11
  • @Legatio, please check my edited answer May 19 at 7:31
  • thanks now I can access the dir again. Is it possible to add x to the dir and subdirs but not the files in those dirs? I would want user2 to only read the files and not execute them.
    – Legatio
    May 19 at 7:38
  • @Legatio, yes, thats possible, see my edited answer May 19 at 7:40
  • how would I use the 2 find commands for the concrete example I added to my question?
    – Legatio
    May 19 at 7:57
0

The traditional Unix permissions are quite coarse-grained, distinguishing only between owner (user), group and others, where a user (more precisely, a process) can belong to several groups. Most current Linux installations create a private group for each user, to isolate users a bit more among themselves (many legacy Unix systems had a catchall group users for all users, so if e.g. I made some of my files group readable/writeable, everybody could have a day with them).

You can set up a group to which only user1 and user2 belong, and use group permissions to get some of what you want, but it isn't at all perfect.

Modern Linux kernel/filesystems/tools handle Access Control Lists (ACLs), see acl(5). Using ACLs you get more fine grained control, i.e., say "user1 has this exact permissions on object foo".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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