Jenkins sudo:没有终端和未指定NOPASSWD的askpass程序。

27

我花了几天时间设置文件/etc/sudoers,以便能够给用户jenkins授予root权限。我在我的服务器上安装了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

2
请参见https://dev59.com/12Ei5IYBdhLWcg3wFoy9#24648413(它谈论的是一行应该是sudoers中的最后一行..) - Jayan
1
请记住,这些“sudoers”设置需要在进行构建的特定节点上设置,该节点不必是“主节点”。 - luka5z
3个回答

44

我已经测试了@Jayan在评论中提到的解决方案。你必须在文件末尾包含新行:

解决方案:https://dev59.com/12Ei5IYBdhLWcg3wFoy9#24648413

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

将 "Adding jenkins ALL=(ALL) NOPASSWD: ALL" 添加到sudoers文件中非常有效,谢谢。 - M.A Majid
2
这真是不好的消息!如果您的Jenkins实例以任何形式变得容易受攻击,攻击者将完全控制主机。允许任何服务(特别是那些具有交互式语言的服务)获得root访问权限是一个坏主意。请使用setcap代替! - Braiam
糟糕透了!把钥匙交给Jenkins?如果有人利用Jenkins怎么办?游戏结束! - Barry Chapman

8

这只是一篇详细阐述如何修复该问题的响应。

首先,找出Jenkins GUI用于执行bash shell脚本的用户。

选择项目 > 配置 > 在可执行脚本中输入whoami > 保存并构建。 enter image description here 在构建历史记录中查看结果 > 点击控制台输出

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

SSH进入Jenkins服务器

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

4

很有可能您正在将错误的用户添加到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使用sudo命令时,因为没有TTY或没有指定askpass程序。解决方法是在sudoers文件中添加“NOPASSWD”选项并指定正确的路径。如果您不确定正确的路径,请尝试使用“which askpass”命令来查找它。

谢谢。whoami和https://gist.github.com/hayderimran7/9246dd195f785cf4783d上的操作指南为我解决了这个问题。 - Florian Straub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接