20

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.)

2 Answers 2

17

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.

10
  • After following these instructions, which sudo still displays /usr/bin/sudo on my system. Dec 22, 2014 at 15:51
  • Did you test it with a fresh eshell buffer? IIRC current eshell session will not pick up changes.
    – kmicu
    Dec 22, 2014 at 18:59
  • Yes. I restarted emacs daemon and open a new client. Dec 22, 2014 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, 2014 at 0:05
  • Not working, still /usr/bin/sudo (I'm using Emacs 25.0.50.1, though) Dec 23, 2014 at 8:13
7

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)
3
  • also need to (require 'esh-module)
    – nymo
    Aug 23, 2018 at 17:43
  • @nymo looks like we no longer need (require 'esh-module) with recent versions of Emacs (27.1+, I think) (I also updated TRAMP to the latest release through GNU ELPA in case that has anything to do with it).
    – mtraceur
    May 5 at 19:53
  • I've simply added (add-hook 'eshell-load-hook (lambda () (add-to-list 'eshell-modules-list 'eshell-tramp))) and it's sufficient to get everything running whenever eshell is started. May 12 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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