【问题标题】:Jenkins sudo: no tty present and no askpass program specified with NOPASSWD [duplicate]Jenkins sudo:不存在 tty,也没有使用 NOPASSWD 指定 askpass 程序 [重复]
【发布时间】:2016-10-02 21:14:35
【问题描述】:

我花了几天时间设置文件/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

【问题讨论】:

  • 请看*.com/a/24648413/54506(它谈到了一行应该在sudoers的最后一行..)
  • 请记住,这些sudoers 设置需要在进行构建的特定节点上进行设置,而不必是master

标签: jenkins sudo


【解决方案1】:

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

解决方案:https://*.com/a/24648413/54506

#
# 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

【讨论】:

  • 添加 jenkins ALL=(ALL) NOPASSWD: ALL 到 sudoers 文件就像一个魅力,谢谢。
  • 这真是太棒了!如果任何形状或形式的 jenkins 实例变得易受攻击,攻击者将获得对主机的完全访问权限。允许任何服务(尤其是具有交互式语言的服务)root 访问权限是一个坏主意。请改用 setcap!
【解决方案2】:

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

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

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

用户可能是 tomcat 或 jenkins 或其他。

SSH 进入 Jenkins 服务器

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

【讨论】:

    【解决方案3】:

    这很可能是您在 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/

    【讨论】: