Linux Command Line · Lesson 24 of 43
Changing permissions with chmod
Change file permissions with chmod using symbolic and octal modes, make scripts executable, protect private files and use -R carefully.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will change who can read, write and run a file, make your own scripts executable and lock down private files. You will also know the two notations that every guide and error message uses.
Two ways to say the same thing
chmod (change mode) accepts a symbolic form that adjusts specific bits, and an octal form that sets everything at once. Both give the same result. Always check afterwards with stat or ls -l. To keep this lesson's output stable, we start by setting the default umask and creating a practice file:
cd ~/project
umask 022
touch report.txt
stat -c '%A %a %n' report.txt-rw-r--r-- 644 report.txtSymbolic mode
A symbolic mode has three parts: who, an operator and the permissions.
| Part | Choices |
|---|---|
| Who | u owner (user), g group, o others, a all |
| Operator | + add, - remove, = set exactly |
| Permissions | r, w, x |
chmod u+x report.txt
stat -c '%A %a' report.txt
chmod go-r report.txt
stat -c '%A %a' report.txt
chmod a+r,u=rw report.txt
stat -c '%A %a' report.txt-rwxr--r-- 744
-rwx------ 700
-rw-r--r-- 644The first command added execute for the owner. The second removed read from group and others. The third combined two changes separated by a comma: it gave everyone read, and set the owner to exactly rw-. Omitting the "who" means "all, modified by the umask", so spell it out.
Octal mode
Octal mode takes three digits for owner, group and others. Using the values from the previous lesson (r=4, w=2, x=1):
chmod 600 report.txt
stat -c '%A %a' report.txt
chmod 644 report.txt
stat -c '%A %a' report.txt
chmod 755 report.txt
stat -c '%A %a' report.txt-rw------- 600
-rw-r--r-- 644
-rwxr-xr-x 755Memorise a few modes and what they are for:
- 644: an ordinary file; the owner edits it, everyone can read it.
- 755: a program or folder; the owner edits it, everyone can run or enter it.
- 600: a private file such as an SSH key; only the owner can read or write.
- 700: a private folder or script.
- 664/775: like 644/755, but writable by the group as well, for shared projects.
Making a script executable
A new script file is not executable. Running it fails until you add x:
printf '#!/bin/bash\necho "It works"\n' > greet.sh
./greet.shbash: ./greet.sh: Permission deniedchmod +x greet.sh
./greet.shIt worksThe message Permission denied appeared because the file lacked x. After chmod +x the shell may run it. The first line, #!/bin/bash, is the shebang, and it tells the system which program interprets the file; the scripting lessons come back to it. You can also run an unexecutable script by naming the interpreter: bash greet.sh needs only read permission.
Folders and recursion
chmod -R applies a mode to a folder and everything inside it. This is powerful, and a single mode is rarely right for both files and folders, since folders need x and ordinary files usually should not have it. The capital X solves this: it adds execute only to directories (and to files that already have it):
mkdir -p shared/sub
touch shared/a.txt shared/sub/b.txt
chmod -R u=rwX,go=rX shared
stat -c '%A %n' shared shared/a.txt shared/sub shared/sub/b.txtdrwxr-xr-x shared
-rw-r--r-- shared/a.txt
drwxr-xr-x shared/sub
-rw-r--r-- shared/sub/b.txtAnother common approach uses find to treat the two kinds separately: find shared -type d -exec chmod 755 {} + and find shared -type f -exec chmod 644 {} +.
Who may change permissions?
Only the file's owner and root may run chmod on it. Changing the owner or group needs root: sudo chown asha:developers file, and chown -R for a tree. chgrp changes only the group.
Dangerous habits
chmod 777gives every user full control. When something "doesn't work", the answer is almost never 777. Find the missing permission instead.chmod -R 777 /or any-Ron a system folder can break the whole machine, because many programs refuse to start when system files are writable by others.- Setuid bits (
chmod u+s) should not be set by beginners. - Private keys: SSH refuses to use a private key that others can read. Use
chmod 600.
Common mistakes
Mixing up symbolic and octal syntax. chmod u+644 is invalid. Pick one form.
Forgetting x on a folder. Without it, you cannot cd into the folder or open files inside it.
Applying a file mode to a whole tree. chmod -R 644 folder removes x from the folder itself, locking you out. Use X or find.
Using chmod on a link. It changes the target, not the link.
Exercise
Create secret.txt and make it readable and writable only by you, then verify with stat. Create tool.sh that prints a message, make it executable only for the owner, and run it. Finally, create a folder with a subfolder and files, and set folders to 755 and files to 644 using find and chmod.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.