Permissions
By the end of this lesson you'll be able to read any file's -rwxr-xr-x permission string at a glance, change permissions with chmod (both ways), reassign ownership with chown , use sudo safely, and turn a plain text file into a script you can actually run.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free Cli course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
1️⃣ Reading the rwx Permission String
Run ls -l and every line begins with 10 characters like -rwxr-xr-x . The first character is the file type ( - for a normal file, d for a directory). The remaining nine are three groups of three — user (the owner), group , and other (everyone else) — and within each group the three slots are always read , write , execute in that order. A letter means the permission is granted; a dash - means it isn't.
Octal notation just adds the values: rwx = 4+2+1 = 7 , r-x = 4+0+1 = 5 , r-- = 4+0+0 = 4 . So -rwxr-xr-- is 754 . Three digits, one per group — that's the whole trick.
2️⃣ Changing Permissions with chmod
chmod ("change mode") re-cuts the keys, and it works two ways. Symbolic mode adjusts one thing relative to what's there: chmod u+x file adds ( + ) execute ( x ) for the user ( u ); g is group, o is other, a is all. Octal mode sets all three groups at once to an exact value: chmod 644 file . Use symbolic for a quick tweak, octal to declare a known final state. Run the worked example and watch the string change with each command.
Now compute one yourself. The program below is almost complete — work out the three-digit octal for rwxr-xr-- using read=4, write=2, execute=1 , then fill in the blank.
3️⃣ Making a Script Executable
When you create a script, it's just text — the execute bit is off, so the shell refuses to run it and you get Permission denied . The fix is one command: chmod u+x script.sh adds execute for the owner, and now ./script.sh works. This is one of the most common things you'll do on the command line, so it's worth doing by hand once.
Your turn again. Make a script runnable for its owner, then lock down an SSH key the way SSH demands. Fill in the two blanks:
4️⃣ Changing Ownership with chown
Permissions decide what each audience may do; ownership decides who the owner and group actually are. chown user file changes the owner, and chown user:group file changes both at once. Because reassigning ownership is a privileged action, it almost always needs sudo — you can't quietly hand your files to someone else, or grab theirs. Add -R to apply it through a whole directory tree.
sudo ("superuser do") runs a single command as root — the all-powerful administrator account that ignores every permission check. You need it to install software, edit system files in /etc , or run chown . Root has no safety net: permissions exist partly to protect you from your own typos, and root removes that protection entirely. The classic disaster is a stray space in sudo rm -rf / some/path , which starts deleting the entire filesystem.
Rule of thumb: if a command works without sudo , don't add it. Use the least privilege that gets the job done, and never paste a sudo command you don't understand.
Your group memberships decide which files' group permissions apply to you, so it helps to know who you are:
If you're in the sudo (or wheel ) group, you're allowed to use sudo . If a file's group is devs and you're a member of devs , the file's group permissions apply to you.
No blanks this time — just a brief and an outline to keep you on track. Write the commands yourself, run them in your terminal, and check your output against the example in the comments.
Practice quiz
In the permission string, what does the very first character indicate?
- The file type (- for file, d for directory)
- The owner
- The read permission
- The file size
Answer: The file type (- for file, d for directory). The first character is the type: - for a file, d for a directory.
The nine permission characters are split into which three groups?
- read, write, execute
- user, group, other
- owner, root, guest
- type, size, name
Answer: user, group, other. They are user (owner), group, and other - each with rwx.
In octal mode, what number does read have?
- 1
- 2
- 4
- 7
Answer: 4. read=4, write=2, execute=1.
What octal digit equals rwx (read, write, and execute)?
- 4
- 5
- 6
- 7
Answer: 7. rwx = 4+2+1 = 7.
What does chmod u+x file do?
- Adds execute permission for the owner
- Removes all permissions
- Changes the owner
- Makes it world-writable
Answer: Adds execute permission for the owner. u+x adds (+) execute (x) for the user/owner (u).
Which octal mode gives rwxr-xr-x?
- 644
- 755
- 700
- 600
Answer: 755. rwx=7, r-x=5, r-x=5, so 755.
Why does a fresh script give Permission denied when you run it?
- The file is empty
- It is owned by root
- It lacks the execute bit
- It has no shebang
Answer: It lacks the execute bit. Without the execute bit the shell refuses to run it; add it with chmod u+x.
Which command changes who owns a file?
- chmod
- ls
- sudo
- chown
Answer: chown. chown changes file ownership (usually needs sudo).
What octal mode locks a file to rw------- (owner read/write only)?
- 600
- 644
- 755
- 777
Answer: 600. rw=6 for the owner, 0 for group and other, so 600.
What does sudo let you do?
- Sort files
- Run a single command as the root administrator
- Show permissions
- Append to a file
Answer: Run a single command as the root administrator. sudo runs a command as root, the all-powerful admin account.