14

In a general term runing bash, when I run a sudo ... command, bash will remember my password for a while. This makes package management task easier.

For example:

sudo apt-get update
# password ......
sudo apt-get install turing-brain
# execute without requiring password again.

Is it possible to accomplish it in eshall?

(I know term or ansi-term in Emacs can do this. But eshell integrate better with Emacs.)

14

First of all check which sudo is executed in your eshell session. It can be your system’s sudo:

$ which sudo
/path/to/system/wide/sudo
$ which *sudo
/path/to/system/wide/sudo

or eshell’s sudo:

$ which sudo
sudo is a compiled Lisp function in `em-tramp.el'
$ which eshell/sudo
eshell/sudo is a compiled Lisp function in `em-tramp.el'

Eshell’s sudo uses TRAMP's su or sudo method. These commands are in the eshell-tramp module, which is disabled by default.

I will cover eshell’s sudo case, because it is internal to Emacs and it does not depend on your OS distro:

  1. Load eshell-tramp module:

    (require 'em-tramp) ; to load eshell’s sudo
    
  2. Switch to eshell’s sudo

    • by prefering built-in commands

      (setq eshell-prefer-lisp-functions t)
      

      It seems that in Emacs 24.4 we need to set

      (setq eshell-prefer-lisp-variables t)
      
    • by creating an alias (execute snippet in eshell)

      alias sudo 'eshell/sudo $*'
      

      Aliases defined (or deleted) by the alias command are automatically written to the file named by eshell-aliases-file, which you can also edit directly (although you will have to manually reload it).

  3. Finally enable password caching for eshell’s sudo (and TRAMP):

    (setq password-cache t) ; enable password caching
    (setq password-cache-expiry 3600) ; for one hour (time in secs)
    

PS If you have changed your prompt with eshell-prompt-function, then remember to adjust prompt regex eshell-prompt-regexp accordingly. Wrong prompt regex can break some eshell functionality — including password detection.

  • After following these instructions, which sudo still displays /usr/bin/sudo on my system. – Boccaperta-IT Dec 22 '14 at 15:51
  • Did you test it with a fresh eshell buffer? IIRC current eshell session will not pick up changes. – kmicu Dec 22 '14 at 18:59
  • Yes. I restarted emacs daemon and open a new client. – Boccaperta-IT Dec 22 '14 at 20:30
  • After shallow testing I see that (setq eshell-prefer-lisp-variables t) is required in emacs 24.4. @Boccaperta-IT can you confirm if it works for you? – kmicu Dec 23 '14 at 0:05
  • Not working, still /usr/bin/sudo (I'm using Emacs 25.0.50.1, though) – Boccaperta-IT Dec 23 '14 at 8:13
3

To get sudo working in Emacs 26 (probably the same for Emacs 25) without making an alias I had to add eshell-tramp to eshell-modules-list.

(add-to-list 'eshell-modules-list 'eshell-tramp)
  • also need to (require 'esh-module) – nymo Aug 23 '18 at 17:43

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.