fbpx chmod on Linux: Permissions 755, +x and -R Explained | Certificação Linux

Mastering chmod in Linux: Octal and Symbolic Permissions

📅 11/17/2021 ⏱ 4 min ✍️ Uira Ribeiro
Linux
Mastering chmod in Linux: Octal and Symbolic Permissions
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The chmod command is one of the most essential tools in Linux for managing file and directory permissions. With chmod, you can control who can read, write, or execute files on your system, ensuring data security and integrity. In this comprehensive guide, you will learn how to use chmod, how permissions work in Linux, and practical examples to apply in your daily work.

What is the chmod Command?

The chmod command (change mode) is used to change file and directory permissions in Linux. Permissions control what different types of users (owner, group, others) can do with a file or directory, such as read, write, or execute it. Understanding how to use chmod is essential for managing security and access on your system.

How Do Permissions Work in Linux?

In Linux, each file or directory has three types of permissions for three categories of users:

  1. User (owner): The file owner.
  2. Group (group): A group of users to which the file belongs.
  3. Others (others): All other users.

Permissions are represented by three letters:

  • r: Read permission.
  • w: Write permission.
  • x: Execute permission.

Each of these permissions can be assigned to user, group, and others.

Using the chmod Command

The chmod command can be used in two main ways: using symbolic notation or numeric notation.

1. Symbolic Notation

Symbolic notation uses letters to represent permissions:

  • u: User (owner)
  • g: Group
  • o: Others
  • a: All (user, group, others)

To add, remove, or assign permissions, use the symbols +, -, and =.

Example:

chmod u+rwx,g+rx,o+r arquivo.txt

This command grants all permissions to the user, read and execute permissions to the group, and read-only to others.

2. Numeric Notation

In numeric notation, each permission is represented by a number:

  • r = 4
  • w = 2
  • x = 1

Permissions are combined to form a 3-digit number. Each digit represents the permissions for user, group, and others, respectively.

Example:

chmod 755 script.sh

This command sets permissions so that the user has read, write, and execute (7 = 4+2+1), while the group and others have only read and execute (5 = 4+1).

Practical Examples of Using chmod

1. Making a File Executable

If you have a script that you want to make executable:

chmod +x script.sh

This command adds execute permission for all users.

2. Removing Write Permissions for Others

To ensure that only the owner can modify a file:

chmod o-w arquivo.txt

This command removes write permission for other users.

3. Setting Recursive Permissions

If you need to change the permissions of all files and directories within a folder:

chmod -R 755 /caminho/para/diretório

The -R applies permissions recursively to all files and subdirectories.

4. Restricting All Access to Others

To prevent any user other than the owner from accessing a file:

chmod 700 arquivo.conf

This command ensures that only the owner can read, write, and execute the file.

5. Default Permissions with umask

If you want new files created to have specific permissions automatically, you can configure this with umask:

umask 022

This command causes new files to be created with 755 permissions (or rw-r--r-- for files).

Understanding Special Permissions: SUID, SGID, and Sticky Bit

Beyond basic permissions, Linux also has special permissions that can be configured with chmod.

1. SUID (Set User ID)

When SUID is set, an executable file runs with the permissions of the owner instead of the user who executes it:

chmod u+s arquivo

2. SGID (Set Group ID)

When SGID is applied to a directory, files created within it inherit the directory's group:

chmod g+s diretório

3. Sticky Bit

The Sticky Bit on a directory prevents users from removing files they don't own:

chmod +t /diretório/compartilhado

Conclusion

The chmod command in Linux is a powerful tool for managing file and directory permissions. Understanding and applying the correct permissions is essential to ensure the security and functionality of your system. Whether configuring a simple script or managing a shared directory, chmod offers the flexibility needed to meet your needs.

Tip: Always review the permissions of critical files and directories on your system to avoid unauthorized access and maintain data integrity.

Explore more about changing file owners with the chown command and how to set default permissions for new files using umask in our complete guide. If you need advanced permissions, read our post about Access-Control-Lists (ACLs).

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp