search
HomeSystem TutorialLINUXA Beginners Guide To Understanding Linux File Permissions

Do you want to secure your Linux system? If so, you need to understand Linux file permissions. File permissions control who can access files and directories on your system. By setting the correct file permissions, you can prevent unauthorized users from accessing sensitive files or directories.

This detailed article offers a comprehensive overview of Linux file and directory permissions, how to view Linux file permissions, and how to change file permissions in Linux.

In this guide, we will explain everything you need to know about Linux file permissions. We will cover the following topics:

  • Types of Permissions in Linux
  • Linux file permissions in Binary, Octal, and String formats
  • Linux file permissions table
  • How to view file and directory permissions using ls, stat, and getfacl commands
  • What is chmod command in Linux
  • How to change Linux file permissions using chmod command
  • Frequently asked questions (FAQ) about file and directory permissions in Linux.

Table of Contents

Introduction

Linux is a multi-user system where different users and processes can access and manipulate files and directories. To maintain security and limit unauthorized access, Linux employs a comprehensive permissions system.

Each file and directory in your Linux system is assigned access rights (or permissions) for the owner of the file, the members of a group of related users, and everybody else.

Understanding file permissions is fundamental for the security of your Linux environment. They dictate who can access files and directories, and what users can do with them.

What are Linux File Permissions?

Linux file permissions are a core security feature that control who can access files and directories on a Linux system. They are represented by a set of three letters, one for each of the three user classes: owner, group, and others. Each letter represents one of three permissions: read, write, or execute.

For example, the permissions rwxr-xr-x mean that the owner has read, write, and execute permissions, the group has read and execute permissions, and others have read and execute permissions.

Types of Permissions in Linux

There are three types of permissions (or permission modes) that Linux allows for each file or directory. They are:

  • Read (r): The file can be opened, and its content viewed. For a directory, the read permission allows you to list the contents of the directory.
  • Write (w): The file can be modified; for a directory, the write permission allows you to create, delete, and rename files within the directory.
  • Execute (x): The file can be executed as a program. For a directory, the execute permission allows you to access, or traverse into, the directory, and access any of its contents.

These permissions are defined for the following types of users. These are also known as permission classes.

  • User (u): The owner of the file.
  • Group (g): Users who are members of the file's group.
  • Others (o): Users who are not the owners of the file and do not belong to the group.
  • All (a): Represents all three types of access classes.

Linux File Permissions in Binary, Octal, String Formats

In Linux, file permissions can be expressed in three different ways: Binary, Octal, or a Symbolic string representation. Here's how each works:

1. Binary Representation

Each permission is represented as a bit. Read is 4 (100 in binary), Write is 2 (010 in binary), and Execute is 1 (001 in binary). So, for example, full permissions (read, write, execute) would be 111 in binary, which stands for rwx.

2. Octal Representation

This is the most common way to represent permissions, and it's really just a compact form of binary representation.

Each digit in octal corresponds to three bits in binary, which is perfect for rwx permissions. So, for example, full permissions (read, write, execute) would be 7 in octal (since 4+2+1=7), which also stands for rwx. No permissions would be 0 (---), read-only would be 4 (r--), write-only would be 2 (-w-), execute-only would be 1 (--x), etc.

To put this in simple words, the numeric value 421 in terms of file permissions in a Linux system stands for rwx, where each character corresponds to a different type of access:

  • 4 stands for r (read).
  • 2 stands for w (write).
  • 1 stands for x (execute).

When looking at permissions in the terminal, you'll usually see three octal digits in a row, like 777, which represents the permissions for the owner, the group, and all other users respectively.

3. String (symbolic) Representation

This is the most human-readable form. Each permission is represented by a letter: r for read, w for write, x for execute. So, for example, full permissions would be rwx, read and write would be rw-, and read and execute would be r-x.

Again, when looking at permissions in the terminal, you'll usually see three sets of these permissions in a row, like rwxrwxrwx, which represents the permissions for the owner, the group, and all other users respectively.

So to summarize, if you have full permissions, you could represent it in binary as 111, in octal as 7, or as a string as rwx. Similarly, if you only had read and write permissions, you could represent it in binary as 110, in octal as 6, or as a string as rw-.

Linux File Permissions Table

You can print the following table and put it on your desk to easily recall the Linux file permissions.

A Beginners Guide To Understanding Linux File Permissions

When you run the above command in your Linux system, the output of this command would look something like this: drwxr-xr-x or -rw-r--r--. This is a sequence of ten characters:

  • The first character indicates the type of the file: a dash (-) means it's a regular file, d stands for a directory, and there are other types as well (l for link, b for block device, c for character device, s for socket, and p for named pipe).
  • The next nine characters represent the permissions for user, group, and others. Each set of three characters (r, w, x) correspond to read, write, and execute permissions. If a dash appears instead of a letter, that permission is not granted. The first set of three applies to the user, the second set applies to the group, and the third set applies to others.

In the case of dir1, it is a directory (d), and the owner (ostechnix) has read, write, and execute permissions (rwx). Both the group and others have read and execute permissions (r-x).

For file.txt, it is a file (-), and the owner (ostechnix) has read and write permissions (rw-). Both the group and others only have read permissions (r--).

2. Check File and Directory Permissions using stat Command

The stat command is used to display more detailed information about a file or directory, including the permissions in numeric (octal) form.

$ stat Documents/

Sample Output:

  File: Documents/
  Size: 4096      	Blocks: 8          IO Block: 4096   directory
Device: 10302h/66306d	Inode: 1572889     Links: 3
<strong><mark>Access: (0755/drwxr-xr-x)</mark></strong>  Uid: ( 1000/ostechnix)   Gid: ( 1000/ostechnix)
Access: 2023-05-24 13:42:26.669502054 +0530
Modify: 2023-05-24 13:42:16.849490036 +0530
Change: 2023-05-24 13:42:16.849490036 +0530
 Birth: 2022-04-02 15:20:56.520250104 +0530

A Beginners Guide To Understanding Linux File Permissions

Look for the line in the output that begins with Access: (0755/drwxr-xr-x) (the numbers and letters might be different based on the file's permissions). In the example output, the number 0755 is the permission in octal form, and drwxr-xr-x is the permission in symbolic form.

Apart from the permissions, stat command also displays other useful details. Here's what each piece of information means:

  • File: This is the name of the file or directory, in this case, Desktop/.
  • Size: This is the total size of the file or directory in bytes, in this case, 4096 bytes.
  • Blocks: This is the number of file system blocks allocated for this file or directory, in this case, 8.
  • IO Block: This is the size of every block this file or directory occupies. It is 4096 bytes, which is typical for many filesystems.
  • Device: This field represents the device number in hexadecimal form on which the file or directory resides.
  • Inode: This is the inode number, a unique identifier for every file and directory on a Unix-like operating system.
  • Links: This is the number of hard links to the file or directory. Directories will always have at least two links: one for the directory name and one for '.', the alias for the current directory.
  • Access: This line shows the permissions of the file or directory in both numeric and symbolic form, along with the user ID (Uid) and group ID (Gid) in both numeric and symbolic form. In this case, the permissions are 0755 (octal) or drwxr-xr-x (symbolic), and the user and group are both ostechnix.
  • Access, Modify, Change: These lines indicate the last time the file or directory was accessed, modified, and changed respectively. Access refers to when the file or directory was last read, Modify refers to when the file or directory's content was last modified, and Change refers to when the file or directory's metadata (such as permissions or ownership) was last changed.
  • Birth: This is the creation time of the file or directory. However, not all filesystems support the tracking of this information.

3. Check File and Directory Permissions using getfacl Command

The getfacl command is used to get the Access Control List (ACL) for a file or directory. ACL is a more flexible permission mechanism than the traditional Unix permissions system.

$ getfacl Documents/

Sample Output:

# file: Documents/
# owner: ostechnix
# group: ostechnix
user::rwx
group::r-x
other::r-x

A Beginners Guide To Understanding Linux File Permissions

The above command is used to display the ACL entries for the Documents directory. Here's a breakdown of the output:

  • # file: Documents/: This line indicates the file or directory for which the ACL entries are being displayed. In this case, it is the Documents directory.
  • # owner: ostechnix: This line displays the owner of the file or directory. Here, ostechnix is the owner.
  • # group: ostechnix: This line shows the group owner of the file or directory. The group owner is also ostechnix.
  • user::rwx: This entry shows the permissions of the owner of the file or directory. rwx means the owner (ostechnix) has read (r), write (w), and execute (x) permissions.
  • group::r-x: This entry shows the permissions of the group. r-x means the group members have read (r) and execute (x) permissions, but not write (w) permissions.
  • other::r-x: This entry shows the permissions for others (everyone else who is not the owner or a part of the group). Here, others also have read (r) and execute (x) permissions, but not write (w) permissions.

Change Linux File Permissions using chmod Command

You can change the permissions with the chmod command in Linux.

What is chmod in Linux?

The chmod (stands for "Change Mode") command is used to change the permissions of a file or directory in Linux. It uses either symbolic notation (like rwx) or octal notation (like 755) to represent permissions.

chmod Operators

To set or change permissions, we can use the plus (+) and minus (-) and equal to (=) operators in chmod command.

Here's a brief explanation of how the +, -, and = operators work in the chmod command:

  • The + operator adds permissions to a file or directory without changing the existing permissions. For example, if you want to add execute (x) permission to the user (u) on a file, you would use chmod u+x filename.
  • The - operator removes permissions from a file or directory without changing the remaining permissions. For example, if you want to remove write (w) permission from the group (g) on a file, you would use chmod g-w filename.
  • The = operator sets the permissions exactly as specified, disregarding the current permissions. For example, if you want to set the user (u) permissions to read and write (rw) only, removing execute if it's there, you would use chmod u=rw filename.

Now let us learn some examples of using the chmod command in both symbolic and octal notation.

Warning: Remember, it's important to carefully use the chmod command, as inappropriate permissions can lead to security vulnerabilities.

How to Set or Change Linux File Permissions in Symbolic Notation?

To add read, write, and execute permissions to the owner of the file named 'file.txt':

$ chmod u+rwx file.txt

To remove write permission from the group and others for 'file.txt':

$ chmod go-w file.txt

To add execute permission to the group for 'file.txt':

$ chmod g+x file.txt

To set the permissions so that the user can read/write, the group can read, and others can't access 'file.txt':

$ chmod u=rw,g=r,o= file.txt

To add read permission to all (user, group, others) for 'file.txt':

$ chmod a+r file.txt

How to Set or Change Linux File Permissions in Octal Notation?

To set read, write, and execute permissions to the owner, and read and execute permissions to the group and others for 'file.txt':

$ chmod 755 file.txt

To assign read and write permissions to the owner, and only read permissions to the group and others for 'file.txt':

$ chmod 644 file.txt

To give all permissions (read, write, execute) to the owner, and no permissions to the group and others for 'file.txt':

$ chmod 700 file.txt

To give read and execute permissions to everyone for 'file.txt':

$ chmod 555 file.txt

To give write and execute permissions to the group for 'file.txt':

$ chmod 070 file.txt

For more details, refer chmod manual page by entering the following command:

$ man chmod

Frequently Asked Questions

Here's a FAQ (Frequently Asked Questions) for chmod command.

1. What does chmod stand for in Linux?

Chmod stands for "Change Mode". It's a Linux/Unix command used to change or modify the permissions of files and directories.

2. What are the different types of permissions in Linux?

There are three types of permissions in Linux: read (r), write (w), and execute (x).

3. How do I use the chmod command to change permissions?

You can use the chmod command in two ways: using numeric (octal) representation or symbolic representation. For example, 'chmod 755 filename' or 'chmod u=rwx,g=rx,o=rx filename'.

4. What does 'chmod 777' do?

The command 'chmod 777' gives read, write, and execute permissions to the user, group, and others for a particular file or directory. This is generally not advisable for most files due to security concerns.

5. How do I remove permissions using chmod?

You can remove permissions using the '-' operator. For example, 'chmod u-w filename' removes write permission for the user.

6. What does the 'a' in chmod stand for?

The 'a' in chmod stands for 'all', i.e., all classes of users - the owner, the group, and others.

7. How do I set exact permissions using chmod?

You can set exact permissions using the '=' operator. For example, 'chmod u=rwx filename' sets the user's permissions to exactly read, write, and execute, removing all others.

8. Can I change the permissions of multiple files at once using chmod?

Yes, you can change permissions of multiple files at once by using the chmod command followed by the desired permissions and then the file names, separated by spaces. For example, 'chmod 644 file1 file2 file3'.

9. How do I view the permissions of a file?

You can view the permissions of a file using the 'ls -l' command, which will display the permissions in the leftmost column of the output.

10. I made a mistake while changing permissions with chmod. Can I undo it?

There is no direct 'undo' command for chmod. However, you can manually change the permissions back to their original state if you know what they were. It's a good practice to check permissions (using 'ls -l') before changing them.

Conclusion

Understanding and managing file and directory permissions is critical to securing your Linux environment and controlling access to your data. As a system administrator or a regular Linux user, mastering the chmod command is very important for effectively managing access to your files and directories in Linux.

We hope this article helped you understand Linux file permissions and how to use them to secure your system. If you have any questions, please let us know via the comment section below.

Related Read:

  • Restore Executable Permission To Chmod Command In Linux

The above is the detailed content of A Beginners Guide To Understanding Linux File Permissions. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does hardware compatibility differ between Linux and Windows?How does hardware compatibility differ between Linux and Windows?Apr 23, 2025 am 12:15 AM

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

What are the differences in virtualization support between Linux and Windows?What are the differences in virtualization support between Linux and Windows?Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Is it hard to learn Linux?Is it hard to learn Linux?Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

What is the salary of Linux administrator?What is the salary of Linux administrator?Apr 17, 2025 am 12:24 AM

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

What is the main purpose of Linux?What is the main purpose of Linux?Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Does the internet run on Linux?Does the internet run on Linux?Apr 14, 2025 am 12:03 AM

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

What are Linux operations?What are Linux operations?Apr 13, 2025 am 12:20 AM

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.