当尝试执行sudo命令[duplicate]时,shell脚本给出了“没有tty present和没有askpass程序指定”

时间:2021-10-19 01:15:31

This question already has an answer here:

这个问题已经有了答案:

  • I have a shell script which creates a user and executes another script as that user

    我有一个shell脚本,它创建一个用户并执行另一个脚本作为该用户。

    sudo useradd -m devops
    sudo passwd devops
    sudo adduser devops sudo  
    su - devops -c "sh /path/to/myscript.sh"
    
  • This script creates the user,sets the password and adds user to sudo group as expected.

    该脚本创建用户,设置密码,并按预期将用户添加到sudo组。

  • myscript.sh contains commands which uses sudo previlages. (sudo apt-get update, sudo apt-get install software-properties-common etc.). And other commands like ssh-keygen,curl and wget.
  • myscript。sh包含使用sudo previlages的命令。(sudo apt-get update, sudo ap -get install software-properties-common等)。以及其他命令,如ssh-keygen、curl和wget。
  • All commands except the one's with sudo are executed correctly and producing results as excepted.
  • 除了sudo命令外,所有命令都被正确执行并产生结果。
  • But commands having sudo fails by giving the error "no tty present and no askpass program specified"
  • 但是有sudo的命令失败了,因为它给出了错误“没有tty present和没有指定askpass程序”
  • Why does this happen in this case and how can I overcome this?
  • 为什么会发生这种情况,我该如何克服呢?
  • I have seen similiar questions but will be thankful if I get a clear explanation in this context,thank you.
  • 我见过类似的问题,但如果我能在这种情况下得到一个清晰的解释,我会很感激的,谢谢你。

1 个解决方案

#1


3  

Try to replace this:

尝试更换:

su - devops -c "sh /path/to/myscript.sh"

with this:

用这个:

sudo -u devops -H sh -c "sh /path/to/myscript.sh"

The -c option of su doesn't support interactive mode:

su的-c选项不支持交互模式:

-c, --command COMMAND Specify a command that will be invoked by the shell using its -c.

命令命令指定一个命令,shell将使用它的-c调用该命令。

The executed command will have no controlling terminal. This option cannot be used to execute interractive programs which need a controlling TTY.

执行的命令将没有控制终端。此选项不能用于执行需要控制的交互式程序。

(man su)

(苏)

By the way, I wouldn't use sudo within a script everywhere. The script might simply require root permissions. Within the script you might drop privileges where necessary by means of the above-mentioned sudo command.

顺便说一下,我不会在脚本中到处使用sudo。脚本可能只需要根权限。在脚本中,您可以通过上述sudo命令在必要时删除特权。

#1


3  

Try to replace this:

尝试更换:

su - devops -c "sh /path/to/myscript.sh"

with this:

用这个:

sudo -u devops -H sh -c "sh /path/to/myscript.sh"

The -c option of su doesn't support interactive mode:

su的-c选项不支持交互模式:

-c, --command COMMAND Specify a command that will be invoked by the shell using its -c.

命令命令指定一个命令,shell将使用它的-c调用该命令。

The executed command will have no controlling terminal. This option cannot be used to execute interractive programs which need a controlling TTY.

执行的命令将没有控制终端。此选项不能用于执行需要控制的交互式程序。

(man su)

(苏)

By the way, I wouldn't use sudo within a script everywhere. The script might simply require root permissions. Within the script you might drop privileges where necessary by means of the above-mentioned sudo command.

顺便说一下,我不会在脚本中到处使用sudo。脚本可能只需要根权限。在脚本中,您可以通过上述sudo命令在必要时删除特权。