On a Linux system, vim and vi are the most popular text editors. We can edit the content of the files and able save it using vi/vim editor.
By default the server configuration files are owned by the root user, hence the normal user can not write the changes due to insufficient Permission.
This article will guide you to the basic commands about how to save a file after writing or modifying its content without Root Permission using sudo option.
Steps to edit the file /etc/hosts
as a normal user.
# vi /etc/hosts
After making some changes (E.g Adding a new entry) in the file, save it by typing the following normal command which you can see that vim/vi
CANNOT open the file due to the permission. :
:w!
and then, Enter
.
Now you try to save the file by followingpressing
,
:w !sudo tee %
:w – Write a file
!sudo – Call shell with sudo command
tee – The output of write command redirected using tee
% - Indicate the Current filename. Here it is /etc/hosts
And then, Enter
.
Enter the password
of the logged-in user and then Enter
.
Press L
to load changes in vim itself.
Now open the file again to see the changes.
# cat /etc/hosts
Related Tutorials