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

Nick
  • 4,423
  • 4
  • 24
  • 41

2 Answers2

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.

kmicu
  • 2,395
  • 1
  • 15
  • 15
  • 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 00:05
  • Not working, still ```/usr/bin/sudo``` (I'm using Emacs 25.0.50.1, though) – Boccaperta-IT Dec 23 '14 at 08:13
  • I tested that with a fresh Emacs 24.4 w/o any config and w/ fresh HOME directory and it works. I will not test development version. Make sure to test it w/o your config or try to use second method with explicit alias for *eshell/sudo* (I adjusted my answer for that). Good luck. – kmicu Dec 23 '14 at 17:52
  • No worries, the alias solution works. Thank you. – Boccaperta-IT Dec 23 '14 at 18:19
  • I don't know about 24.4 but on Emacs 25, `password-cache-expiry` is in seconds, so the comment on your code should read `; for one minute`. – Omar Feb 10 '15 at 12:51
  • Yes, it is in seconds. Thanks for pointing that out. Cheers. – kmicu Feb 10 '15 at 21:13
  • This answer works, but I did not need to switch to the TRAMP version of `sudo` / `su` to get the password caching stuff to work; all I needed was for those variables to be set (and they were by default). – GDP2 Oct 11 '16 at 15:19
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)
manandearth
  • 2,068
  • 1
  • 11
  • 23
Emiluren
  • 71
  • 1
  • 1
  • also need to `(require 'esh-module)` – nymo Aug 23 '18 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 05 '23 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. – Patrick Poitras May 12 '23 at 20:50