Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

By :wq I want save my text file on the vim. But when I run :wq I saw this error E45: 'readonly' option is set (add ! to override) also when I add ! to :wq like :wq! .I saw below

"/etc/dhcp/dhcpd.conf"

"/etc/dhcp/dhcpd.conf" E212: Can't open file for writing

I want edit a file name is dhcpd on the /etc/dhcpd path

share|improve this question
    
Prefer :x or ZZ to exit and save. It will write only on change. – Łukasz Niemier May 25 '14 at 11:08
up vote 5 down vote accepted

You need to open the file using superuser permissions as follows:

sudo vi /etc/dhcp/dhcpd.conf

edit the file by pressing i and then save and exit by pressing Esc and then either :wq or :x or just :w to save.


Thanks to Riking for suggesting sudoedit: you can use sudoedit /path/to/file/filename for editing files owned by root rather than using sudo <editor> /pat/to/file/filename. This is useful for enterprise-level machines or production machines since sudoedit logs to /var/log/auth.log.

If you want to change the default editor for sudoedit, do the following:

sudo update-alternatives --config editor

and then press Return and choose the editor of your choice and again press Return.


See also: A discussion on redit on why sudoedit may be advantegeous

share|improve this answer
3  
I prefer sudoedit for these situations - it creates a temporary copy, runs the editor, and copies the result. If you previously chose nano for that command, run EDITOR=vi sudoedit /etc/file. – Riking May 25 '14 at 8:18
1  
@Riking: Thanks for sudoedit, didn't even know it existed. However, came across this while googling sudoedit. I'll suggest it, but leave it to the user if they find it necessary to use. – i08in May 25 '14 at 8:43
    
sudoedit also picks up on the EDITOR variable, it's easier to set that than configure alternatives – muru Jun 21 at 7:24

/etc consists of all configurations files so to edit the files under /etc you need the superuser permissions but sometime we forget to use sudo. but at that time we can also use

:w !sudo tee %

then enter your administrator password

:q!

to exit successfully

share|improve this answer
    
You can add cmap w!! w !sudo tee > /dev/null % to your .vimrc to allow saving via this method with :w!!. If you're interested in why this method works see stackoverflow.com/questions/2600783/… – Gerhard Burger Jun 21 at 7:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.