How to Overwrite a Read-only File on Linux

How to Overwrite a Read-only File on Linux

Are you trying to figure out how to overwrite a read-only file on Linux? There is more than one way to do it, and we’ve detailed them below. Linux is one of the most widely used operating systems, with various editors such as vim, nano, and gedit

Read-only Files: Basics

Often, you open files in your favorite editor in Linux to edit them, but you don’t have the desired permissions. Hence, the file is opened as read-only. In read-only mode, you can make the changes in the file, but you won’t be able to save it. 

The problem is that you have not opened the file via the sudo command. So, you close the editor and open the file again as the root user. 

How to Overwrite a Read-only File on Linux

There are several workarounds to overwrite a read-only file in Linux. We’ve boiled things down into multiple solutions below for you to try out:

Open the editor as super user

The first approach is to open the editor as a super user. Being a root user, you can modify the file as often as possible. Open the terminal and open your favorite editor by using the following command:

sudo <editor><filename>

Here, the editor could be the executable name of the editor, such as nano, gedit, vim, etc., and the filename is the name of the file you want to edit. Alternatively, you can also write:

gksudo gedit <filename>

Note: You will be asked for the super user password. Its password is created by the user the very first time they install Linux.

Root password for Ubuntu

The reader must be thinking about the root password for Linux. By default, there is no root password for Ubuntu, neither are you required to provide one. Hence, you can enter an empty password and click enter while prompted for the password.

Modifying .bashrc file

.bashrc is a file automatically executed while a user logs in to Linux. The file usually contains configuration information and settings such as coloring, completion, shell history, and command alias. Modify the .bashrc file by typing the following.

Input:

alias svim='sudo vim'

Now, whenever you want to edit a file, open it with svim.

Writing to a read-only file

If the file is owned by you but is read-only, you can use the following command to save the content:

:w!

Alternatively, you can use: 

:set noreadonly

And the following:

:w!

Editing in vim without sudo

In case you are editing the file in vim, and you have forgotten or don’t want to run the editor as a super user, you can use the following vim command to save your changes:

:w !sudo tee%

Where, ‘%’ represents the name of the current file. The vim editor, in return, will notice the changes and ask if you want to load the changes—press “L” in this case. This is the most common approach based on opening a pipe to the current file as the super user using an implementation of sudo tee. The tee command is run as a super user. 

The tee command directs output to provided files and the standard output stream. If you supply another piped command, it can capture the output. Here, we are using tee as a workaround and ignoring standard output.

Create a shortcut

You can also create a shortcut file and define your own command. Write the following command in the .vimrc file:

command W w!sudo tee% >/dev/null

Here, /dev/null explicitly throws away the standard output.

With the above shortcut created, press “:W” and “Enter” to save the file as root. Another way of doing the same is by typing in the following command:

cmap w!! w !sudo tee >/dev/null %

Now type “w!!” to save the file.

The following command also works:

:w !sudo sh -c "cat>%"

Change the permissions of the file

One of the approaches could be to modify the file’s permission, then save your file in vim, and change it back. You can also note down the permission of the file via using the following command:

ls -1file

Now you will modify the permissions of the file as follows:

chmod 777 file

Save the file in vim, then change the permission back with the following command:

chmod xxx file

Where ‘xxx’ are the file’s permissions you note down in the first step.

This article discusses a workaround for a situation in which a user opens a file in vim for writing and realizes later that root permission is required to save the changes. We also discussed a few common approaches or hacks for this problem. 

The best way is to always open the file in super user mode. You can write the SU command in .bashrc as an alias and then always use this alias for opening files. 

Alternatively, you can use the tee hack as a workaround. The .vimrc can be modified to provide shortcuts to achieve the desired result. Lastly, you can also use the chmod command to change the permissions and later on, restore the permissions to their original state. 

Of course, there are other ways to pull this off, like using shell scripting or a temporary file to achieve similar effects. 

If this guide helped you, please share it.

Leave a Reply
Related Posts