How to Change File Permissions in Linux?
Tags: webdev, linux, beginners, programming
Published: 2024-05-16 17:15:51

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](https://www.linux.org/) 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.

```javascript
ls -l
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5fag11vgk1n19uxzlj7.png)

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

```javascript
chmod 666 file.txt
```
By running the above command I can easily do that.

![file permission after applying chmod command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvvefghfqva6an5zevhk.png)

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](https://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.