Linux下umask命令的用法

奮起直追CDS發表於2016-01-12

umask設定新文件的預設屬性,有字元表達形式(Symbolic values)和八進位制表達形式(Octal values)兩種。

1 為所有使用者統一修改預設的umask
You can setup umask in /etc/bashrc or /etc/profile file for all users. By default most Linux distro set it to 0022 (022) or 0002 (002). Open /etc/profile or ~/.bashrc file, enter:

# vi /etc/profile

OR

$ vi ~/.bashrc

Append/modify following line to setup a new umask:

umask 022

Save and close the file. Changes will take effect after next login. All UNIX users can override the system umask defaults in their /etc/profile file, ~/.profile (Korn / Bourne shell) ~/.cshrc file (C shells), ~/.bash_profile (Bash shell) or ~/.login file (defines the user’s environment at login).

2 常用的umask

1)A umask of 022 allows only you to write data, but anyone can read data.
2)A umask of 077 is good for a completely private system. No other user can read or write your data if umask is set to 077.
3)A umask of 002 is good when you share data with other users in the same group. Members of your group can create and modify data files; those outside your group can read data file, but cannot modify it.
4)Set your umask to 007 to completely exclude users who are not group members.

3 數字的詳細含義
Octal value : Permission
0 : read, write and execute
1 : read and write
2 : read and execute
3 : read only
4 : write and execute
5 : write only
6 : execute only
7 : no permissions
這裡寫圖片描述

To set the umask 077 type the following command at shell prompt:

$ umask 077
$ mkdir dir1
$ touch file
$ ls -ld dir1 file

Sample outputs:

drwx------ 2 vivek vivek 4096 2011-03-04 02:05 dir1
-rw------- 1 vivek vivek    0 2011-03-04 02:05 file

理解上:
**(1)講umask看做【Owner,Group,Others】三個物件的原始預設屬性中需要去掉哪些許可權(r=4, w=2, x=1, -=0)。例如,umask=003則表示Owner不用去掉任何許可權,Group不需要去掉任何許可權,Others需要去掉2+1即為w和x許可權。
(2)檔案的原始預設屬性是666(-rw-rw-rw-),目錄的原始預設屬性為777(drwxrwxrwx)。**


4 用字元表達形式設定umask
r : read
w : write
x : execute
u : User ownership (user who owns the file)
g : group ownership (the permissions granted to other users who are
members of the file’s group)
o : other ownership (the permissions granted to users that are in
neither of the two preceding categories)

The following command will set umask to 077 i.e. a umask set to u=rwx,g=,o= will result in new files having the modes -rw——-, and new directories having the modes drwx——:

$ umask u=rwx,g=,o=
$ mkdir dir2
$ touch file2
$ ls -ld dir2 file2

相關文章