chmod [Special Bit][Permission] [File]
chmod -R [Special Bit][Permission] [Folder]
Recursive flag -R
All the files within folder also be granted execute permission at the same time.
Refer section X and x
Example: chmod -R 7775 /project/alpha
Special Bit
Example: SUID + SGID + Sticky Bit = 7
Permission
Permission = [Permission for Owner][Permission for Existing Group][Permission for Other users and groups]
Example for file:
Owner --> Read + write + execute = 4+2+1 = 7
Group --> Read + write = 4+2 = 6
Others --> Read = 4
If there's no execute permission, user can only read and edit files, but can't run the files as script
chmod 764 log.txt
Example for folder:
Owner --> Read + write + execute = 4+2+1 = 7
Group --> Read + write + execute = 4+2+1 = 7
Others --> Read + execute = 4+1 = 5
If there's no execute permission, user only can list directory content but can't open the folder
chmod 775 /project/alpha
chmod [Special Bit 1],[Special Bit 2],[Permission1 ],[Permission 2] [File]
chmod -R [Special Bit 1],[Special Bit 2],[Permission1 ],[Permission 2] [Folder]
Recursive flag -R is optional
All the files within folder also be granted execute permission at the same time.
Refer section X and x
Example: chmod -R u=rwx,g=rwx,o=rx,u+s,g+s,o+t /project/alpha
Special Bit
Example: SUID + SGID + Sticky Bit = u+s,g+s,o+t
Permission
Permission = [Reference ][Operator ][Mode]
If there's no reference, system will assume is a (all).
Example for file:
Owner --> Read + write + execute
Group --> Read + write
Others --> Read
If there's no execute permission, user can only read and edit files, but can't run the files as script
chmod u=rwx,g=rw,o=r log.txt
Example for folder:
Owner --> Read + write + execute
Group --> Read + write + execute
Others --> Read + execute
If there's no execute permission, user only can list directory content but can't open the folder
chmod u=rwx,g=rwx,o=rx /project/alpha
X and x
x = Grant execute permission to folder and files inside
X = Grant execute permission to folder but not files inside
Imagine you have a project folder with 100 subfolders and 1,000 images. You want to make sure you can "enter" all the folders, but you don't want your images to be marked as executable programs.
❌ chmod -R a+x project/
You can enter folders, but now 1,000 images are erroneously marked as "programs."
✅ chmod -R a+X project/
All 100 folders become enterable, but the 1,000 images stay exactly as they were.
ls -l [File]
ls -ld [Folder]
This command will return file/folder details, example
-rw-r--r--. 1 student student 30720 Apr 8 11:22 arc_log.tar
Check the first column
Example
Base permission in numeric mode
File: 666
Folder: 777
Formula For Final Permission
Final PermissionOCT = BaseOCT & (~umaskOCT)
& is AND gate, ~ is NOT gate
Example: umask = 022OCT
File: 666OCT &(~022OCT) = 644OCT
Folder: 777OCT &(~022OCT) = 755OCT
Formula For umask
umaskOCT = 777OCT - Target Permissions file/folderOCT
Example: Target Permissions for file = 600OCT, folder =700OCT
Step 1 Subtract
File: 777OCT - 600OCT = 177OCT
Folder: 777OCT-700OCT = 77OCT
Step 2 Verify
umask = 177OCT
File : 666OCT &(~177OCT) = 600OCT
Folder: 777OCT &(~177OCT) = 600OCT
umask = 77OCT
File : 666OCT &(~77OCT) = 600OCT
Folder: 777OCT &(~77OCT) = 700OCT
umask = 77OCT is suitable.
Tutorial
Checking current umask value
Numeric mode: umask
Symbolic mode: umask -S
Example result: 022
Create a new file
touch [New file]
Example: touch original_file.txt
Check the new file's permissions
ls -l [File]
Example: ls -l original_file.txt
Example result: -rw-r--r-- = 644 = 666 &(~022)
Change the umask value temporarily
umask [New value]
Example: umask 077
Create a second new file
touch [Second New file]
Example: touch secure_file.txt
Check the second file's permissions
ls -l [Second New file]
Example: ls -l secure_file.txt
Example result: -rw------- = 600 = 666 &(~077)
Change the umask value permanently
Open .bashrc by using vim editor : vim .bashrc --> Esc --> I --> Enter
Edit/Add new line umask [New value]: umask 077
Save and exit: Esc -->:wq --> Enter
Update setting immediately
source ~/.bashrc
umask
Result
student@localhost:~$ umask
0022
student@localhost:~$ umask -S
u=rwx,g=rx,o=rx
student@localhost:~$ touch original_file.txt
student@localhost:~$ ls -l original_file.txt
-rw-r--r--. 1 student student 0 May 25 12:42 original_file.txt
student@localhost:~$ umask 077
student@localhost:~$ touch secure_file.txt
student@localhost:~$ ls -l secure_file.txt
-rw-------. 1 student student 0 May 25 12:48 secure_file.txt
student@localhost:~$ vim .bashrc
student@localhost:~$ source ~/.bashrc
student@localhost:~$ umask
0077