分享

Linux文件默认权限和umask笔记

 IT技术分享社区 2022-10-20 发布于江苏

关于Linux文件默认权限的问题,可以实际先尝试一下如下命令:

root用户登录

[root@localhost test]# touch file1

[root@localhost test]# ls-l file1

-rw-r--r-- 1 root root 0 May  5 08:28 file1 #输出结果 对应的数字权限 644

[root@localhost test]# touch file2

[root@localhost test]# ls-l file2

-rw-r--r-- 1 root root 0 May  5 08:29 file2 #输出结果 对应的数字权限 644

[root@localhost test]# mkdir dir1

[root@localhost test]# ls-ld dir1

drwxr-xr-x 2 root root 4096 May  5 08:29 dir1 #输出结果 对应的数字权限 755

[root@localhost test]# mkdir dir2

[root@localhost tmp]# ls-ld dir2

drwxr-xr-x 2 root root 4096 May  5 08:29 dir2 #输出结果 对应的数字权限 755

user1用户登录

[user1@localhost test]# touch file1

[user1@localhost test]# ls-l file1

-rw-rw-r-- 1 root root 0 May  5 08:28 file1 #输出结果 对应的数字权限 664

[user1@localhost test]# touch file2

[user1@localhost test]# ls-l file2

-rw-rw-r-- 1 root root 0 May  5 08:29 file2 #输出结果 对应的数字权限 664

[user1@localhost test]# mkdir dir1

[user1@localhost test]# ls-ld dir1

drwxrwxr-x 2 root root 4096 May  5 08:29 dir1 #输出结果 对应的数字权限 775

[user1@localhost test]# mkdir dir2

[user1@localhost tmp]# ls-ld dir2

drwxrwxr-x 2 root root 4096 May  5 08:29 dir2 #输出结果 对应的数字权限 775

通过上面的执行结果可以得出以下结论:如果是root用户创建的文件默认权限是644,目录默认权限是755;普通用户创建的文件默认权限是664,目录默认权限是775.两者的默认权限是不同的,造成两者用户权限不同的原因就是Linux针对不同的用户创建文件和创建目录默认的权限不同,Linux系统通过umask(遮罩)的概念来控制相应的权限。可以在/etc/profile 文件中进行查看。

内容如下(51-55行):

if [ $UID-gt 99 ] && [ "`id-gn`" = "`id-un`" ]; then

    umask 002

else

    umask 022

fi

通过上面的文件内容可以看出:如果UID>99 设置的umask值为002,如果UID不大于99则umask值为022.关于遮罩计算权限的方式如下:比如 777 用字符串表示 rxwrwxrwx,如果遮罩值是022 对于的字符串是 ----w--w-,计算方法是如果遮着包含字母的,计算出真正的权限就不包含该位置的字母用-代替即可,个人理解公式:遮罩值+计算的真正权限=rxwrwxrwx

上面的权限可以这样理解: ----w--w-+rxwr-xr-x=rxwrwxrwx

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多