:wq in Vim does not save

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


/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