Keep the user's configuration when running emacs in sudo mode

Asked
Active3 hr before
Viewed126 times

8 Answers

runningconfiguration
90%

I believe the best option here is to run Emacs normally and then edit the file as root using TRAMP. In this case I think prefixing the file with /sudo:: will do the trick, e.g. C-x C-f /sudo::/var/www/html/foo.php RET. Emacs will prompt you for your password, just like sudo would on the command line.,I have to run emacs in sudo mode, to edit some .html or .php files in my /var/www directory. When I run it in normal-user mode, there's no problem with the syntax, and the colors (I installed the php-mode.el extension). Unfortunately when I run it in sudo mode, I lose this configuration. Is there any way to get it back?,That's completely expected. When you run a command with sudo you're running it as a different user, usually root. In most cases the target user's configuration will be used.,The sudoedit command / sudo -e option exists for the purpose of editing files owned by other users. The editor which will be used is described in the manual (man sudo) under the description for the -e option:

Unfortunately when I run it in sudo mode, I loose this configuration, which is sad.

That's completely expected. When you run a command with sudo you're running it as a different user, usually root. In most cases the target user's configuration will be used.

sudo

Unfortunately when I run it in sudo mode, I loose this configuration, which is sad.

That's completely expected. When you run a command with sudo you're running it as a different user, usually root. In most cases the target user's configuration will be used.

root
load more v
88%

sudo emacs runs as the root user. One consequence is Emacs won't look for your .emacs in the home directory of your normal user, it will look in root's home directory instead. So none of your config will be loaded, and you don't have access to any packages installed by your normal user.,As mentioned in a comment, you should run Emacs as your normal user, and use tramp to open files as root. If you're trying something more arcane, we'll need more info to help you.,make symlinks in root home dir,make symlinks in root home dir

sudo ln - s~/.emacs /root
sudo ln - s~/.emacs.d /root /
72%

His suggestion of sudo -s, if it works, is the simplest, but there's a good chance it won't work. The problem is really a matter of how your sudoers is setup. The sudoers is likely setup to reset your environment, which means it doesn't preserve the HOME directory for your user when it runs the sudo command. The possible options are presented here as the top rated response, and using -s can't work around most of the sudoers settings that will screw it up in the first place.,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., Stack Exchange network consists of 178 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. ,Click Save Theme Settings and your sudo session will now use your custom theme in the future.

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
load more v
65%

You may find it convenient to have all your Emacs configuration in one directory, in which case you should use ~/.emacs.d/init.el or the XDG-compatible ~/.config/emacs/init.el. ,You can use the command line switch ‘-q’ to prevent loading your init file, and ‘-u’ (or ‘--user’) to specify a different user’s init file (see Initial Options). ,If you are going to write actual Emacs Lisp programs that go beyond minor customization, you should read the Emacs Lisp Reference Manual. See Emacs Lisp in the Emacs Lisp Reference Manual. , Next: Authentication, Previous: Key Bindings, Up: Customization   [Contents][Index]

There can also be a default init file, which is the library named default.el, found via the standard search path for libraries. The Emacs distribution contains no such library; your site may create one for local customizations. If this library exists, it is loaded whenever you start Emacs (except when you specify ‘-q’). But your init file, if any, is loaded first; if it sets inhibit-default-init non-nil, then default is not loaded.

inhibit -
   default -init

There can also be a default init file, which is the library named default.el, found via the standard search path for libraries. The Emacs distribution contains no such library; your site may create one for local customizations. If this library exists, it is loaded whenever you start Emacs (except when you specify ‘-q’). But your init file, if any, is loaded first; if it sets inhibit-default-init non-nil, then default is not loaded.

nil
load more v
75%

We can now save the file, quit emacs, change the file’s permission and run it from the command prompt.,menu-bar-mode: This can switch the main menu on or off.,To change the major mode from within emacs, press the M-x keys. The mini buffer will wait for your response. Examples of some major modes are:,Once the spell checking completes, the mini buffer will show a message like this:

You can check if your Linux system has emacs installed by simply running the following command:

emacs
load more v
40%

You can also open the config file in bash instead of a separate window.,Type the following command as sudo in Terminal:,Once done with the installation, you can open and edit any config file in it.,To install Emacs editor, launch Terminal by pressing Ctrl+Alt+T key combinations. Type the below command in Terminal as sudo:

To edit a config file in text editor, launch Terminal by pressing Ctrl+Alt+T key combinations. Then type the below command as sudo:

$ sudo gedit / path / to / filename
load more v
22%

Use sudo for command line programs (like nano), but use gksu or gksudo for GUI programs, which often use configuration files in the home directory. If you use plain sudo, the root user can take ownership or your user ID's configuration files and the program used that way will stop working (unless you continue using sudo). An alternative to gksu and gksudo is sudo -H,That means open that file with say the text editor nano but ensure you use root powers to edit it or it won't save. How, do: ,The answer you provided in the link wants you to open a file with root privileges, which can be achieved using these two commands ,Please be sure to answer the question. Provide details and share your research!

The answer you provided in the link wants you to open a file with root privileges, which can be achieved using these two commands

sudo nano / path / to / your / file.foo

in the command above you are opening file.foo in a command based text editing tool called nano, In your particular case this command should work.

sudo nano / etc / NetworkManager / NetworkManager.conf

or

gksu gedit / path / to / your / file.foo

In this command you're opening the same file with a GUI based text editor called Gedit (default on Ubuntu and Ubuntu GNOME), In your particular case, this command should work

gksu gedit / etc / NetworkManager / NetworkManager.conf

An explanation for commenting out is to add a hash # sign in front of the line, in the example you gave, the line

dns = dnsmasq

should become

#dns = dnsmasq
load more v
60%

ctrl-x u : undo the last command.,ctrl-x ctrl-c : Save and quit.,STEP 4: Install Emacs.cd /emacs/emacs-26.1/ ./configure #Configure Emacs make #build components using makefile sudo make install #Install Emacs ,ctrl-x ctrl-s : Save File. This saves the current buffer content to the file.

Ubuntu / Debian:
 sudo apt - get install emacs
load more v

Other "running-configuration" queries related to "Keep the user's configuration when running emacs in sudo mode"