Jenkins sudo:没有 tty 存在,也没有用 NOPASSWD 指定的 askpass 程序

Jenkins sudo: no tty present and no askpass program specified with NOPASSWD

我花了几天时间设置文件 /etc/sudoers 以便能够将 root 权限授予用户 jenkins。我在我的服务器上安装了 Jenkins,因为我用 symfony、ionic、neo4j 等托管了几个项目......问题是我不能用 ionic 在项目中构建,我得到这个错误:sudo: no tty present and no askpass program specified. 这是我的 /etc/sudoers 文件的内容:

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL
jenkins ALL=(ALL) NOPASSWD: ALL
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

我已经测试了@Jayan在问题评论中描述的解决方案。您必须在文件末尾包含新行:

解决方案:

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
jenkins ALL=(ALL) NOPASSWD: ALL

这很可能是您向 sudoers 文件添加了错误的用户。这也发生在我身上,在 sudoers 文件中添加条目的解决方案对我不起作用。您将需要知道您的 jenkins 用来执行命令的实际用户。为此,您可以使用以下命令在您的 jenkins 中添加构建步骤(执行 shell):

whoami

然后再次尝试 运行 jenkins 作业,在控制台输出中,您将看到 jenkins 用来执行命令的用户。然后,您需要将此用户添加到您的 sudoers 文件中。例如,如果用户是 'tomcat',您将在 sudoers 文件的末尾添加以下行:

tomcat ALL=(ALL) NOPASSWD: ALL

参考:http://techrofile.com/jenkins-sudo-no-tty-present-and-no-askpass-program-specified-with-nopasswd/

这只是详细说明解决问题的步骤的回复。

首先,找出 Jenkins GUI 正在使用哪个用户执行 bash shell 脚本。

select 项目 > 配置 > 在可执行文件中输入 whoami shell > 保存并立即构建。 在构建历史中查看结果 > 单击控制台输出

用户可能是 tomcat 或 jenkins 或其他任何人。

通过 SSH 连接到 Jenkins 服务器

  1. sudo su
  2. visudo
  3. tomcat ALL=(ALL) NOPASSWD: ALL #if 显示的用户是 tomcat