How to edit a etc read only file? [duplicate]

Which text editor are you using? If you are a sudoer and know your password, try (in a terminal) either:

  • sudoedit /etc/dhcp/dhcpd.config if you are using a terminal-based editor such as nano or are ssh'ing into a server.
  • gksudo <text editor here> /etc/dhcp/dhcpd.config if you are using a GUI-based editor such as gedit

In either case, you'll need to first provide your password when requested.

In Ubuntu 14.04 onwards, gksudo is not installed by default. You will have to install the gksu package (either from the Software Centre or via sudo apt-get install gksu) to get it.

In Ubuntu 17.10, Wayland is default instead of the traditional X server, and running graphical editors as root is difficult. See Why don't gksu/gksudo or launching a graphical application with sudo work with Wayland? for details.

In Ubuntu 18.04, gksudo is not available in the official repositories. Use sudo -H <text editor> instead, or the admin:// protocol:

gedit admin:///etc/dhcp/dhcpd.config

If you don't want to use a terminal, you can invoke a GUI program as root thus (this example is for Gedit):

  1. Hit Alt+F2 to bring up the Run dialog.
  2. Type gksudo followed by the executable name of your program (which might be different from the name displayed by the GUI). For example:

    gksudo gedit
    
  3. Hit Enter.

I can't think of a case where root would get permission denied.

In 12.04 you can do it like this: alt+f2 and enter gksudo in the box that appears like this:

F2 dialogue

You get a box like this and enter gedit and click ok:

Type gedit in Run dialogue

Click Open:

Open tab inside gedit

Just browse to the file:

open files dialogue

Done

Warning: There is a super-user/root for a reason. It's so that you don't accidentally put something bad or out of structure in something important by mistake. You should be exceedingly careful that whatever you edit as root is the correct file you mean to edit AND that your syntax is perfect. If other tools are available for editing, it's recommended to use those. For instance, use the visudo terminal command instead of manually editing /etc/sudoers.

Some files that could get messed up are easy to recover from. Others are relatively complicated or you have to use recovery console and a lot of command lines.

Yes the recovery console command line seems a little scarier than even this. Just be careful.


You can test if your current shell is running as root a few different ways

~/ > whoami
paul
~/ > sudo whoami
root
~/ > id
uid=1000(paul) gid=1000(paul) groups=1000(devs),4(adm),20(dialout),24(cdrom),…
~/ > sudo id
uid=0(root) gid=0(root) group=0(root)

Other answers give good ways to escalate to root privilege, so I won't repeat those. Assuming you are root and you still can't edit the file /etc/dhcp/dhcpd.config - then very probably someone or some program has used the chattr program to make the file immutable.

From the chattr manual page:

A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.

You can find out by using lsattr

lsattr /etc/dhcp/dhcpd.config

If indeed it's immutable you can turn that off like this:

chattr -i /etc/dhcp/dhcpd.config