Linux File Access Permissions

 

4.5 of Advanced Programming in the UNIX programming has a very clear explanation for file access permissions.

 

Here is an experiment to show the explanation. 

 

 

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, const char *argv[]) 
{
    int fd;

    if ((fd = open("data", O_RDONLY)) < 0) {
        printf("open error\n");
        return 1;
    } else {
        printf("open ok\n");
        return 0;
    }
}
 

 

$ gcc code.c

$ echo abc > data

$ ls -l

 

-rwxr-xr-x 1 jing jing 7174 2010-10-09 17:27 a.out

-rw-r--r-- 1 jing jing  302 2010-10-09 17:19 code.c

-rw-r--r-- 1 jing jing    4 2010-10-09 17:26 data


$ /a.out
open ok

$ chmod u-r data
$ /a.out
open error
$ id
uid=1000(jing) gid=1000(jing) groups=4(adm),20(dialout),24(cdrom),46(plugdev),104(lpadmin),115(admin),120(sambashare),121(vboxusers),125(kvm),126(libvirtd),1000(jing)

Here is the tricky part. If the effective user id is equal to the file owner id and it does not have the read permission, the permission is denied even the effective group id is equal to the file group id and does  has the read permission.

$ sudo chown test data
$ ./a.out
open ok
$ sudo chown :kvm data
$ ./a.out
open ok

kvm is a supplementary group of the current user jing. So the file can be still opened after its group being set to kvm.

$ sudo chmod g-r data
$ ./a.out
open error

$ sudo chown :fax data
$ ./aout
open ok

$ sudo chmod o-r data
$ ./a.out
open error
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值