When you have to edit system files, it's an usual situation that you launched Emacs with a normal user before, so the file will be write-protected. What can I do to gain root privileges? I mean something like M-x sudo from dired.

I don't want to launch Emacs as super-user and I don't want to quit it.


Use C-x C-f and type out /su::/etc/hostname or /sudo::/etc/hostname as applicable.

This uses the TRAMP package, which is distributed with Emacs. This package provides access to remote files, and more generally to files that Emacs can't open directly.

  • 3
    Thanks. What is the meaning of the :: ? – smonff Sep 23 '14 at 22:29
  • 8
    The : is a separator; we supply it the second time to indicate that there are no more arguments. You can /su:otheruser@localhost:, for instance. – artagnon Sep 23 '14 at 22:51
  • 10
    /sudo:: is tramp shorthand for sudo:root@<hostname>: – phils Oct 6 '17 at 6:48

Use find-file with tramp. At the prompt /sudo::/etc/ssh/ssh_config will use sudo and a subshell to open that file with root privileges. When sudo is first used it will prompt for a password, but until that session closes you can edit any file with those permissions by prefixing with the existing /sudo:: command.

See http://www.gnu.org/software/tramp/ for more details.


If you use Helm. You don't need any external package / additional config. it works out of the box.

Inside helm-find-files you can invoke find file as root which bound to C-c r. This works for files or directories in helm-find-files session.

If you don't want to type password every time you open root files/dirs. Put the following line to your ~/.authinfo.gpg

machine localhost port sudo login root password xxxxxx
machine your_machine_name port sudo login root password xxxxx



There are today many packages that solve this problem (as of 2018). Some of them are:

  • sudo-edit, allows to switch editing rights on an already opened read-only file. Just M-x sudo and type a password and you're done. After many years of use of this one, I think it is the more convenient for this need. sudo-edit allows you to open a file as any user, but it default on sudo if none provided.
  • dired-toggle-sudo. It was the first packaged solution I found after asking this question. It is Dired oriented, so if you prefer the file oriented version, see the first bullet of this list.
  • Other solutions exists on Melpa
  • 1
    M-x sudo works out of the box in doom emacs. – Michael Dorst Apr 2 at 23:22
  • @michael-dorst I didn't know despite I am using it ^^ – smonff Apr 4 at 6:36
  • 1
    For Spacemacs users, SPC f E is the shortcut – Benjamin R May 2 at 13:03

If you use the ivy package, and have configured counsel-find-file to replace find-file, then one of the "ivy actions" pre-configured for you already is to edit the file as root. The default hydra keystroke for that action is r.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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