Emacs not loading init file when run as sudo

I'm a little of topic, but you could use Emacs as a normal user, and tramp to open file as root:

C-xC-f /sudo::/path/to/file

will ask you for your sudo password, and open the file as root.


When you run as sudo add -s to preserve your environment. It's not maintaining your environment, thus your HOME directory is lost. Emacs looks in ~/.emacs, ~/.emacs.el, or ~/.emacs.d/init.el, so if you lose HOME, you lose the pathing to your startup files.


sudo -s didn't work for me. Here is the work-around I used.

Load emacs as a normal user (so your settings take effect)

  • Type M-x customize-themes
  • Use the link to migrate your settings to a theme.
  • Give your theme a name. (I unclicked the Remove saved theme settings from Custom save file. Don't know if that affected it)
  • Click save theme and note where the theme is saved.
  • Exit emacs and copy your theme file to your default emacs theme folder (e.g. ./usr/share/emacs/24.4/etc/themes/)
  • Now, open emac with sudo.
  • Type M-x customize-themes again and you should see your newly created theme as selectable.
  • Click Save Theme Settings and your sudo session will now use your custom theme in the future.

I wish there was a more elegant way of fixing this but it gets the job done.


jmq is right. In alternative tricky solution. If you are the only one allowed to be sudoer or to login as root, you can add a symlink in /root/ to your .emacs.d directory. For example:

ls -l /root/.emacs
lrwxrwxrwx. 1 root root 18 May  5 11:14 /root/.emacs -> /home/<user>/.emacs