The chmod command ("change mode") changes who can read, write or execute a file. You can change a file's mode if you own it, or if you are root.
Symbolic mode
A symbolic change has three parts: who, operator and permissions.
- who:
u(owner),g(group),o(others),a(all three) - operator:
+adds,-removes,=sets exactly - permissions:
r,w,x
$ ls -l backup.sh
-rw-r--r-- 1 maya devs 91 Mar 3 11:00 backup.sh
$ chmod u+x backup.sh
$ ls -l backup.sh
-rwxr--r-- 1 maya devs 91 Mar 3 11:00 backup.shYou can join several changes with commas. This gives the group write access and takes all access from others:
$ chmod g+w,o= backup.sh
$ ls -l backup.sh
-rwxrw---- 1 maya devs 91 Mar 3 11:00 backup.shThe = operator replaces what was there for that class. + and - leave the other bits alone.
Numeric mode
Each permission has a value: read is 4, write is 2, execute is 1. Add them for each class, then write the three digits in the order owner, group, others.
| Digit | Meaning | |---|---| | 7 | rwx | | 6 | rw- | | 5 | r-x | | 4 | r-- | | 0 | --- |
$ chmod 640 notes.txt
$ ls -l notes.txt
-rw-r----- 1 maya devs 58 Mar 3 11:05 notes.txtA numeric mode sets all nine bits at once, so you do not need to know what they were before. Common choices are 644 for ordinary files, 755 for programs and directories, and 600 for private files such as SSH keys.
Changing whole trees
chmod -R applies a change to a directory and everything under it. Be careful: chmod -R 755 would make every ordinary file executable. A capital X helps here. It adds execute only to directories, and to files that already have execute permission for someone:
$ chmod -R u=rwX,go=rX projectAvoid chmod 777 as a quick fix. It lets any user on the system change the file.
Try it
$ touch demo.sh
$ chmod 600 demo.sh
$ chmod u+x,g+r demo.sh
$ ls -l demo.shPredict the string before you run ls. Then try the file and directory permissions lab.
Check your understanding
Modifying Permissions
All 4 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.