▷ File Permissions in Linux with Examples in 2024 - Mindmajix (2024)

Linux is a multi-operating system that can be accessed through numerous users concurrently. So, it has to be authenticated to ensure people from accessing other confidential files. Linux can also be used in mainframes and servers without any modifications. It uses the concept of ownership and permissions to enhance the security of the directories and files.

Linux File Permissions

If you want to enrich your career and become a professional inLinux, then visitMindmajix- a globalonline trainingplatform: "Linux Online Certification" This course will help you to achieve excellence in this domain.

In this article, we are going to learn the following topics:

  1. Types of permissions

    1. Read

    2. Write

    3. Execute

  2. Permission groups

    1. Owner

    2. Group

    3. Other

  3. Changing file permission using chmod

  4. Changing ownership or group

  5. Advanced permissions

  6. Setuid/setgid special permissions

  7. Sticky bit special permissions

  8. Conclusion

[ Related Article: 10 Reasons to Learn Linux ]

Types of permissions

Every directories and file in Linux have three basic permission types. They are discussed as follows:

#1 Read Permission

The read permission enables you to open and read a file. For a directory, the read permission enables the user to list the contents of the directory.

#2 Write Permission

The write permission allows the user to modify the file and write new data to the file. For a directory, the write permission allows the user to modify the content of the directory. The user can add, remove, or rename files that belong to a particular directory.

#3 Execute Permissions

The execute permission allows the user to execute the file as a shell script or a program. For the directory, the execute permission enables the user to access the files in a directory and enter it by using the cd command but it does not allow to list the content.

#4 Viewing the permissions

The view permission allows the user to check the directory or file in the GUI file manager or by reviewing the output using the command given below.

ls -l 

Permissions groups

Every directory and file on Linux is owned by a specific user and group and are defined separately as three user based permission groups. They are as follows:

# User

A user is a person who owns the directory or file. By default, the user who creates the file or directory will be the owner.

# Group

The user group that owns the directory or file will not affect the actions of other users. All the users who belong to the group that owns the directory or file will have the same permission to access the file or directory.

# Other

The user who is not the owner of the directory or file and doesn’t belong to the same group of the directory or file. Simply, if we set the permission for the ‘other’ category, by default it will affect everyone.

If you want to view the users on the system, you can view the user using the command below:

cat /etc/passwd

Similarly, you can view the group on the system by using the command below:

cat /etc/group

-rw-rw-r-- is a code that represents the permissions given to the owner, user group, and the world.

Here, the ‘-’ represents the selected file. For the directory, it is denoted as ‘d’.

The characters are simple to remember and understand.

  • r- read permission

  • w- write permission

  • x- execute permission

  • _- no permission

The first part of the code ‘rw-’ represents the owner can read the file, write the file, but cannot execute the file since the execute bit is set to ‘-’. Several Linux distributions such as CentOS, Ubuntu, Fedora, etc. will add users to the group of the same group name as the username.

Visit here to learn Linux Training in Hyderabad

The second part of the code ‘rw-’ represents for the user group and group members can read the file, write the file.

The third part of the code ‘r--’ represents any user and the user can only read the file.

[ Related Article: Linux Tutorials-Guide ]

Changing file permission using chmod

With the help of the change mode ‘chmod’ command, we can set the permissions such as read, write, and execute on a directory or file for the owner, user, and the group.

chmod <permission-number> <file-name>

Here, the permission number is calculated by using the assigned values for r, w, and x. The basic permission number includes three digits. Some special cases can use four digits as a permission number.

Leave an Inquiry to learn Linux Training in Bangalore

There are two ways to use the commands. They are as follows:

1. Numeric mode

In a numeric mode, file permissions do not denote as characters but as a three-digit octal number. The following table provides the numbers for all permission types.

Number

Character of Symbol

Permission Type

---

No permission

1

--x

Execute

2

-w-

Write

3

-wx

Write+Execute

4

r--

Read

5

r-x

Read+Execute

6

rw-

Read+Write

7

rwx

Read+Write+Execute

For example, see the command below.

mindmajix@mindmajix:~$ chmod 764 samplemindmajix@mindmajix: ~ ls -l samplerwxrw-r-- 1 mindmajix mindmajix 20 June 20 06:00 sample

chmod 764 and checking permission. In the above command, we have changed the file permissions to 764. 764 represents the following:

  • The owner can read, write, and execute

  • The user group can read and write

  • Any user can only read

[ Related Article: Advanced Interview Questions for Experienced ]

2. Symbolic mode

In this mode, we can change permissions for all three owners. We can modify the permissions of a specific owner. With the use of mathematical symbols, we can modify the file permissions.

Operator

Description

+

Adds permission to access directory or files

-

Removes permissions

=

Sets permission and overrides the permissions set earlier

Let’s see the example:

Current file

mindmajix@mindmajix: ~ ls -l sample-rw-rw-r-- 1 mindmajix mindmajix 22 2020-06-29 13:45 sample?

Setting permission to other users

mindmajix@mindmajix:~$ chmod 0=rwx samplemindmajix@mindmajix:~$ ls -l sample-rw-rw-rwx 1 mindmajix mindmajix 22 2020-06-29 13:45 sample

Adding execute permission to the user group

mindmajix@mindmajix:~$ chmod g+x samplemindmajix@mindmajix:~$ ls -l sample-rw-rwxrwx 1 mindmajix mindmajix 22 2020-06-29 13:45 sample?

Removing read permission for the user

mindmajix@mindmajix:~$ chmod u-r samplemindmajix@mindmajix:~$ ls -l sample--w-rwxrwx 1 mindmajix mindmajix 22 2020-06-29 13:45 sample

Changing ownership and group

[ Related Article: What is Linux OS 2021 ]

For changing the ownership of a directory or file, use the command below:

chown user

If you want to change the user along with the group for a directory or file, use the command below

chown user: group filename

If you wish to change group owner only, use the command below

chgrp group_name filename

Here, chgrp represents for change group

Advanced permissions

The special permissions that are used to access directories or files are as following:

  • _ - It represents there are no special permissions.

  • d- It represents the directory.

  • l- It represents the symbolic link of a directory or a file.

  • t- It represents the sticky bit permissions. It represents ‘t’ in the executable portion of all user permissions.

  • s- It indicates the setuid or setgid permissions. It represents ‘s’ in the read portion of the owner or group permissions.

Setuid or setgid special permissions

The setuid or setgid permissions are used to assign the system to run an executable as the owner with the owner’s permissions. We can assign this permission by explicit defining permissions.

The character that represents the setuid or setgid is ‘s’. To set the setuid or setgid bit on file1.sh, use the command below:

chmod g+s file1.sh

Be careful while using setuid or setgid permissions. If you assign the permissions incorrectly, then your system goes to intrusion.

Sticky bit special permissions

The sticky bit can be useful in a shared environment because when it is assigned to the permissions on a directory it sets permissions for the file owner only to rename or delete the file.

The character for the sticky bits is ‘t’. To set the sticky bits on a directory, use the command below:

chmod+t dir1

Conclusion

Linux as a multi-operating system sets permissions and ownership to ensure security for a file and directories of the users. And it also allows to change and modify the permissions to a set of people as per the requirements. In this article, we have learned the Linux file permissions, Linux commands, and some examples in brief.

▷ File Permissions in Linux with Examples in 2024 - Mindmajix (2024)

FAQs

▷ File Permissions in Linux with Examples in 2024 - Mindmajix? ›

The command chmod -R 777 / makes every single file on the system under / (root) have rwxrwxrwx permissions. This is equivalent to allowing ALL users read/write/execute permissions. If other directories such as home, media, etc are under root then those will be affected as well.

What is file permissions in Linux with examples? ›

Examples of different types of permissions on files and directories:
  • -rwx------ This file is read/write/execute for the owner only.
  • dr-xr-x--- This directory is read/execute for the owner and the group.
  • -rwxr-xr-x This file is read/write/execute for the owner, and read/execute.

What is 777 permission in Linux to a file? ›

The command chmod -R 777 / makes every single file on the system under / (root) have rwxrwxrwx permissions. This is equivalent to allowing ALL users read/write/execute permissions. If other directories such as home, media, etc are under root then those will be affected as well.

What are four basic permissions? ›

There are four categories (system, owner, group, and world) and four types of access permissions (Read, Write, Execute and Delete).

What is the T permission in Linux? ›

Sticky Bit Special Permissions

You can only assign the sticky bit by explicitly defining permissions. The character for the sticky bit is t. To set the sticky bit on a directory named dir1 you would issue the command chmod +t dir1.

What is chmod 777 command? ›

The `chmod 777` command is used in Unix-based systems (such as Linux or macOS) to change the permissions of a file or directory. The `chmod` command stands for “change mode” and the `777` argument specifies the permissions to be set.

What are file permissions explain with examples? ›

For files, the permissions have their common English meanings. If a user has read permission, he or she can read but not modify the file. A user needs write permissions to modify the file. To run the file as a compiled program, the user needs execute permissions.

What is chmod 777 and 644? ›

644 - owner can read/write, group/others can read only. Some directory permission examples: 777 - all can read/write/search.

What is chmod 777 and chmod 775 and chmod 755? ›

Change permission on all the files in a directory recursively. chmod 777: Everything for everyone. chmod +x or chmod a+x: Execution for everyone. chmod 755: Only owner can write, read and execute for everyone. chmod 700: Everything for owner only.

What is 666 permission in Linux? ›

A text file has 666 permissions, which grants read and write permission to everyone. A directory and an executable file have 777 permissions, which grants read, write, and execute permission to everyone.

What is 750 permission in Linux? ›

chmod 750 my_file1. txt : set the user has read, write, and executable permissions; group has read/executable permission; others no permission.

What is 700 permission in Linux? ›

700 means you can do anything with the file or directory and other users have no access to it at all. Suitable for private directories and programs. 600 means you can read and write the file or directory and other users have no access to it.

What is 644 permission code? ›

The standard permissions for a regular file are -rw-r--r--, or, numerically, 644, which gives the file owner permission to read and write, and the group and world permission to read only.

How to read file in Linux? ›

There are some commands such as cat, ls, that are used to read files from the terminal. In Linux, we can display various file formats such as text file, audio files, video, image, doc, pdf, or any other file contents. Open the file using cat command. Open the file using less command.

How do I check file permissions in Linux? ›

Listing File Permissions

Type the command ls -l to list the files and directories with file permissions for your current location. The first character denotes whether an item is a file or a directory. If 'd' is shown, it's a directory, and if '-' is shown, it's a file.

How do I give permission to Linux? ›

To change directory permissions in Linux, use the following:
  1. chmod +rwx filename to add permissions.
  2. chmod -rwx directoryname to remove permissions.
  3. chmod +x filename to allow executable permissions.
  4. chmod -wx filename to take out write and executable permissions.
Jan 9, 2023

What are the 3 Linux file permissions? ›

There are three kinds of file permissions in Linux:
  • Read (r): Allows a user or group to view a file.
  • Write (w): Permits the user to write or modify a file or directory.
  • Execute (x): A user or grup with execute permissions can execute a file or view a directory.
Jan 9, 2023

What are 777 permissions? ›

Understanding File Permission 777

Access mode 777 makes a file fully readable, writable, and executable for everyone. To put it another way, this permission gives any user full access to a file on a system. Accordingly, we should use this permission only when we trust all our users with a given set of data.

What is an example of file permissions in Unix? ›

Permission examples:
  • chmod a+r files are readable by all.
  • chmod a-r files cancels the ability for all to read the file.
  • chmod a-rwx cancels all access for all.
  • chmod g+rw files give the group read and write permission.
  • chmod u+rwx files give the owner all permissions.

What is chmod 755? ›

755 means read and execute access for everyone and also write access for the owner of the file. When you perform chmod 755 filename command you allow everyone to read and execute the file, the owner is allowed to write to the file as well.

Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 5799

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.