File permissions in linux with examples LEARNOVITA

Linux File Permissions : Expert’s Top Picks | Everything You Need to Know

Last updated on 02nd Nov 2022, Artciles, Blog

About author

Prabhu (DevOps Engineer )

Prabhu is an industry expert and subject specialist who has intense knowledge in Dev, QA, Python, YAML, Ruby, shell scripts, Jenkins, Linux bash shell, PostgreSQL, and Oracle. His articles assist in sharing information and abilities in core fields and provide students with informative knowledge.

(5.0) | 19447 Ratings 2092
    • In this article you will get
    • 1.Types of permissions
    • 2.Permission groups
    • 3.Changing a file permission using chmod
    • 4.Changing ownership or group
    • 5.Advanced permissions
    • 6.Setuid/setgid special permissions
    • 7.Sticky bit special permissions
    • 8.Conclusion

Types of permissions

Each directories and file in Linux have a three basic permission types:

1.Read Permission:

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

2.Write Permission:

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

3.Execute Permissions:

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

4.Viewing the permissions:

The view permission allows user to check a directory or file in the GUI file manager or by reviewing output .

Permissions groups

Each directory and file on Linux is owned by a particular user and group and are explained separately as three user based permission groups.

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

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

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

If want to view users on the system, and can view the user.Similarly, can view a group on the system.-rw-rw-r– is a code that represents permissions given to the owner, user group, and world.Here, the ‘-’ represents selected file. For directory, it is denoted as ‘d’.

The characters are simple to remember and also understand.

  • r- Read permission
  • w- Write permission
  • x- Execute permission
  • _- No permission

The first part of a code ‘rw-’ represents the owner can read the file, write the file, but cannot execute file since the execute bit is set to ‘-’. Several Linux distributions like CentOS, Ubuntu, Fedora, etc. will add users to a group of the same group name as a username. The second part of code ‘rw-’ represents for user group and group members can read a file, write the file.The third part of code ‘r–’ represents any user and the user can only read a file.

Permission classes in Linux

Changing a file permission using chmod

With the help of change mode ‘chmod’ command, can set the permissions like read, write, and execute on a directory or file for owner, user, and the group.Here, permission number is calculated by using assigned values for r, w, and x. The basic permission number includes are three digits. Some special cases can use a four digits as permission number.

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

1.Numeric mode:

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

    NumberCharacter of SymbolPermission Type
    0 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

Command have changed a file permissions to 764. 764 represents the following:

  • The owner can be read, write, and execute.
  • The user group can also read and write.
  • Any user can only read.

2.Symbolic mode:

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

    OperatorDescription
    + Adds permission to access the directory or files
    Removes the permissions
    = Sets permission and overrides a permissions set earlier
Changing a file permission using chmod

Changing the ownership and group

  • For changing the ownership of directory or file, use a command below:
  • chown user
  • If want to change user along with the group for a directory or file, use a command below
  • chown user: group filename
  • If wish to change the group owner only, use a command below
  • chgrp group_name filename
  • Here, chgrp represents for the change group

Advanced permissions

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

  • _ – It represents the there are no special permissions.
  • d- It represents a directory.
  • l- It represents a symbolic link of directory or a file.
  • t- It represents a sticky bit permissions. It represents ‘t’ in an executable portion of all user permissions.
  • s- It indicates setuid or setgid permissions. It represents ‘s’ in read portion of owner or a group permissions.

Setuid or setgid special permissions

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

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

  • chmod g+s file1.sh

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

Sticky bit special permissions

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

Conclusion

Linux as a multi-operating system sets the permissions and ownership to ensure security for file and directories of the users. And it also allows to change and modify a permissions to a set of people as per requirements.

Are you looking training with Right Jobs?

Contact Us

Popular Courses