DEV Community

Cover image for How to Change File Permissions in Linux?
Faruk Sardar
Faruk Sardar

Posted on

How to Change File Permissions in Linux?

If you recently started using Linux or want to deploy your project to the WPS server then having a basic knowledge about Linux File Permissions is a good thing.

Sometimes, you might not want others to open or modify your files on your Linux computer or server.

Linux has a handy built-in tool for this called CHMOD, short for "change mode."

The CHMOD command lets you change who can read, write, and execute a file. You can decide if it's just the owner, a group of people, or everyone who can do these things with the file.

I have divided this post into three different sections:

  1. Explain the CHMOD codes in a simpler way
  2. How can you run this command on your terminal?
  3. Easy way to calculate CHMOD permission codes

How to read the CHMOD command?

Let's first take a look on what is the meaning of this code by breaking it into small pieces.

The chmod permission code has two different variants a numeric code or symbolic code.

Numeric Code

The numeric code consists of three digits.

Each digit represents the permissions for a specific group of users: owner, group, and others (sometimes called world).

Each digit is the sum of the permissions for that group:

  • 4 stands for read permission.
  • 2 stands for written permission.
  • 1 stands for execute permission.
  • 0 stands for no permission.

Instead of a single permission, you can provide multiple permissions by adding the permission values, here is an example:

If you want to read and write permissions, you add 4 (read) + 2 (write), which gives 6.

If you want to read, write, and execute permissions, you add 4 (read) + 2 (write) + 1 (execute), which gives 7.

This numeric code is shorter but might be harder to understand compared to the symbolic representation, where letters are used to show permissions.

Symbolic code

The length of the symbolic code is longer than the numeric code but many users find it easier to read symbolic code than the numeric code.

The symbolic representation chmod allows users to specify permissions using letters rather than numbers.

It consists of three parts:

who the permission applies to (user, group, others)

the operation to perform (add, remove, set)

and the permission itself (read, write, execute).

Here's a breakdown:

Who:

  • "u" represents the user/owner.
  • "g" represents the group.
  • "o" represents others.
  • "a" represents all (user, group, and others).

Operation:

  • "+" adds permission.
  • "-" removes a permission.
  • "=" sets the permission explicitly, overriding existing permissions.

Permission:

  • "r" stands for reading.
  • "w" stands for write.
  • "x" stands for execute.

For example:

chmod u+x file.txt: Adds execute permission for the user/owner of the file.

chmod g-w file.txt: Removes written permission for the group.

chmod o=r file.txt: Sets read-only permission for others.

chmod a+rw file.txt: Adds read and write permission for all users.

chmod ug=rwx,o=r file.txt: Sets read, write, and execute permissions for the user/owner and group, and read-only permission for others

How can you run the CHMOD command in the terminal?

To change the current permission of a file or directory (folder) first you need to check the current permission of that file or directory.

ls -l
Enter fullscreen mode Exit fullscreen mode

By running this command into your terminal you can check the current permission of that file.

In my case I have a file called "file.txt" and its current permission is owner can read and write the file, the group can only read it, and others can also only read it.

file permission before applying chmod command

I want to change it to read and write permissions for the owner, group, and others.

chmod 666 file.txt
Enter fullscreen mode Exit fullscreen mode

By running the above command I can easily do that.

file permission after applying chmod command

Now you can see the permission is successfully changed.

You can follow the same process to change the permission of a file or directory using symbolic code.

Just replace the number code with a symbol code and it will work the same way.

How to Calculate the CHMOD easily?

In many situations you might get foncused by calculating permission code, the good thing is that there are many tools available on the internet that you can use to calculate the code.

Chmodcalculator.in is the one I use personally and I will recommend it to you, using this tool is free and doesn't require creating an account.

Just visit the site toggle the permission that you want copy the generated code and use it in your terminal to change permission.

I hope I have explained the CHMOD permission in the simplest way possible.

If you still have any questions, please leave a comment, and I'll be happy to answer them.

Top comments (2)

Collapse
 
oculus42 profile image
Samuel Rouse

I like the detail and clarity in your article. Knowledge of command like utilities like this can be really helpful.

My most common use is probably chmod +x some_script.sh to make a small script executable.

Something I think is worth mentioning is the numbers in chmod are representing a binary value where each zero or one – each place – is for a specific permission.

The fields are in order from largest to smallest:

  • Bit 2 (fours): read
  • Bit 1 (twos): write
  • Bit 0 (ones): execute

When you take the individual fields and make a binary number, that gives you the decimal number we see in the command line.

4 2 1 Number
--x 0 0 1 0b001 => 1
-w- 0 1 0 0b010 => 2
-wx 0 1 1 0b011 => 3
r-- 1 0 0 0b100 => 4
r-x 1 0 1 0b101 => 5
rw- 1 1 0 0b110 => 6
rwx 1 1 1 0b111 => 7

Then, to pull a specific bit out, we can use a bit mask. These are operations we don't normally do in modern development because we have a lot of memory and storage, but used to be an important way to save space and be efficient with processor time.

const MASK_READ = 1;
const MASK_WRITE = 2;
const MASK_EXECUTE = 4;

// Bit-wise AND
const getBit = (mask, number) => (mask & number) !== 0;

const getBit(MASK_WRITE, 5); // false

// Same thing with the number written in binary
// Now we can see the zero in the "twos" place
const getBit(MASK_WRITE, 0b101); // false
Enter fullscreen mode Exit fullscreen mode
Collapse
 
0x8c profile image
tiff

Thanks for this! I've struggled to remember these, though I will say I find the numeric representation for permissions quite odd and arbitrary. Nevertheless, this is helpful for me as I dive deeper into "DevOps".

Some comments may only be visible to logged-in visitors. Sign in to view all comments.