To change file or directory permissions in Linux, you use the chmod (Change Mode) command.
There are two common ways to use it: Symbolic Mode (using letters) and Absolute/Numeric Mode (using numbers).
1. Symbolic Mode (Easiest to Remember)
You use letters to represent who you are changing permissions for and what you want to do.
- Who:
u(user/owner),g(group),o(others),a(all). - What:
+(add),-(remove),=(set exactly). - Permission:
r(read),w(write),x(execute).
Examples:
chmod u+x file1: Add execute permission for the owner.chmod g-w file1: Remove write permission from the group.chmod a+r file1: Give read permission to everyone.
2. Numeric Mode (Fastest for Experts)
You use a three-digit number. Each digit represents a specific person (Owner, Group, Others).
- 4 = Read
- 2 = Write
- 1 = Execute
- 0 = No permission
You add these numbers together to get the permission level:
- 7 (4+2+1) = Read, Write, and Execute (Full access)
- 6 (4+2) = Read and Write
- 5 (4+1) = Read and Execute
- 4 = Read only
Common Examples:
chmod 777 file1: Everyone can do everything (usually unsafe!).chmod 755 script.sh: Owner can do everything; others can only read and run it.chmod 644 file.txt: Owner can read/write; others can only read.
How to try it now:
If you have file1 in your current directory, try this:
- Check the current permissions:
ls -l file1 - Remove all permissions from others:
chmod o-rwx file1 - Check again to see the difference:
ls -l file1
Note: Since you have sudo privileges in this lab, if you ever run into a "Permission Denied" error while trying to change permissions on system files, you can use sudo chmod ....
Would you like to try changing the permissions of one of the files you created earlier?