How to Change File Permissions in Linux

Change File Permissions Linux

Linux, like other operating systems, organizes itself using directories and files that can potentially be accessed, altered, or executed. To prevent internal anarchy, Linux gives different levels of permission for interacting with those files and directories. If you want to modify those permissions, the chmod (change mode) command is what you need.

Introducing the chmod Command

What can chmod allow you to change? Permission can be granted or refused to different users for reading(r) a file, writing(w) to the file, and executing(x) the file, in the case of a program. The permission system for directories is very similar: “r” for reading the directory contents, “w” for writing into the directory by creating files or subdirectories, and “x” for entering into that directory.

These permission levels can be defined for the owner(u) of the file or directory, the group(g) to which the owner belongs, the other(o) users, or simply all(a) users all at once.

Using the ls Command for a Long Format Listing

If you use the ls command with the -l option (for a long format listing) on a given file like my_app, for example, you’ll see output similar to this:

$ ls -l my_app

OR

-rwxr-x--x 1 hadley tech 729 Mar 15 08:01 my_app

The very first character (“” in this case) shows that the entity “my_app” is indeed a file. If it had been a directory, we would have seen d as the very first character. Next, we see three lots of three characters (rwxr-x–x). The first three (rwx) mean the owner of the file (named as “Hadley” a little further along) has the permission to read, write, and execute my_app. The next three (r-x) indicate that members of the same group as Hadley (the “tech” group) may read and execute my_app, but cannot write or modify it. The final three characters (–x). mean that other users may simply execute my_app, but neither read nor write to it.

The chmod Command in Action

So how do we use chmod to modify these permissions? We’ll use a command like:

$ chmod 771 my_app

OR

$ chmod g+w my_app

In the context above, these two commands in fact mean the same thing. To see why, we have to understand that chmod has a peculiarity. It accepts instructions to change file permissions for different users either by using combinations of these letters (text or symbolic format), or by using a system of numbers (numeric or octal format). Here’s how the two systems correspond:

  • Permission to read, r, is given the numeric value of 4
  • Permission to write, w, is given the value 2
  • Permission to execute, x, is given the value 1

So in the original situation in our example above, Hadley, who is the owner of the file, has permission to do everything (rwx), or in numerical terms, has permission 4 + 2 + 1 = 7. Similarly, other members of Hadley’s group, “tech,” have permission to read and execute the file, but not write to it. So they have r-x permission, or numerically they have 4 + 1 which makes 5. Everybody else just has –x or 1. Our string of characters -rwxr-x–x then becomes 751 in digits.

So our command chmod 771 my_app above is simply changing the 751 level of permission to 771. In other words, we’re just adding the write permission (w or 2) to members of Hadley’s group, “tech.” Now the meaning of the other version of this same command becomes clear as well: g+w simply means “add write permission to members of Hadley’s group,” just as before.

If full permissions to read, write, and execute were to be given to everybody, then the two forms of the chmod command would look like:

$ chmod 777 my_app

OR

$ chmod ugo+rwx my_app

And they would both have the same effect.

Additional Options of the chmod Command

The chmod command also has options that may be useful to know about. They include:

  • -R for recursively applying the same permissions to a directory, and all the subdirectories and files in the directory and its different subdirectories
  • -c to report when a change is made, -f to suppress error messages, and –v (verbose) to output a diagnostics for every file or directory processed

For example,

$ chmod -R a+rwx /mydir

This would ensure everybody (a) had full read, write, and execute permissions for mydir and all the subdirectories and files in it.

Photo Sources: newsyaps.com

Advertiser Disclosure

HostingAdvice.com is a free online resource that offers valuable content and comparison services to users. To keep this resource 100% free, we receive compensation from many of the offers listed on the site. Along with key review factors, this compensation may impact how and where products appear across the site (including, for example, the order in which they appear). HostingAdvice.com does not include the entire universe of available offers. Editorial opinions expressed on the site are strictly our own and are not provided, endorsed, or approved by advertisers.

Our Editorial Review Policy

Our site is committed to publishing independent, accurate content guided by strict editorial guidelines. Before articles and reviews are published on our site, they undergo a thorough review process performed by a team of independent editors and subject-matter experts to ensure the content’s accuracy, timeliness, and impartiality. Our editorial team is separate and independent of our site’s advertisers, and the opinions they express on our site are their own. To read more about our team members and their editorial backgrounds, please visit our site’s About page.