🖥️chmod

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the chmod command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#                 ██████╗██╗  ██╗███╗   ███╗ ██████╗ ██████╗ 
#                ██╔════╝██║  ██║████╗ ████║██╔═══██╗██╔══██╗
#                ██║     ███████║██╔████╔██║██║   ██║██║  ██║
#                ██║     ██╔══██║██║╚██╔╝██║██║   ██║██║  ██║
#                ╚██████╗██║  ██║██║ ╚═╝ ██║╚██████╔╝██████╔╝
#                 ╚═════╝╚═╝  ╚═╝╚═╝     ╚═╝ ╚═════╝ ╚═════╝ 
                                                            
																				
																				
# Add execute for all (myscript.sh)
chmod a+x myscript.sh

# Set user to read/write/execute, group/global to read only (myscript.sh), symbolic mode
chmod u=rwx, go=r myscript.sh 

# Remove write from user/group/global (myscript.sh), symbolic mode
chmod a-w myscript.sh

# Remove read/write/execute from user/group/global (myscript.sh), symbolic mode
chmod = myscript.sh

# Set user to read/write and group/global read (myscript.sh), octal notation
chmod 644 myscript.sh

# Set user to read/write/execute and group/global read/execute (myscript.sh), octal notation
chmod 755 myscript.sh

# Set user/group/global to read/write (myscript.sh), octal notation
chmod 666 myscript.sh

# Roles
u - user (owner of the file)
g - group (members of file is group)
o - global (all users who are not owner and not part of group)
a - all (all 3 roles above)

# Numeric representations
7 - full (rwx)
6 - read and write (rw-)
5 - read and execute (r-x)
4 - read only (r--)
3 - write and execute (-wx)
2 - write only (-w-)
1 - execute only (--x)
0 - none (---)

# chmod-usage
#------------------------------------#
# format <user,group,other> 
			7 read, write and execute 111
			6 read and write 110
			5 read and execute 101
			4 read only 100
			3 write and execute 011
			2 write only 010
			1 execute only 001
			0 none 000

			u user the owner of the file
			g group users who are members of the file's group
			o others users who are neither the owner of the file nor members of the file's group
			a all all three of the above, same as ugo

			r read read a file or list a directorys contents
			w write write to a file or directory
			x execute execute a file or recurse a directory tree
			X special execute
			s setuid/gid details in Special modes section
			t sticky details in Special modes section

chmod -R u+rwX,g-rwx,o-rx PersonalStuff
chmod -R a+x,a-rw TEST

usermod -m -d /home/newUserName -l newUserName oldUserName 
groupmod --new-name newUserName oldUserName 
chfn -f "First Last" newUserName 
# Rename linux user - Easy commands to rename a user, including home directory
######################

# Chmod command Examples in UNIX and Linux
# Now let us see some practical and frequently used example of chmod command in UNIX

# chmod command Example 1: making read only file in Unix
# In this example of chmod command in UNIX we will see how to make a file read only by only providing read access to owner. You can also give read access to group and others and keep write access for owner which we will see in subsequent examples.

ls -lrt stock_trading_systems
    -rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*

# Here file stock_trading_systems has read, write and execute permission "-rwxrwxrwx" for all

chmod 400 stock_trading_systems

# 400 means 100 000 000 means r-- --- --- i.e. read only for owner

ls -lrt stock_trading_systems
    -r-------- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems

# Now file is read only and only owner can read it "-r--------"

# chmod command Example 2:  change permissions only for user, group or others.
# In this example of chmod command we will see how to change file permissions on user, group and others level. You can easily modify file permission for any of these classes. If you are using text format than “u” is user , “o” is other and “g” is group also “r” is read , “w” is write and “x” is execute. + means adding permission and “-“is removing permission.

ls -lrt chmod_examples
    -r-------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

chmod u+w chmod_examples

ls -lrt chmod_examples
    -rw------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

# Now let’s change file permissions only for group by using chmod command

ls -lrt chmod_examples
    -rw------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

# chmod g+w chmod_examples

ls -lrt chmod_examples
    -rw--w---- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

# In this chmod command example we will change permission only for others class without affecting user and group class.

ls -lrt chmod_examples
    -rw--w---- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

chmod o+w chmod_examples

ls -lrt chmod_examples
    -rw--w--w- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

# Chmod command Example 3:  change file permissions for all (user + group + others)
# In last unix chmod example we learn how to change permission for user, group and others individually but some time its convenient to change permissions for all instead of modifying individual permission for user, group and other.
# If you are providing permission in text format than “a” is used for “all” while “u” is used for user.

ls -lrt linux_command.txt
    -rw--w--w- 1 example Domain Users 0 Jul 15 11:42 linux_command.txt

chmod a+x linux_command.txt

ls -lrt linux_command.txt
    -rwx-wx-wx 1 example Domain Users 0 Jul 15 11:42 linux_command.txt*

# Chmod command Example 4: Changing permissions in numeric format of chmod command
# Chmod command in UNIX and Linux allows modifying permissions not just on text format which is more readable but also on numeric format where combination of permissions are represented in octal format e.g. 777 where first digit is for user, second is for group and 3rd is for others. Now if you write down 1st digit in binary format it will be written as 111 on which 1st digit is for read permission, 2nd is for write and 3rd is for execute permission.

ls -lrt unix_command.txt
    -rw--w--w- 1 example Domain Users 0 Jul 15 11:42 unix_command.txt

chmod 777 unix_command.txt

ls -lrt unix_command.txt
    -rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 unix_command.txt*

# Chmod command Example 5: How to remove file permission using chmod command Unix
# In this example of chmod command in UNIX we will see how to remove various permissions from files. You can easily remove read, write or execute permission from file using chmod command in both numeric and text format. Below examples shows removal of execute permission represented by –x in text format.

ls -lrt linux_command.txt
    -rwx-wx-wx 1 example Domain Users 0 Jul 15 11:42 linux_command.txt*

chmod a-x linux_command.txt

ls -lrt linux_command.txt
    -rw--w--w- 1 example Domain Users 0 Jul 15 11:42 linux_command.txt

# Chmod command Example 6: changing permission for directory and subdirectory recursively in Unix
# This is the most frequently used example of chmod command where we want to provide permission to any directory and all contents inside that directory including files and sub directories. By using –R command option of chmod in Unix you can provide  permissions recursively to any directory as shown in below example of chmod command.

ls -lrt
    total 8.0K
    -rwxrwxrwx  1 example Domain Users    0 Jul 15 11:42 unix_command.txt*
    drwxr-xr-x+ 1 example Domain Users    0 Jul 15 14:33 stocks/

chmod -R 777 stocks/

ls -lrt
    total 8.0K
    -rwxrwxrwx  1 example Domain Users    0 Jul 15 11:42 unix_command.txt*
    drwxrwxrwx+ 1 example Domain Users    0 Jul 15 14:33 stocks/

ls -lrt stocks
    total 0
    -rwxrwxrwx 1 example Domain Users 0 Jul 15 14:33 online_stock_exchanges.txt*

# Chmod command Example 7:  How to remove read and write access from file for all
# So far we have been seeing how to provide read, write and execute permission to file and directory in UNIX and now we will see opposite of that i.e. how to remove read, write and execute access. Its simple in text format because instead of + we are going to use -. Just like + used to add permission – will be used to remove permissions.

ls -lrt stock_trading_systems
    -rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*

chmod a-wx stock_trading_systems

ls -lrt stock_trading_systems
    -r--r--r-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems

# Chmod command Example 8: setting execute permission only on directories without touching files

# Many times we just want to provide directory or subdirectory execute permission without modifying permissions on file just to make those directories searchable. Until I know this command I used to do this by finding all directory and then changing there execute permission but we have a better way to do it by using chmod command in UNIX. You can use "X" (capital X) options to provide execute permission to only directories without touching files. Let’s see an example of chmod command for that:

ls -lrt
    total 8.0K
    -r--r--r--  1 example Domain Users    0 Jul 15 11:42 stock_trading_systems
    drw-rw-rw-+ 1 example Domain Users    0 Jul 15 14:33 stocks/

chmod a+X *

ls -lrt
    total 8.0K
    -r--r--r--  1 example Domain Users    0 Jul 15 11:42 stock_trading_systems
    drwxrwxrwx+ 1 example Domain Users    0 Jul 15 14:33 stocks/

# Remember to use X (capital case) if you use x (small case) it will affect all files and directories.

# Chmod command Example 9:  changing mutiple permission for a file or directory in Unix or Linux
# You can change combination of user + groups or groups+ other in one command to modify permissions of files and directory. Below example of chmod command just doing same it’s providing execute permission for user and read, execute permission

ls -lrt
    total 8.0K
    -r--r--r--  1 example Domain Users    0 Jul 15 11:42 stock_trading_systems
    drwxrwxrwx+ 1 example Domain Users    0 Jul 15 14:33 stocks/

chmod u+x,g+x stock_trading_systems

ls -lrt stock_trading_systems
    -r-xr-xr-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*

# Chmod command Example 10: How to copy permission from one file to another in Unix
# This is very interesting example of chmod command in UNIX which copies permission from one file to another. You can easily reference source file and copy all permissions on that file to destination file as shown in following chmod example:

ls -lrt future_trading
    -rwxrwxrwx 1 example Domain Users 0 Jul 15 15:30 future_trading*

ls -lrt stock_trading_systems
    -r--r--r-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems

chmod --reference=stock_trading_systems future_trading

ls -lrt future_trading
    -r--r--r-- 1 example Domain Users 0 Jul 15 15:30 future_trading

chmod +x http://program.py  
# Quick and easy way to add execute permissions to *all* groups on a file when you don't care about the number.

# find and chmod
find . -type d -exec chmod 0755 {} \;
find . -type f -exec chmod 0644 {} \;
find . -type d -user harry -exec chown daisy {} \;
find . -name "*.bash" -execdir chmod u+x {} +

chmod 777 !*
# !* Tells that you want all of the *arguments* from the previous command to be repeated in the current command
# Example: touch file{1,2,3}; chmod 777 !*

#==============================##==============================#
# CMD chmod						       #
#==============================##==============================#
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░