44

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?

42

Which text editor are you using? If you are a root user and know the 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 root 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
15

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.
10
+200

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 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). – Seth Mar 25 '14 at 22:07
  • 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. – RobotHumans Mar 25 '14 at 22:29
  • root can get permission denied if the file has been set immutable using chattr or sometimes if the disk is read-only. – Chai T. Rex Sep 24 '18 at 11:00
6

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
3

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.

2

Use Your Favourite Text Editor

You can use sudo -e or its alias sudoedit with your favourite (graphical!) text editor through the VISUAL environment variable1:

VISUAL=gedit sudo -e /path/to/some-config-file.cfg

which is equivalent to

VISUAL=gedit sudoedit /path/to/some-config-file.cfg

Of course you can use any other editor that you like, e. g. leafpad, kate, or subl.

Some editor offer a command-line option to always start a new instance, even if one is currently running. Since sudoedit waits for the editor process to terminate to apply changes to the edited file, this may be necessary if you have other instances of the same Editor running:

  • For GEdit use VISUAL='gedit -s'.
  • For Kate use VISUAL='kate -n'.

Advantages

  • Doesn’t require additional and/or deprecated applications.
  • Doesn’t require security policy changes like pkexec (for which you’d need to edit files… as super-user, the exact thing this answer is trying to solve).
  • Doesn’t require the execution of potentially unsafe applications with elevated privileges.
  • Uses the current user’s application configuration but mess with the configuration file access permissions like sudo -H etc.
  • Doesn’t fall back onto the user application configuration data of the root user which may or may not look and behave acceptably.

Disadvantages

  • You need to learn a short new command.

1 EDITOR or SUDO_EDITOR work too but take less priority or are more specialised respectively. See the manual page for details.

0

To be safe back it up using cp /etc/dhcp/dhcpd.config /$HOME/dhcpd.config.backup and run gksu gedit /etc/dhcp/dhcpd.config

Replace gedit with the editor of your choice

you will get a prompt like this

password prompt

Enter your password here.

A new window opens and you can edit your config there.

In case something goes wrong you can restore it from your backup in the home folder.

You get "permission denied" because of how Linux handles permission.

Anything in the $HOME folder belongs to the user, whereas almost anything else belongs to root.

The /etc/dhcp/dhcpd.conf file you are trying to access belongs to root, and most files in /etc/ belong to root by default for security reasons.

You can still edit this file by elevating your permissions.

To do this you have a number of ways:

  • use su which stands for switch user. Without any attributes to it, it functions as a command to switch to the root account, but you need to enter the password of the user you are switching to, and since root has no password by default on Ubuntu (effectively disabling the root account), this won't work unless you have set a password for root, which is not recommended.

  • using sudo which elevates your permission for only one command. This is generally used only for command-line apps such as CLI text editors like vim,nano etc

  • using gksu which elevates your permission for only one command, but unlike sudo is used for graphical programs such as GUI text editors like gedit . it uses a graphical prompt to facilitate entering of a password.

0
sudo < editor > < filelocation >

e.g.:

sudo -H gedit etc/dhcp/dhcpd.config

protected by Zanna Sep 12 '18 at 8:25

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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