UMASK in linux
What is Umask?
- A umask (user file-creation mode mask) determines the default permissions assigned to new files and directories when they are created.
- It works by subtracting these values from the maximum possible permissions.
Default Maximum Permissions
- Files: 0022
- Directories: 755
pavan@EPINHYDW1828:~$ touch EXAMPLEpavan@EPINHYDW1828:~$ mkdir -p EXAMPLE2pavan@EPINHYDW1828:~$ ls -ltr-rw-r--r-- 1 pavan pavan 0 Mar 23 17:52 EXAMPLEdrwxr-xr-x 2 pavan pavan 4096 Mar 23 17:52 EXAMPLE2pavan@EPINHYDW1828:~$ umask0022pavan@EPINHYDW1828:~$ umask -Su=rwx,g=rx,o=rxCommands Summary
Section titled “Commands Summary”| Command | Description |
|---|---|
umask | Check the current umask value in octal format. |
umask -S | Check the current umask in symbolic format (e.g., u=rwx,g=rx,o=rx). |
umask | Temporarily set a new umask value (e.g., umask 0022). |
source ~/.bashrc | Apply changes to umask made in the .bashrc file immediately. |
Examples
Section titled “Examples”1. Checking Current Default Permissions
Section titled “1. Checking Current Default Permissions”- To see your current umask setting:
umask- Output example:
0022
- Output example:
- To see the human-readable default permissions:
umask -S- Output example:
u=rwx,g=rx,o=rx
- Output example:
2. Calculating File Permissions
Section titled “2. Calculating File Permissions”- If umask is
022, new files are calculated as666 - 022 = 644.- File Permissions: 644 (
rw-r--r--)
- File Permissions: 644 (
3. Temporarily Changing Default Permissions
Section titled “3. Temporarily Changing Default Permissions”- To remove write permissions for the group and others for new files:
umask 0022
4. Permanently Changing Default Permissions
Section titled “4. Permanently Changing Default Permissions”- To make changes permanent, add the
umaskcommand to your shell configuration file (e.g.,~/.bashrcor~/.profile).- Edit the file:
nano ~/.bashrc - Add line at the end:
umask 0077(This makes files private to the user only). - Apply changes:
source ~/.bashrc
- Edit the file:
5. Modifying Specific Permissions (Symbolic Mode)
Section titled “5. Modifying Specific Permissions (Symbolic Mode)”- Instead of calculating numbers, you can directly set permissions for users (u), groups (g), or others (o).
- To remove read permissions for others for new files:
umask o-r