Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

As per the title, I have to create a file, for example:

/home/john/file.ini

I created a user and a group, myuser and mygroup, and set them to be the owner and group of this file.ini:

chown myuser:mygroup file.ini

I have another user named mymain. This user must be able to read this file. How can I change the permissions to allow this?

Important: I want to allow only the "mymain" user to read this file, NOT all other users.

So myuser (the owner of the file) and mymain should have access.

share|improve this question

migrated from stackoverflow.com Apr 26 '11 at 9:06

This question came from our site for professional and enthusiast programmers.

3  
Put both users in the same group (and there should be no other user in this group). Assign the the file to the group. Done. –  Felix Apr 26 '11 at 9:02

3 Answers 3

In the traditional Unix file permission system that's not possible: a file has only a single owner. You could create a group containing just the two users that should have access and make that the owning group of the file (and give the desired permissions to that group). This approach has some administrative overhead, however (not every user can create a group and place other users in it).

Many (most) modern filesystems support ACLs, however and they aren't too hard to use. If you want to use POSIX ACLs to give another user read (r) and write (w) permissions then you can use this command:

setfacl -m user:mymain:rw file.ini

Two caveats:

  • The necessary tools aren't always installed. On Ubuntu/Debian they are found in the acl package (sudo apt-get install acl)
  • Even if the filesystem used supports ACLs, it might be that your local filesystem is mounted without support for ACLs. In that case you need to modify the mount parameters to enable it (for ext4, for example, that simply means appending ,acl to the mount options in /etc/fstab).
share|improve this answer

You should make sure that user mymain is a member of mygroup and give the group permissions, like

chmod g+r <filename>

Some filesystems implement ACL (access control lists), and these may proof to be much more useful to you, but they are beyond the scope of my answer here. You will want to read up on ACLs in an enterprise (ADS) setting_

added

Technically, of course, two users cannot simultaneously be the owner of the same file. Also, if you want to mimic that, you should give full permission to the group, perhaps making a special group for just these two 'owners' and the extent to which this actually mimics owernship will be paltform dependent (old-style UNIX-en tend to be very strict in their rules for allowing chmod/chown)

share|improve this answer

Use the Group facility of Unix permissions.

I explain more about Unix file permissions here:

http://blog.superuser.com/2011/04/22/linux-permissions-demystified/

share|improve this answer
    
The second answer here is wrong. A hard link is a second directory entry that points to the same inode. The file does not “exist twice”. Since all file metadata other than the name (including owner, group, mode (permissions number), and ACL) are in the inode, this means that a file and a hard link to that file cannot have different owners. –  Scott Oct 20 at 20:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.