i wonder, why nobody has written about it ...

How can i grant permission for files to specific user or specific group ??

Updated:

We have 3 groups: "g12" ("u1" and "u2), "g34" and "g56".

"g12" should only read the file.

"g34" should write and read it.

"g56" should have all permissions (rwx).

And others should not access the file at all.

link|improve this question

56% accept rate
3  
Why not start accepting some answers? – Mitch Dempsey May 25 '10 at 8:53
I agree with @webdestroya, and additionally I'd strongly advice you to read my answer. I takes care of your update too. With Access Control Lists you get what you fine grained control on file and directory access. – Peter Jaric May 31 '10 at 19:47
feedback

3 Answers

up vote 1 down vote accepted

You need to use Access Control Lists. They are a more advanced way of handling permissions than the default user/group/other way in Linux. See this page for example: Ubuntu Access Control Lists

An example from that page:

setfacl -m u:mike:rwx file or directory

I've only used these commands in a lab on an server adminstration course myself, but as far as I could see, it's a pretty easy way to do it.

link|improve this answer
feedback

short answer is, you can't

you don't set permissions for groups or users, you set permissions for files. A file has an owner and a group, and you can set the "read" "write" and "execute" permissions seperately for one user, one group, and everyone else.

Can you give an example of what it is you're wanting to do?

link|improve this answer
Sorry, i wrote my question not correct. I want to grant, say, only read permission for file (f1.txt) only for one user "u1" – AntonAL May 25 '10 at 8:57
corrected my question – AntonAL May 25 '10 at 8:58
I have read man, but i don't understood it. It states only about groups, users, owners and other. How can i exactly specify, to which group (or user) i grant permission to ? – AntonAL May 25 '10 at 9:01
A file has one owner, and one owning group, which you can see when doing ls -l and change with chown. You set the permissions for this user and this group (and all others). – DevSolar May 25 '10 at 11:48
feedback

As root:

chown u1:u1 f1.txt
chmod 400 f1.txt

This will ensure that the file is owned by user u1 (chown) and group u1 (assuming that user u1's default group is u1) and that only that user can read the file (chmod). If you would like the user to be able to write to the file, change 400 to 600.

You can add a second user to the default group of the first user with:

useradd -G u1 u2

The above command assumes that user u1's default group is also called u1 and the second user is u2.

Now we change the permissions on f1.txt to allow members of group u1 read access (the second "4" in 400 is group permissions):

chmod 440 f1.txt

Each of the three digits following the chmod command represents the permissions for the owner (first digit), the group (second digit), and all other users (third digit) on the system. A value of 1 is the execute permission, 2 is the write permission and 4 is the read permission. You add add these numbers together to "mix" these three types of permissions. Example, 4 (read) + 1 (execute) = 5, so to allow the owner and the group to read and execute the file but restrict access to everyone else you would use 550, to allow the owner to read and write to the file and the group and all other users to read the file, you would use 644.

There are other formats to setting permissions, type:

man chmod

at the command prompt for more details.

link|improve this answer
Thanks. OK, and how can i grant to "u2" the same right ? Should i use "chown u2:u2 f1.txt && chmod ..." ?? I really, don't understand the main conception ... For example, when i asking my friend to bring something from my home, i give them a key. But i don't present my home to he :) – AntonAL May 25 '10 at 9:53
That would change the owner to u2 and the group to user u2's default group. If you do that and chmod 400 then u1 would no longer be able to access the file. I've added some more detail to my answer, hope that helps. – Stacey Richards May 25 '10 at 10:39
See my answer for a way to add a user without removing the existing ones. It should work at least if you are running Ubuntu. – Peter Jaric May 25 '10 at 18:12
Thanks, now i understood the main idea. But, as far, as i understand it - changing permission is performed in scope of only ONE group. In our example, u1 and u2 are in one group, say "g12". But, what, if another group "g34" is coming (u3 and u4), that must have different permissions ? To completely figure this question out, please say, how to solve the task, described in my question (updated) – AntonAL May 27 '10 at 13:55
feedback

Your Answer

 
or
required, but never shown

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