0

umask shows the weird results when applying value: 0666

When apply any value for umask that works as aspected, but when apply 0666 then directory got correct permission value but file got weird value.

$ mkdir demo
$ cd demo
$ umask 0666 ../demo
$ mkdir d1 && touch f1
$ umask 0333 ../demo
$ mkdir d2 && touch f2
$ ls -l

drwxrwxr-x 4 riajul riajul 4096 Mar 27 17:33 ./
drwxr-xr-x 4 riajul riajul 4096 Mar 27 17:30 ../
d--x--x--x 2 riajul riajul 4096 Mar 27 17:32 d1/
dr--r--r-- 2 riajul riajul 4096 Mar 27 17:33 d2/
---------- 1 riajul riajul    0 Mar 27 17:32 f1
-r--r--r-- 1 riajul riajul    0 Mar 27 17:33 f2

Why f1's permission different than d1

Example: picture

enter image description here

1
  • umask only takes one argument -- it applies to the user, not any specific directory. And it masks out permission, not sets them. 666 masks out rw, but files do not get x by default. 333 masks out wx and leaves r. Directories get x by default (for them, it means searchable). Mar 27 at 11:59

2 Answers 2

1

umask is a mask, applied to file permissions. touch creates files with permission 666 by default; with a mask of 0666, all those permission bits are masked, so you end up with files with no permission bits set.

The umask isn’t set on specific directories, it is managed per process and inherited from the parent process when a process starts. When you run umask 0666 or umask 0333, the current shell’s umask is set to the given value; extra arguments are ignored.

2
  • Did touch add permission while creating file ? or file inherit permission form parent ? Mar 27 at 12:00
  • As mentioned in this linked answer, touch explicitly sets permission 666 (which is subsequently masked by the current umask). Mar 27 at 12:05
1

umask created some confusion, so after reading some documentation I got a clear idea.

umask: [ u => User ]

  • When we create files or directories, those files got some default permission.
  • mask work exactly as do in photo editing tools. It does not set permissions, instead it remove given permissions.

Default_permission = pre-defined_initial_permission – umask_permission

Pre-defined permission are fixed and cannot be changed.

  • for directory: 777
  • for file : 666

Default umask permissions for user:

  • root user : 0022
  • normal user : 0002

So Without any change in default umask permissions:

+---------------------------------------------------+
|   User    |   Directory       |   File            |
|---------------+-----------------------+-----------|
|                                                   |
| <user>    | 777 - x   => y    | 666 - x   => y    |
| root      | 777 - 022 => 755  | 666 - 022 => 644  |
| normal    | 777 - 002 => 775  | 666 - 002 => 664  |
|                                                   |
|                                                   |
| NB: 1st bit (0) of umask values: not effect       |
|    permission so it ignored from calculation.     |
|                                                   |
|                                                   |
+---------------------------------------------------+

Umask permission change can be 2 type:

  • Temporary : by umask command, closing shell reset to default.
  • Permanent : By setting UMASK value in:
    • User Level: ~/.profile, or .bashrc
    • Globally: /etc/profile, /etc/bashrc, or /etc/login.defs

NB: umask substract permissions from user not from particular directory.

  • umask: set permission before created.
  • chmod: Change permission after created.
$ umask --help
    -S : makes the output symbolic; otherwise an octal number is output
    -p : if MODE is omitted, output in a form that may be reused as input


$ umask     # Show umask of user
$ umask -S  # Show umask of user as symbol instead of number

$ umask 0022 # Set Umask.
3
  • I edited your answer a bit. Code brackets { } should only enclose code or other pre-formatted text. Please verify I didn't change the intention. Nothing awkward in answering your own question, if you find the solution you definitely should write an answer :-) Mar 28 at 19:59
  • thanks for your effort, and I used the { } to make my code formatted, when I copy-paste text from my editor to this editing editor my format brokes that's the reason I use {} just format the code. I guess I need to learn a little bit more about writing questions and answering them. Mar 29 at 10:05
  • You're welcome :-) The Help Center does contain just about everything there is to know about the subject. Searching also returns results from discussions in the Meta for things that might not be immediately found. I should thank you - that explained umask for me :-D Mar 29 at 10:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .