如何在Emacs中运行sudo命令?

时间:2022-08-02 21:48:54

I'm trying to create shortcut keys for some commonly used sudo shell commands (for example, having C-c s run (shell-command "sudo /etc/init.d/apache2 restart")).

我正在尝试为一些常用的sudo shell命令创建快捷键(例如,使用C-c s运行(shell命令“sudo / etc/init”)。d /输入重启”))。

I tried using a straight-up shell-command call as above, but it just outputs the following to the *Shell Command Output* buffer:

我尝试使用上面的直接Shell命令调用,但它只输出如下的*Shell命令输出*缓冲区:

[sudo] password for Inaimathi:
Sorry, try again.
[sudo] password for Inaimathi:
Sorry, try again.
[sudo] password for Inaimathi:
Sorry, try again.
sudo: 3 incorrect password attempts

It doesn't actually ask for a password. I don't want to have to start up Emacs using sudo emacs, but I guess that's an option if nothing else will work.

它实际上并不要求密码。我不希望使用sudo Emacs启动Emacs,但我想这是一个选项,如果没有别的办法。

The ideal solution would be a function from within Emacs (as opposed to OS jiggery-pokery to change the behaviour of the shell or the sudo command). Something like (sudo-shell-command "dostuff"), or (with-password-prompt (shell-command "sudo dostuff")).

理想的解决方案将是Emacs中的一个函数(与OS jiggery-pokery相反,以改变shell或sudo命令的行为)。类似于(sudo-shell-command“dostuff”),或者(with_passwordprompt (shell命令“sudo dostuff”))。

7 个解决方案

#1


13  

How about:

如何:

(shell-command (concat "echo " (shell-quote-argument (read-passwd "Password? "))
                       " | sudo -S your_command_here"))

#2


11  

I frequently call commands from Emacs like aptitude update. scottfrazer's solution might not be as useful. Synchronous commands make me wait for a long time, and if you execute an unsupported program (for example, aptitude, which uses ncurses), you will hang up Emacs (C-g won't help), and CPU load will be 100%. Changing to async-shell-command solves this.

我经常调用Emacs的命令,比如智能更新。scottfrazer的解决方案可能没有那么有用。同步命令让我等了很长时间,如果您执行一个不受支持的程序(例如,使用ncurses的能力),您将挂起Emacs (C-g不会帮助),CPU负载将是100%。更改为async-shell命令可以解决这个问题。

But it also introduces a new problem. If your command fails, your password will end up in *Messages* buffer:

但它也带来了一个新的问题。如果您的命令失败,您的密码将在*消息*缓冲区中结束:

echo PASSWORD | sudo -S aptitude: exited abnormally with code 1.

That's why i propose the following solution:

这就是我提出以下解决方案的原因:

(defun sudo-shell-command (command)
  (interactive "MShell command (root): ")
  (with-temp-buffer
    (cd "/sudo::/")
    (async-shell-command command)))

Here "M" in interactive prompts for program name in minibuffer, with-temp-buffer creates a sham buffer, in which we change directory to /sudo::/ to use TRAMP for sudo prompt.

这里的“M”是在minibuffer中为程序名的交互提示,使用-temp-buffer创建一个伪缓冲区,在其中我们将目录更改为/sudo::/使用TRAMP为sudo提示。

This is the solution by David Kastrup from sudo command with minibuffer password prompt @ gnu.emacs.help.

这是来自sudo命令的David Kastrup的解决方案,使用minibuffer密码提示符@ gnu.emacs.help。

Note, you still shouldn't call aptitude directly, otherwise the subprocess will be there forever, until you send sudo pkill aptitude.

注意,你仍然不应该直接调用资质,否则子流程将永远存在,直到你发送sudo pkill才能。

Read on shells and processes in manual.

阅读手册中的shell和流程。

#3


3  

If you're running emacs22 or later, you can just start up a shell from emacs and run your sudo command there. It'll automatically pull you into the minibuffer window for your password:

如果您正在运行emacs22或稍后,您可以从emacs启动一个shell并在那里运行您的sudo命令。它会自动将你拉入到你的密码的最小缓冲窗口:

M-x shell
sudo whoami

This should just ask for your password down at the bottom of the screen (without displaying it).

这应该要求你的密码在屏幕底部(不显示它)。

#4


3  

Workaround (rather than an emacs solution):

解决方案(而不是emacs解决方案):

Set up a ssh key pair so that no password is necessary.

设置ssh密钥对,以便不需要密码。

Procedure:

过程:

  1. run ssh-keygen to generate a pair of keys. Give them a useful name to keep them sorted out from all the others you'll make once you get use to this
  2. 运行ssh-keygen生成一对密钥。给他们一个有用的名字,让他们在你习惯了之后,把他们从其他人那里整理出来。
  3. Copy the public one to $HOME/.ssh for the receiving account
  4. 将公众号复制到$HOME/。ssh用于接收帐户。
  5. Keep the private one in $HOME/.ssh of the sending account (you could copy it to multiple sending accounts, but it might be better to make a separate keypair for every incoming machine)
  6. 把私人的留在家里。发送帐户的ssh(您可以将其复制到多个发送帐户,但最好为每台传入的机器分别创建一个密钥对)
  7. edit $HOME/.ssh/config on the sending machine to tell ssh what key to use
  8. 编辑$ HOME /。在发送机器上的ssh/config告诉ssh使用什么密钥。
  9. Profit
  10. 利润

#5


1  

sudo attempts to open the terminal device (tty) to read the password. Your emacs process may not have a controlling terminal. sudo -S tells it to use the standard input for a password which should be coming from emacs.

sudo尝试打开终端设备(tty)来读取密码。您的emacs过程可能没有控制终端。sudo -S告诉它使用来自emacs的密码的标准输入。

#6


1  

EDIT: Scott's answer above is vastly preferable to this hack. Use that.

编辑:斯科特的回答比这个更可取。使用它。

A possible solution:

一个可能的解决方案:

I found out that setting a default password-asking utility solves this problem.

我发现设置一个默认密码请求工具可以解决这个问题。

What I had to do is add Defaults:ALL askpass=/usr/lib/openssh/gnome-ssh-askpass to my /etc/sudoers file (using sudo visudo, obviously).

我需要做的是添加默认值:所有askpass=/usr/lib/openssh/gnome-ssh-askpass到我的/etc/sudoers文件(显然,使用sudo visudo)。

Eval-ing (shell-command "sudo /etc/init.d/apache2 restart") now prompts me for a password instead of trying to guess it unsuccessfully.

Eval-ing(shell命令sudo /etc/init.d/apache2重启“)现在提示我输入密码,而不是试图猜测它是否成功。

I'm not accepting this answer unless it becomes clear that there's no better solution; ideally I'd like something that solves the problem internally to Emacs instead of requiring you to poke around your /etc directory.

我不接受这个答案,除非很明显,没有更好的解决方案;理想情况下,我希望能在内部解决这个问题,而不是要求您在您的/etc目录下进行查找。

#7


0  

I used the following to start nmap from emacs as root,

我使用下面的方法从emacs作为根开始nmap,

http://nakkaya.com/sudoEl.markdown

http://nakkaya.com/sudoEl.markdown

#1


13  

How about:

如何:

(shell-command (concat "echo " (shell-quote-argument (read-passwd "Password? "))
                       " | sudo -S your_command_here"))

#2


11  

I frequently call commands from Emacs like aptitude update. scottfrazer's solution might not be as useful. Synchronous commands make me wait for a long time, and if you execute an unsupported program (for example, aptitude, which uses ncurses), you will hang up Emacs (C-g won't help), and CPU load will be 100%. Changing to async-shell-command solves this.

我经常调用Emacs的命令,比如智能更新。scottfrazer的解决方案可能没有那么有用。同步命令让我等了很长时间,如果您执行一个不受支持的程序(例如,使用ncurses的能力),您将挂起Emacs (C-g不会帮助),CPU负载将是100%。更改为async-shell命令可以解决这个问题。

But it also introduces a new problem. If your command fails, your password will end up in *Messages* buffer:

但它也带来了一个新的问题。如果您的命令失败,您的密码将在*消息*缓冲区中结束:

echo PASSWORD | sudo -S aptitude: exited abnormally with code 1.

That's why i propose the following solution:

这就是我提出以下解决方案的原因:

(defun sudo-shell-command (command)
  (interactive "MShell command (root): ")
  (with-temp-buffer
    (cd "/sudo::/")
    (async-shell-command command)))

Here "M" in interactive prompts for program name in minibuffer, with-temp-buffer creates a sham buffer, in which we change directory to /sudo::/ to use TRAMP for sudo prompt.

这里的“M”是在minibuffer中为程序名的交互提示,使用-temp-buffer创建一个伪缓冲区,在其中我们将目录更改为/sudo::/使用TRAMP为sudo提示。

This is the solution by David Kastrup from sudo command with minibuffer password prompt @ gnu.emacs.help.

这是来自sudo命令的David Kastrup的解决方案,使用minibuffer密码提示符@ gnu.emacs.help。

Note, you still shouldn't call aptitude directly, otherwise the subprocess will be there forever, until you send sudo pkill aptitude.

注意,你仍然不应该直接调用资质,否则子流程将永远存在,直到你发送sudo pkill才能。

Read on shells and processes in manual.

阅读手册中的shell和流程。

#3


3  

If you're running emacs22 or later, you can just start up a shell from emacs and run your sudo command there. It'll automatically pull you into the minibuffer window for your password:

如果您正在运行emacs22或稍后,您可以从emacs启动一个shell并在那里运行您的sudo命令。它会自动将你拉入到你的密码的最小缓冲窗口:

M-x shell
sudo whoami

This should just ask for your password down at the bottom of the screen (without displaying it).

这应该要求你的密码在屏幕底部(不显示它)。

#4


3  

Workaround (rather than an emacs solution):

解决方案(而不是emacs解决方案):

Set up a ssh key pair so that no password is necessary.

设置ssh密钥对,以便不需要密码。

Procedure:

过程:

  1. run ssh-keygen to generate a pair of keys. Give them a useful name to keep them sorted out from all the others you'll make once you get use to this
  2. 运行ssh-keygen生成一对密钥。给他们一个有用的名字,让他们在你习惯了之后,把他们从其他人那里整理出来。
  3. Copy the public one to $HOME/.ssh for the receiving account
  4. 将公众号复制到$HOME/。ssh用于接收帐户。
  5. Keep the private one in $HOME/.ssh of the sending account (you could copy it to multiple sending accounts, but it might be better to make a separate keypair for every incoming machine)
  6. 把私人的留在家里。发送帐户的ssh(您可以将其复制到多个发送帐户,但最好为每台传入的机器分别创建一个密钥对)
  7. edit $HOME/.ssh/config on the sending machine to tell ssh what key to use
  8. 编辑$ HOME /。在发送机器上的ssh/config告诉ssh使用什么密钥。
  9. Profit
  10. 利润

#5


1  

sudo attempts to open the terminal device (tty) to read the password. Your emacs process may not have a controlling terminal. sudo -S tells it to use the standard input for a password which should be coming from emacs.

sudo尝试打开终端设备(tty)来读取密码。您的emacs过程可能没有控制终端。sudo -S告诉它使用来自emacs的密码的标准输入。

#6


1  

EDIT: Scott's answer above is vastly preferable to this hack. Use that.

编辑:斯科特的回答比这个更可取。使用它。

A possible solution:

一个可能的解决方案:

I found out that setting a default password-asking utility solves this problem.

我发现设置一个默认密码请求工具可以解决这个问题。

What I had to do is add Defaults:ALL askpass=/usr/lib/openssh/gnome-ssh-askpass to my /etc/sudoers file (using sudo visudo, obviously).

我需要做的是添加默认值:所有askpass=/usr/lib/openssh/gnome-ssh-askpass到我的/etc/sudoers文件(显然,使用sudo visudo)。

Eval-ing (shell-command "sudo /etc/init.d/apache2 restart") now prompts me for a password instead of trying to guess it unsuccessfully.

Eval-ing(shell命令sudo /etc/init.d/apache2重启“)现在提示我输入密码,而不是试图猜测它是否成功。

I'm not accepting this answer unless it becomes clear that there's no better solution; ideally I'd like something that solves the problem internally to Emacs instead of requiring you to poke around your /etc directory.

我不接受这个答案,除非很明显,没有更好的解决方案;理想情况下,我希望能在内部解决这个问题,而不是要求您在您的/etc目录下进行查找。

#7


0  

I used the following to start nmap from emacs as root,

我使用下面的方法从emacs作为根开始nmap,

http://nakkaya.com/sudoEl.markdown

http://nakkaya.com/sudoEl.markdown