Linux: How to Change File Permission in Linux

Home /

Table of Contents

File Permissions

Linux allows several users to access the system. As a result, it must protect the safety of all users. It protects users from making unauthorized changes to files. These permissions were separated into two categories by Linux: ownership and permission. Understanding these concepts is important to operate any Linux distribution. In this section, we will discuss this in detail. 

  • Ownership: Every file and directory in Linux is owned by a user, group, or other entity. The individual who generated the file is referred to as the user. A user group is a collection of users who all have the same permissions. Instead of providing access to each user individually, Linux allows you to group all users together. Other users who did not create the file or who do not belong to a user group might possess the file. So it applies to everyone else in one sense.
  • Permissions: Linux offers three kinds of permissions to every directory and file. These are read, written, and executed. With read permissions, you can open the file and read it but can not make a change. To modify the file you must need to write permission. Execute permissions are needed to run a program that is a similar type to the ‘.exe’ extensions in Windows. Without executing permissions, you may read and write the file but can not run it. 

- Linux: How to Change File Permission in Linux

To see the permissions of a file (sample.txt), you need to run the following command.

$ ls -l sample.txt

jYRU7UIu1YfZlCJ5Cv4QNcgIUMdhI27hBvojbUV9o o mn2AT4ANV9UcCVDS3CFeyGC1SObtmvfYJW7CvmZIZjU4FZIS3o26HrUwDKLEuqeHAJSKSq2p7EPr - Linux: How to Change File Permission in Linux

Different sorts of permissions are displayed in the result. The initial ‘-‘ denotes the presence of a file, sample.txt. It displays ‘d’ for a directory. There are three permit groups after that. The first group is for users, the second is for groups, and the third section displays permissions for additional groups

We can see different letters for different permissions.

 MQWv 5 WPuCEyGUjxjWi9mBsBexVzBI2sXPHZTpSdZNrmjeKRgG7e3fSgg0VIN4ohwzJuBYV VuVCZOmWYugyx1gEqfsaaZRm5zjlBXSkzd1uAopDRh5Be6vVD8dO7Bp9pgoBpejwGWd2G0NGO8UlCkwjcfRCECC0H4U12p qwc K49 Xer aEpy - Linux: How to Change File Permission in Linux

  • R: read
  • W: write
  • X: execute
  • -: no permission

Next to the permissions, the terminal shows the file owner and the date when the file was created.

Modifying Permissions

Permissions of any files or directory can be changed using the ‘chmod’ command. To change the permission first you need to choose the ownership for which you want to change the permissions. 

Permissions added to a file

Let’s say you want to add execute permission for a user of the sample.txt file. For this run the following command.

$ chmod u+x sample.txt

Now, you can see sample.txt has to execute permission for the user. You may also change this for the user group by running the “ug+x” command.

JlE5sn z2Y6J3nMQ4peICN4n95YT 5DQCXpn9UmVHDb 4Btt2WsHL9GI1bQ0cSneb W6T3sdWUaM3wje2yFtjeWTybui0 4XI eSqFyyzVG7E QnH0v72lc32Qv4noPZHzHyVeJ - Linux: How to Change File Permission in Linux

Removing the permissions

For removing permissions, you need to use the ‘-’ operation. Try the command line below.

$ chmod u-x sample.txt


HPRxQOOJg5bFldwzBdFdFcjVdFBFQFwjt1LeQyq8e1c8ml0 - Linux: How to Change File Permission in Linux

Another option is to alter the authorization in numerical format. The following are the numerical representations:

  • 4: read
  • 2: write
  • 1: execute

Now, run the command below.

$ chmod 765 sample.txt

gCoyVAwbCdjmAduJab7WDmI5gcxSvtLkzWYnQjIOS2K93 sqY0771STwh39WxHwRq wZ5o1aFpAcJVZu9xEohGOtoU5V5yvUCj6Qxn2KegzMmyjq5Z BPOadc0IPT49SAeOWLp7TlysQhx9691qN7BYb2vukZHblFC ewWaHjU89GMTFYT WnH4I - Linux: How to Change File Permission in Linux

The first digit represents the user, the second digit represents the user group, and the last digit represents the other group. So, in this situation, the user has read, write, and execute rights because of the 7(4+3+1) permissions number, the user-group has read and write permissions because of the 6(4+2) permissions number, and the other group has read and execute permissions because of the 5(4+1) permissions number. 

Setuid

Users can run a file or program with the rights of the user (setuid) or group (setgid) that controls the file using setuid and setgid. If you want a user to be able to do a certain operation that requires root/superuser rights but doesn’t want to grant them sudo or root access, this is a good option.

To explore this in detail, let’s run the following command.

$ passwd

By running the command, you can change your user password in the system. This password is saved in the ‘usr/bin/passwd’ directory. Now, let’s see the permissions of the directory. 

$ ls -l usr/bin/passwd


FtipjW3Bm9iR7nBu8iJFMPu fGW2HFJNxioEZEoRX46UA18s6PnkKrYupJpnKLQ7rbEBhKXuNUtr5tyd G3itxKWh6T0qYba3o KOA42JRVPPHkNgJTF3OcUf68GKl0TbOFvc8grCnfcC4vX TCUx45Wvsvt9wMVhY 9s1ex3pNSjsQ wK6J8v4 - Linux: How to Change File Permission in Linux

You can see that root is the owner of the directory. So, how come you can change it? The answer lies in the ‘s’ in the user permissions group. This ‘s’ denotes the user id (UID) which allows the user to change its password. That means ‘s’ provides a certain kind of superuser power to the user. By user id, one user can only modify its own password, not the others. 

Like traditional ways to change a file’s permissions, there are two ways to modify SUID permissions. 

Symbolic way:

$ sudo chmod u+s sample.txt

R clBk2j2LDCg8dGjXvASJSw1YFMna6yixig grW9w9QZ9tYJJnyS7vCezpJXxXJXTn9MCekWurq3d33kYVsWxWVmxiqWZliM3R2loOO4jRF YSVhFIMBb03KDcy5ue81qq7mt - Linux: How to Change File Permission in Linux

You can notice that there are ‘s’ in the user group.

Numerical way:

$ chmod 4755 sample.txt

SUID permission number(4) is placed before the other three permissions. 

LmFypY3viQeBCKVfEhhbUmKK6wVi4hd amKeJNpfYZ3xkLeMFJFjQNsea77geZISe6l7Cf38yPjsvf11Lylgmaxjqben91SipV7 EsQzJEI6lJOkZWO - Linux: How to Change File Permission in Linux

In this same way, you can also change the group id (SGID) of a file or directory. To try this, first, make a folder named ‘group’ and check its default permissions.

$ mkdir group
$ ls -ld group/


z2QPRjv42DHyQEBLDokz4wPmBgB8zbbwcGPn bEO0NwjURB7aaIR6gp63uGJzyO10jyviW8r3IgZEslyy0y1Ulh6cJIQ9eOXrIpPatIzyZmBTemsgK6dHuIdLNoYC67tmwF7MpJ8w0WhY7JX0ePXADgDV4t9L5Fm0JLe96AJRqbuqQEf6UejT21d - Linux: How to Change File Permission in Linux

So, it does not have any SGUID. To set SGUID, run the following command.

$ chmod g+s group
$ ls -ld group


- Linux: How to Change File Permission in Linux

Now, the folder has ‘s’ in the group-user permission group. You can also do this in a numerical way.

$ chmod 2555 folder
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article