How do I get permissions to edit system configuration files?

301,328

Solution 1

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.
  • gedit admin:///etc/dhcp/dhcpd.config using the the admin:// protocol with GNOME applications such as gedit (and others that support GVfs).
  • sudo -H <text editor> /etc/dhcp/dhcpd.config for GUI editors in general.
  • In old versions of Ubuntu, gksudo <text editor here> /etc/dhcp/dhcpd.config could be used to launch GUI editors.

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

If you are using Ubuntu 17.04 or higher, it is recommended to use the gvfs admin backend. Simply add admin:// to the front of the full filepath you want to open in an app like the Text Editor or the Files apps.

For instance, to change boot settings, open

admin:///etc/default/grub

If you are using Ubuntu 17.10 with the default Wayland, sudo and gksu as mentioned in the older answer here will not work.

Share:
301,328

Related videos on Youtube

Matthew Wilson
Author by

Matthew Wilson

beer

Updated on September 18, 2022

Comments

  • Matthew Wilson
    Matthew Wilson almost 2 years

    I can't seem to edit and save config files in the root system (eg: /etc/dhcp/dhcpd.config); it just says "permission denied" or the save option is blocked in the text editor.

    I'm pretty sure I'm a root user, but how do I make sure? If I am a root user, how do I fix this?

  • web.learner
    web.learner over 10 years
    You might need to update this for 13.04 and newer. Some things relating to gksu/gksudo have changed (not sure if that applies to your answer or not though).
  • RobotHumans
    RobotHumans over 10 years
    It's my understanding sudo now magically exports DISPLAY environment variables and gksu/gksudo have gone or are going away. I haven't checked though.
  • Chai T. Rex
    Chai T. Rex almost 6 years
    root can get permission denied if the file has been set immutable using chattr or sometimes if the disk is read-only.
  • Murtaza Haji
    Murtaza Haji over 3 years
    The save location changes for some reason to var/tmp/filename. Any idea why this could be happening?