Skip to lesson

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.

Beginner to intermediate·12 min·Users and permissions·Updated 2026-11-24

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:

bash
cd ~/project
umask 022
touch report.txt
stat -c '%A %a %n' report.txt
result
-rw-r--r-- 644 report.txt

Symbolic 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
bash
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
result
-rwxr--r-- 744
-rwx------ 700
-rw-r--r-- 644

The 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):

bash
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
result
-rw------- 600
-rw-r--r-- 644
-rwxr-xr-x 755

Memorise 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:

bash
printf '#!/bin/bash\necho "It works"\n' > greet.sh
./greet.sh
result
bash: ./greet.sh: Permission denied
bash
chmod +x greet.sh
./greet.sh
result
It works

The 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):

bash
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.txt
result
drwxr-xr-x shared
-rw-r--r-- shared/a.txt
drwxr-xr-x shared/sub
-rw-r--r-- shared/sub/b.txt

Another 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 777 gives 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 -R on 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.

0 / 4 understood
01 What does chmod u+x run.sh do?
02 Which octal mode leaves only the owner able to read and write?
03 What does chmod go-w file do?
04 Why is chmod 777 a bad idea?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses