Managing file and Directory Permissions in Linux

Permission are crucial part of every system. After creating files and directories you need to set the proper permissions on file and directories so that they can be accessed by required users/groups. In Linux permissions are managed by chmod and chown commands.

Permission in Linux

In Linux there are Following three basic Permissions:

Read
Write
Execute

While giving permissions it can be specified using character or a number

PermissionCharacter Representation
Number Representation
Read
r6
Writew2
Executex1

Each file or directory has three different level of permissions. Users groups and others maintain their own read write and execute permission. Don’t worry if you did not understand following diagram will make everything clear.

file-permissions-Linux
file-permissions-Linux

There are total 10 Hyphens.

First Hyphen – represents that it is a file. In case of directory you will see ‘d’ there.

Next 9 hyphens  represents the permissions, 3 for user, 3 for group and 3 for others. 

Why 3 for each, because we have 3 different permissions (read,write,execute) and each hyphen represents one permission.

In the above image we have following permissions:

r w –  :  Read and write for user, no execute (x)
r – –  :   Read only to group
r – –  :   Read only to others (Other means all other users)

What is Execute Permission for a File

Execute permission enables a file to be executed as a program. Execute permission is generally given to source code / script files or binary files.

Default Permission in Linux

Every file or directory you create in Linux gets default permissions.

Default permission for a File

6 4 4 is the default file permissions it means:
Read and Write for user/owner of file.
Read only for group
Read only for others.

Default Permissions for a Directory

7 7 5 (4+2+1, 4+2+1, 4+1 ) is the default directory permission it means:
Read, Write and Execute for User/Owner of directory.
Read, Write and Execute for group.
Read and Execute for others.

What is Execute Permission for a Directory

Execute permission for directory means user can ‘cd’ into the directory or user can get inside the directory.

Managing Linux Permissions with Chmod

chmod command stands for Change Mode and its used to modify the permissions for users,groups and others. To work with chmod command you own the director/file or you need to be sudo user.
The permissions can be specified as character(r,w,x) or as numbers(6,4,1).

chmod command has following basic syntax

# chmod  grantee + permissions    file / directory  

(+ is to add permissions)
(- is to remove permission)

Grantee or the entity who is getting the permission can be specified as:
u :  for User/owner of the file
g :  for Group of the file.
o : for Others.

Set Execute Permission on a File

$ chmod   u+x   file

The user of the file will get execute permissions

set-file-execute-permissions-Linux
set-file-execute-permissions-Linux
Setting Multiple Permissions with Chmod

you can specify multiple permissions after + or – as follows.

$  chmod  u+wx  file

multiple-permissions-with-chmod
multiple-permissions-with-chmod
Set Permissions for multiple Entities with Chmod

you can also use chmod to set permissions for user,groups and others at the same time separated by comma.

It will remove the Read from other and Execute from user/owner.

$ chmod  o-r, u-x  file

multiple-entities-permissions-with-chmod
multiple-entities-permissions-with-chmod

Apart from specifying the character values (r,w,x) with chmod you can also specify the numeric values.

when you use numeric values you don’t specify the u/g/o

But for each entity (user/group/others) you need to specify only one numeric value by adding 6, 4, 1 according to your needs.

To give read,write and execute permissions to user and group you need following.

$ chmod  774  file

First 7 for User’s read,write and execute.
Second 7 for Group’s read,write and execute.
Then  4  for Others read only.

chmod-with-numeric-permissions
chmod-with-numeric-permissions

There is a special Permission known as Sticky Bit. You can read my  separate post on Sticky Bit Permissions.

Changing Ownership of File / Directories with Chown command

Chown command stands for Change Ownership. Every file and directory in Linux is owned by a User and a Group.

file-owners-linux
file-owners-linux

To change the User and Group of a file or directory you need chown command. It has following syntax.

#  chown  options   user : group    file / directory

Changing Ownership of a Directory Recursively

# chown  -R  admin1:dbadmins  db-backups

Option -R will do these ownership changes recursively changing the ownership of all files and directories inside the ‘db-backup’ directory

To change ownership of a file you can use the same command without -R option.

chown-ownership-changes-Linux
chown-ownership-changes-Linux

You can learn more about user and groups on my dedicated post on users and groups management.

I hope this has given you the understanding of file and directory permissions and also how to manage the ownership. Let me know in comments how did it help you.

Leave a Reply

Your email address will not be published. Required fields are marked *