How to Change File Permissions on Linux

How to Change File Permissions on Linux Using the chmod Perintah Command

As is well known, Linux uses multi-user concept, which means one computer can be used by more than one user to do different jobs.

This means that files owned by multiple users are on the same device, which can be a problem if it turns out that files belonging to one user are deleted or changed by another user when the real owner doesn’t want that to happen.

For this reason, the concept of file access rights was created to handle the multi-user concept that exists on a Linux. In this tutorial set Learning Terminal, we will learn how to set permissions for a file in Linux.

We will not discuss the concept in depth. But we will discuss the technical steps of how to set permissions for a file in Linux in detail but simple so that it is easy to understand.

How to View Access Rights of a File Using Terminal

Before going into the section on how to set file permissions in Linux, we will first see how to view the permissions of a file. To see the permissions of a file, you can use the command ls -l followed by the name of the file for which you want to view the permissions.

For example, if you want to see the permissions for the “report.pdf” file in the “Documents” folder in your home directory, you can do this by using a command like the following:

ls -l Documents/laporan.pdf

Then the access rights of the file will appear as shown in the following image:

Viewing File Permissions on Linux
Viewing File Permissions on Linux

The first letter “rwx” after the “-” sign is a sign of the access rights of the user who owns the file, then “rwx” then indicates the access rights of the user. groupand the last “rwx” indicates other permissions (ie a user who is not the owner of the file and is not in the file group).

The letters “r”, “w”, and “x” are representations of the access rights to the file where the letter “r” means access rights read, the letter “w” is the access right write and “x” represents access rights execute.

Access rights read means that the user can read and open the file, while the access rights write means that the user can write and modify the file, while the access rights execute means the user is given permission to run the file, usually a program file.

On Linux, you can change the permissions of a file by using the command chmod through command line interfaces.

How to Change the Access Rights of a File on Linux

In order to make changes to a file, make sure you are the user who owns the file or the “root” user (or a user with the ability to run sudo commands).

As mentioned earlier, to set permissions for a file on Linux you can use the command chmod with a writing format as follows:

chmod hak_akses nama_file

Access rights granted by using the command chmod are generally represented by using a decimal number that represents the permissions granted, as follows:

File Permissions on Linux
File Permissions on Linux

As already mentioned at the beginning of the article, there are three objects that can be set access rights to a file in the command chmodi.e. the user who owns the file, group who owns the file rights and other users (other).

So for example you want to grant access rights to a file called “report.pdf” which is in the Documents folder as before, assuming you will grant access rights:

  1. “read, write and execute” against the user,
  2. “read and write” to group
  3. only “read” access rights to other,

You can do this by using a command like the following:

chmod 764 Documents/laporan.pdf

The first number, “7” is used to set access rights to which user will be given access rights read, write and execute. The second number, “6” is used to set the access rights to the group which one will be granted access rights read and write. While the last digit, “4” is used to grant access rights to other or to all users, which will only be granted read.

To test whether the access rights to the file have been successfully changed, you can do this by using the command ls -l as in the following image:

Access Rights After Changes Are Made
Access Rights After Changes Are Made

The picture above shows that you have succeeded in changing the file access rights as previously desired, namely “(r)ead, (w)rite and e(x)ecute” access rights for users, “(r)ead and (w)rite” access rights ” for group and only “(r)read” access rights to other.

How to Change the Access Rights of All Files in a Folder

In addition to giving the right access right to one file directly, by using the command chmod You can also grant access rights to all files in a folder directly by using parameters -R.

For example, you want to change the permissions of all files in the “vue” sub-folder in the “project” folder in the home directory, provided that:

  1. the user has full access rights i.e read, write and execute,
  2. group only have access rights read and write,
  3. whereas other do not have any access rights,

You can do this with a command like the following:

chmod -R 760 project/vue

Then the files in the folder will have the desired access rights, to make sure you can use the command ls -l as done before as in the image below:

Viewing the Access Rights of All Files in a Folder
Viewing the Access Rights of All Files in a Folder

Then you will see the file permissions as you set earlier.

Enough of the Learn Terminal tutorial this time on how to set file permissions on Linux. In the next section we will learn how to add or remove a file permissions in Linux using the font format, you can try this method if you have trouble using the format numeric as above.

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *