The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
  

umask - Changing Default File Permissions





By default, when regular users create files, the permissions are set to "rw-rw-r--," and when they create directories, the permissions are set to "rwxrwxr-x." You might wonder why these default permissions are set this way, and can you change them? The umask command allows you to modify these default permissions.

For example, let's take a look at what umask displays when you run it:

$ umask
0002

What does the output "0002" mean? The numbers output by umask correspond to the permissions that will be removed when creating files or directories. Here's a table to explain it:

umask Default File Permissions Default Directory Permissions
000
rw- rw- rw-
rwx rwx rwx
002
rw- rw- r--
rwx rwx r-x
022
rw- r-- r--
rwx r-x r-x
027
rw- r-- ---
rwx r-x ---
077
rw- --- ---
rwx --- ---
277
r-- --- ---
r-x --- ---

For instance, when umask displays "0022," it means that when regular users create files, the permissions will be "rw-r--r--," and for directories, they will be "rwxr-xr-x."

When running umask as the root user, the output is typically "0022," resulting in files with "rw-r--r--" permissions and directories with "rwxr-xr-x" permissions by default.

If you want to change the default permissions for creating files or directories, you can use the umask # command followed by a mask number (where # is a number from the table above).

For example:
$ umask 0

This will set the default file permissions for future file creations to "rw-rw-rw-" and directory permissions to "rwxrwxrwx."

For more information on the principles behind umask and additional examples, you can refer to the provided link.