How to edit read-only file in /etc?
Solution 1
Changing system settings requires superuser permissions. From a terminal, do
sudo <editor> <filename>
where could be vim or nano or any other editor command, and is the one you need to edit.
You could also usegksudo gedit <filename>
.
In either case you will be prompted for password of a user with superuser permissions - in a normal Ubuntu installation this would be the first user created during installation.
Solution 2
If you are editing a file without sudo
, and you need sudo
in order to save, simply use this vim command:
:w !sudo tee %
Credit to Dr Beco. Note that vim will notice the file change and ask you if you want to (L)oad the changes, press L.
Solution 3
Very Short Answer:
You can modify a file (even if it's read-only) if you own it.
Short Answer:
Even if the user which you are logged in as (in this case navid
) has administrative privileges, you won't be able to modify /etc/hostname
, because the root
user owns that file. Consequently, you should log in as the root user.
Long Answer
Assuming that you want to change your computer name from oldName
to newName
, here are the steps that you should follow:
-
Log in as the root user:
navid@oldName:~$ sudo su -
-
Open hostname:
root@oldName:~# vi /etc/hostname
You will see
oldName
. Press i to go to the insert mode then change it tonewName
. Then press Esc + : + w + q + Enter to save and exit.-
Open hosts:
root@oldName:~# vi /etc/hosts
The top 2 lines look like this:
127.0.0.1 localhost 127.0.1.1 oldName
Similarly to what you did in step 3, change the computer name from
oldName
tonewName
. Then save and exit.-
Exit the root user:
root@oldName:~# exit
-
Save all of your unsaved work and restart your computer:
navid@oldName:~$ reboot
-
Open your terminal, and you will see that your computer name has successfully been changed! :-)
navid@newName:~$
Note: Although you may achieve what you want by skipping steps 4 and 5, I strongly recommend doing them as well, to avoid potential errors in the future.
See also:
Related videos on Youtube
WardenMorgan
Updated on September 18, 2022Comments
-
WardenMorgan over 1 year
I'm a brand new user so I'm not sure of my way around the system yet. I want to change my computer's name, so I tried to edit the name in
/etc/hostname
, but it is a read-only file. Adding the!
character didn't help. I'm usingvim
to edit the file.-
Admin over 9 yearsHow is tagMatcher's answer any better than askubuntu.com/a/92382/158442? There is literally nothing new. You can accept it if you want, but merging it is not needed.
-
-
owl about 8 yearsRead this, gksudo is removed? askubuntu.com/questions/290810/…
-
tagMacher about 8 yearsjust checked - gksudo is available on my Ubuntu 14.04 LTS
-
Vlad Havriuk over 6 yearsThx you! Very helpful!
-
Yoga over 6 yearsthanks! this works even when the file's permissions are 400 or 440, with no one having write permission!
-
Marses about 4 yearsThis doesn't work for me, running as sudo or even logged in as root. I get a message "Read-only file system" when I try this, and "Shel returned 1", with no change and no ability to save the file. -----EDIT: indeed, this can't be done when you're in a read-only filesystem (my example was an app installed with snap; I was trying to modify the .desktop file). However, there are alternative locations for the .desktop files, e.g: /var/lib/snap/desktop/applications
-
Tinashe Chinyanga over 2 yearssudo vi <filename> when intending to use Vi.