Getting root permissions on a file inside of vi on Linux

There are many scenarios where we think that we have opened a file to make changes using the root user, but when we actually try saving the changes, we realize that the file was opened with a normal user or with a user that doesn't have the specific permission to edit the file. In such cases, we usually have only one option and that is to close the file with the command shown below −

:q!

And then, open the file again with the command shown below −

sudo su
vi file.txt

Make changes and save the file with the command shown below −

:wq!

While this is all simple, what if we actually want not to make use of the sudo su command and still be able to edit and save the file that doesn't allow other users except root.

Solution − Using Tee Command

In order to do the same, we can make use of the command mentioned below −

:w !sudo tee filename

The above command is a bit complex to understand first, let me break it down. The : (colon) symbol is what begins the command and then we write w just after it, which stands for write permission, which normally accepts a file path to which to write.

Then, we have !sudo where the sudo keyword is obvious, which allows you to run the command as super-user. The ! symbol is used to execute an external shell command from within vi. Then following the !sudo we have the tee command which pipes stdin to the given file.

How It Works

The process works as follows −

  • The :w writes the buffer contents to stdout (instead of a file)

  • The ! executes an external shell command

  • The sudo tee filename receives the file contents via stdin and writes to the file with root privileges

  • The file modes and attributes are preserved without creating a new file

Example

Now that we know what the command actually does, let's use it on a simple file where the file only allows a super-user to edit it.

Consider we have a file in /usr/local/go/src which is the default path of Go's source code. The name of the file is symtab.go which can be found at this location −

/usr/local/go/src/runtime/symtab.go

If we are using any user that doesn't have the privileges of the super-user and open this file and then try to change the content and save the file with the command shown below −

:wq!

Then we will get the following error −

"symtab.go" E212: Can't open file for writing

This is because we don't have the permission to make changes to the file. The workaround is to use the command shown below in your vi editor −

:w !sudo tee symtab.go

After executing this command, you will be prompted for your sudo password, and the file will be saved with root privileges.

Alternative Methods

There are other approaches to handle this situation −

Method Command Description
Tee with redirect :w !sudo tee % > /dev/null Suppresses tee output to terminal
Direct sudo vi sudo vi filename Opens file with root privileges from start
Sudo with specific editor sudo -e filename Uses EDITOR environment variable with sudo

Key Points

  • The % symbol in vi represents the current filename, so :w !sudo tee % works without typing the filename

  • Adding > /dev/null prevents tee from displaying the file contents on screen

  • This method works for any file that requires elevated permissions

  • You'll need to enter your sudo password when prompted

Conclusion

The :w !sudo tee filename command allows you to save files with root permissions from within vi without exiting the editor. This technique is particularly useful when you discover permission issues after making changes to a file. It combines vi's write command with the system's tee utility and sudo privileges to overcome permission restrictions efficiently.

Updated on: 2026-03-17T09:01:38+05:30

915 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements