How to Change a Read-Only File in Linux?

In Linux operating systems, files and folders have three main permissions, which are read, write, and execute, which determine what actions can be taken on them. If you cannot make changes to a file or execute it, it may be because you do not have the necessary permissions. This is often done for security, as it prevents unauthorized access or modifications to sensitive files or folders.

In this article, we will discuss how to give write and execute permissions to a file or folder that is currently only readable. The content of this article is described below:

Before coming to the main topic, it is important to get a brief knowledge on different permission, which are explained below:

Type of Permissions in Linux OS

There are generally three types of permission which you can allow to any user or remove from them that are as follow

Read Permission

The read permission allows you to give or remove the reading rights of any file directory to the user and in the terminal, it stands for ‘r’

Write Permission

The write permission allows you to give or remove the rights to write anything on the file and make changes and in the terminal, it stands for ‘w’

Execute Permission

The execute permission allows you to give or remove the rights to execute any file, such as app image, and in the terminal, it stands for ‘x’.

When you see the permission of any file or directory, it will show you in the order as mentioned below:

This means that when you check the permission of any file, it will show you that using 10 dash (-) lines. The first one shows you if it’s a directory or not, so if it’s not a directory, that line will remain empty.  Now the next 9 lines are for users, groups, and others, and each can get access to any of these explained permissions.

Method 1: Changing Read-Only File (Alphabetic Representation)

If you want to see the permission of any specific file, then you can do that by typing

By default, the permission of a file can be seen as all users have the same permission to read a file only, so you will have something like that as below:

Now there are two different methods that you can apply to change the read-only file permission which is explained below:

$ chmod a+w testfile.txt     # for giving write rights to all users
$ chmod a+x testfile.txt     # for giving execute rights to all user
$ chmod a+wx testfile.txt     # for giving both write and execute rights to all users 

For example, if you want to give the write and execute rights both to all the users then you need to run the third command which is

$ chmod a+wx testfile.txt

If you execute the first code, then you are giving rights to write anything on a file and modify it. If you execute the second code, you are given the right to execute a file, and then in the third code, you can do both, which are written and executed.

The main drawback is that it gives the same rights to all levels of users so if you want to give any right to a certain user, then you can do that by typing the below commands:

For User Rights:

To give any rights to the main user you need to type the following command mentioned below:

$ chmod u+w testfile.txt     # for giving write rights to a user
$ chmod u+x testfile.txt     # for giving execute rights to a user
$ chmod u+wx testfile.txt    # for giving both write and execute rights to a user

Suppose you want to give the write and execute right both to the main user then you can do that by typing the third command from above and the result is mentioned below:

For Group Rights:

To give any rights to the group users you need to type the following command mentioned below:

$ chmod g+w testfile.txt   # for giving write rights to group users
$ chmod g+x testfile.txt   # for giving execute rights to group users
$ chmod g+wx testfile.txt  # for giving both write and execute rights to groups users

Suppose you want to give write and execute right both to the group user then you can do that by typing the third command from above and the result is mentioned below:

For Other Rights:

To give any rights to the other users you need to type the following command mentioned below:

$ chmod o+w testfile.txt     # for giving write rights to other users
$ chmod o+x testfile.txt     # for giving execute rights to other users
$ chmod o+wx testfile.txt  # for giving both write and execute rights to other users

Suppose you want to give write and execute right both to the group user then you can do that by typing the third command from above, and the result is mentioned below:

Method 2: Changing a Read-Only File (Octal Values)

You can use the numeric values (also known as octal values in permissions) as well assigned to the read write and execute to change a read-only file, and their values are mentioned below.

Read 🡪4

Write 🡪2

Execute 🡪 1

For example, if you want to give read, write, and execute permission to all users; you need to add the corresponding numbers, such as (4 + 2 + 1) = 7. So, if you want to permit every group user, then you need to type the following command:

$ chmod 777 testfile.txt

For User Rights:

To give any rights to the main user, you need to type the following command mentioned below:

$ chmod 644 testfile.txt     # for giving read and write rights to a user
$ chmod 544 testfile.txt     # for giving read and execute rights to a user
$ chmod 744 testfile.txt    # for giving read, write and execute rights to a user

For Group Rights:

To give any rights to the group users you need to type the following command mentioned below:

$ chmod 464 testfile.txt   # for giving read and write rights to group users
$ chmod 454 testfile.txt   # for giving read and execute rights to group users
$ chmod 474 testfile.txt  # for giving read, write and execute rights to groups users

For Other Rights:

To give any rights to the other users you need to type the following command mentioned below:

$ chmod 446 testfile.txt     # for giving read and write rights to other users
$ chmod 445 testfile.txt     # for giving read and execute rights to other users
$ chmod 447 testfile.txt  # for giving read, write and execute rights to other users

That’s how you can change the read-only file’s permission.

Conclusion

In a Linux operating system, every file and folder has a set of permissions that dictate what actions can be taken on them. These permissions include reading, writing, and executing the file or folder. If you cannot make changes to a file or execute it, it may be because you do not have the appropriate permissions. So if you want to change the permissions, we have discussed two different methods for your better understanding.