Abrir un archivo con su/sudo inside Emacs


Supongamos que quiero abrir un archivo en una sesión Emacs existente usando su o sudo, sin bajar a un shell y hacer sudoedit o sudo emacs. Una forma de hacer esto es

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

Pero esto requiere un costoso viaje de ida y vuelta a través de SSH. ¿Hay una manera más directa?

[EDITAR] @JBB tiene razón. Quiero ser capaz de invocar su/sudo para guardar como. Estaría bien (pero no lo ideal) volver a autorizar al guardar. Lo que estoy buscando son variaciones de find-file y save-buffer que puede ser "canalizado" a través su/sudo.

Author: emisilva, 2008-09-18

9 answers

Lo bueno de Tramp es que solo pagas por ese viaje de ida y vuelta a SSH cuando abres el primer archivo. Sudo almacena en caché sus credenciales, y Emacs guarda un identificador, por lo que los archivos posteriores abiertos por sudo tardan mucho menos tiempo.

Tampoco he encontrado el tiempo extra que se necesita para ahorrar carga. Es lo suficientemente rápido, IMO.

 59
Author: EfForEffort,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-09 03:44:11

Tramp hace nosudo de ida y vuelta a través de SSH, utiliza una subcapa . Ver el manual: https://www.gnu.org/software/tramp/#Inline-methods

Por lo tanto, te recomiendo que te quedes con TRAMP.

 62
Author: Teddy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-21 14:09:35

Si usa helm, helm-find-files soporta abrir un archivo como root con C-c r.

 14
Author: Qudit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-27 19:25:48

No es realmente una respuesta a la pregunta original, pero aquí hay una función auxiliar para hacer la ruta tramp/sudo un poco más fácil:

(defun sudo-find-file (file-name)
  "Like find file, but opens the file as root."
  (interactive "FSudo Find File: ")
  (let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
    (find-file tramp-file-name)))
 13
Author: Burton Samograd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-12 17:11:35

Al menos para guardar, se escribió un paquete sudo-save exactamente para ese tipo de problema.

 5
Author: huitseeker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-06-17 12:19:18

Su ejemplo no inicia ssh en absoluto, al menos no con mi versión de TRAMP ("2.1.13-pre"). Tanto find-file como save-buffer funcionan muy bien.

 4
Author: jfm3,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-19 03:14:41

Le recomiendo que utilice comandos de asesoramiento. Pon esta función en tu ~/.emacs

(defadvice ido-find-file (after find-file-sudo activate)
  "Find file as root if necessary."
  (unless (and buffer-file-name
               (file-writable-p buffer-file-name))
    (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
 2
Author: anquegi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-25 12:24:48

Ugh. Tal vez podrías abrir un shell en Emacs y exec sudo emacs.

El problema es que presumiblemente no solo desea abrir el archivo. Quieres poder guardarlo más tarde. Por lo tanto, necesita que sus privs raíz persistan, no solo existan para abrir el archivo.

Parece que quieres que Emacs se convierta en tu gestor de ventanas. Ya está bastante hinchado sin eso. :)

 0
Author: JBB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-18 19:03:15

(funciona solo localmente. Necesita ser actualizado para funcionar correctamente a través de tramp)

Un poco extendida respuesta de Burton:

(defun sudo-find-file (file-name)
"Like find file, but opens the file as root."
(interactive "FSudo Find File: ")
(let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
(find-file tramp-file-name)))


(add-hook 'dired-mode-hook
    (lambda ()
      ;; open current file as sudo 
      (local-set-key (kbd "C-x <M-S-return>") (lambda()
        (interactive)
        (message "!!! SUDO opening %s" (dired-file-name-at-point))
        (sudo-find-file (dired-file-name-at-point))
      ))
    )
)
 0
Author: alex_1948511,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-12 16:02:09