Saturday, September 24, 2022

File permissions in Linux with examples

In this article, we want to teach you about file permissions in Linux with examples.

For effective security, Linux divides authorization into 2 levels:

  • ownership
  • permissions

Let’s start with the ownership.

File permissions in Linux with examples

For effective security, Linux divides authorization into 2 levels: first ownership and second permission.
Let’s start with the ownership.

Ownership of Linux files

Every file and directory in our Linux system has 3 types of owner: user, group, and other.

User
A user is the owner of the file. By default, the person who created the file becomes its owner.

Group
A user group can contain multiple users. All users in a group have the same permission access to the file. Think you have a project where a number of users ask to access a file. In this situation instead of manually setting permissions for each user, you can create a group and set permissions for that group and add your members to it and only group members can read or modify the files.

Other
This person hasn’t created a file or doesn’t belong to a group. It means everybody else. When we set the permissions for others, it means that we set permissions for the world.

Types of permission in Linux

Every file and directory in our Linux system has three permissions for all three owners said above. Read, write, execute.

Read: by this permission you can open a file and read it. on a directory it will give you the ability to list its content.

Write: by this permission you can modify the contents of a file. On a directory, by this permission, you can add, remove, and rename files stored in a directory.

Execute: in Linux, you can’t run a program unless the execute permission is set.
Let’s see file permissions in Linux.
By using ls -l command you can see file type and access permissions.
For example, we have this ‘-rw-rw-r– ‘the first ‘-‘shows that we have selected a file.
If it were a directory instead of ‘-‘it would have been shown by ‘d’.

The characters are easy to remember:

r=read permission
w=write permission
x=execute permission
=no permission
The first part of the code ‘rw-‘means that the owner can:
Read the file.
Write or edit the file.
But he can’t execute the file.
The second part of the code is ‘rw-‘. It is for the user group. Group members can:
Read the file.
Write or edit the file.
The third part is for the world which means any user. It says ‘r–‘. This means the user can only:
Read the file.

Changing file/directory permissions

Ok! now We know about types of File permissions in Linux and ownership, now, We can use ‘chmod’ command (changing mode) for setting the permissions (read, write, execute) on a file or directory for the owner, group, and the world.

chmod permissions filename

There are two ways of using this command: numeric and symbolic mode.

Numeric mode:

Number Permission Type Symbol
0 No permission
1 Execute –x
2 Write -w-
3 Write and Execute -wx
4 Read r–
5 Read and Execute r-x
6 Read and Write rw-
7 Read, Write and Execute rwx

For example:

 chmod 764 filename

This code means that the file owner can read, write and execute. The user group can read and write. The world can only read.

Symbolic mode

In the symbolic mode, you can modify the permissions of a specific owner.

+ : (adds a permission to a file or directory)
  : (removes the permissions)
= : (Sets the permission and overrides the permissions set earlier)

Owners are shown as:

u : (users)
g : (groups)
o : (other)
a : (all)

For example:

 chmod u-x filename

It means that the user cannot execute the file.

Changing ownership and groups

For changing the ownership of a file/directory, you can use the following command:

chown user

If you want to change the user as well as a group for a file or directory use the command:

chown user:group filename

For changing group-owner only, you can use the following command:

chgrp group-name filename

Hope you enjoy file permissions in Linux with examples article.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular