Linux修改文件默认权限umask,linux/Centos/ubuntu下如何使用umask命令修改新建文件时的默认权限...

系统之后创建一个文件总是有一个默认权限的,使用umask设置了用户创建文件的默认权限,它与chmod的效果刚好相反,umask设置的是权限“补码”,而chmod设置的是文件权限码。一般在/etc/profile、$ [home]/.bash_profile或$[home]/.profile中设置umask值。

umask命令格式umask [-p] [-S] [mode]

umask 函数格式及头文件:#include

#include

mode_t umask(mode_t mask);

函数介绍如下:

DESCRIPTION

umask() sets the calling process’s file mode creation mask (umask) to mask & 0777.

The umask is used by open(2), mkdir(2), and other system calls that create files to modify the permissions placed on

newly created files or directories. Specifically, permissions in the umask are turned off from the mode argument to

open(2) and mkdir(2).

The constants that should be used to specify mask are described under stat(2).

The typical default value for the process umask is S_IWGRP | S_IWOTH (octal 022). In the usual case where the mode

argument to open(2) is specified as:

S_IRUSR | S_IWUSR | S_IRGRP | S_IWRGRP | S_IROTH | S_IWOTH

(octal 0666) when creating a new file, the permissions on the resulting file will be:

S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH

(because 0666 & ~022 = 0644; i.e., rw-r--r--).

RETURN VALUE

This system call always succeeds and the previous value of the mask is returned

umask命令及umask函数参数值计算方法

umask命令允许你设定文件创建时的缺省模式,对应每一类用户(文件属主、同组用户、其他用户)存在一个相应的umask值中的数字。对于文件来说,这一数字的最大值分别是6。系统不允许你在创建一个文本文件时就赋予它执行权限,必须在创建后用chmod命令增加这一权限。目录则允许设置执行权限,这样针对目录来说,umask中各个数字最大可以到7。

该命令的一般形式为:umask nnn

其中nnn为umask置000 - 777。我们只要记住umask是从权限中“拿走”相应的位即可。

umask参数值对于的目录文件权限如下图:

c1ca50eecc8ef060b326558eb653f52b.png

如:umask值为022,则默认目录权限为755,默认文件权限为644。

umask 命令用法举例:

1)查看系统当前的umask 值,可以使用umask命令:$umask

2)改变umask值:$ umask 002

3)确认系统是否已经接受了新的umask值:

$umask

002

$touch 169it.com

$ls -l 169it.com

rw- rw- r--

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值