Kahibaro
Discord Login Register

Changing permissions

Understanding `chmod`: Changing File Permissions

In this chapter you’ll learn how to change permissions, assuming you already understand what users, groups, and the r, w, x bits are.

We’ll focus on:

The `chmod` Command: Basic Usage

chmod (change mode) is used to modify the permission bits of files and directories.

Basic syntax:

chmod [OPTIONS] MODE FILE...

Symbolic Mode: Human-Readable Permission Changes

Symbolic mode lets you describe which permissions to add/remove/change using letters.

Parts of a symbolic mode

A symbolic mode looks like:

[who][operator][permissions]

You can chain multiple symbolic instructions separated by commas:

chmod u+r,g-w,o= file

Common symbolic examples

Make a script executable for its owner
chmod u+x script.sh
Allow group read access
chmod g+r report.txt
Remove write permission from others
chmod o-w notes.txt
Give everyone read permission
chmod a+r picture.jpg
# Same as:
chmod ugo+r picture.jpg
Set exact permissions for others (override)
chmod o=rx public_script.sh
Multiple changes in one command
chmod u=rw,g=r,o= file.txt

Using `X` (capital X) safely

X adds execute only to:

Example: Make a directory tree readable and searchable by others, but don’t accidentally make all files executable:

chmod -R o+rX shared_dir

Numeric (Octal) Mode: Precise Permission Settings

Numeric mode uses numbers to set all three permission groups (user, group, others) at once.

You map r, w, x to bits:

For each of user (u), group (g), others (o), you add the values:

So an octal mode is usually three digits:

$$
\text{mode} = U \, G \, O
$$

Where each of U, G, O is from 0–7.

Numeric examples

`chmod 644 file`

Permissions: rw-r--r-- – very common for text files.

chmod 644 document.txt
`chmod 600` – private file
chmod 600 secret.txt
`chmod 755` – typical program/script
chmod 755 script.sh
`chmod 700` – private directory or script
chmod 700 private_dir
chmod 700 backup.sh

Choosing Symbolic vs Numeric

Use symbolic when:

Use numeric when:

Changing Permissions Recursively

To change permissions for a directory and everything inside it, use -R:

chmod -R MODE DIRECTORY

Example: Make everything in project readable and writable by the owner only:

chmod -R 700 project

Be careful: -R can affect a lot of files. Always double-check the path and mode.

Different permissions for files and directories

Sometimes you want:

You can’t express that in one chmod call without additional tools, but you can combine find + chmod (just conceptually here):

# Directories: 755
find mydir -type d -exec chmod 755 {} +
# Files: 644
find mydir -type f -exec chmod 644 {} +

This pattern is common when fixing permissions in web projects, Git repos, etc.

Practical Permission Patterns

Here are some patterns you’ll see often:

1. A typical text file (config, document)

chmod 644 file.txt

2. Private config or secret

chmod 600 id_rsa
chmod 400 id_rsa  # read-only for owner

3. Private scripts or directories

chmod 700 backup.sh
chmod 700 ~/private

4. Shared project directory (group-based collaboration)

If multiple users in the same group work on project:

chmod 775 project

Often combined with group ownership and other settings (handled in another chapter).

Permissions on Directories vs Files

Changing permissions works the same syntactically, but the meaning of bits on directories matters:

Common directory modes:

Viewing Changes: Using `ls -l`

Always verify what you did:

ls -l file
ls -ld directory

Example:

$ ls -l script.sh
-rw-r--r-- 1 alice alice 1234 Dec  1 12:00 script.sh
$ chmod u+x script.sh
$ ls -l script.sh
-rwxr--r-- 1 alice alice 1234 Dec  1 12:00 script.sh

Safety Tips and Common Mistakes

Practice Ideas

To get comfortable with chmod, try:

  1. Create a file and:
    • Set it to 600, then to 644, then to 664 using numeric modes.
  2. Create a script file:
    • Add execute for user with symbolic mode.
  3. Make a directory tree:
    • Give it sensible permissions using -R and then adjust with X.
  4. Experiment:
    • Use ls -l before and after each chmod to see exactly what changed.

By combining symbolic and numeric modes, and by understanding the impact on files vs directories, you can precisely control who can do what with every file on your system.

Views: 25

Comments

Please login to add a comment.

Don't have an account? Register now!