Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

A Jenkins job fails with sudo asking for a terminal and password

Errorsudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper

Short answer

A Jenkins build has no terminal, so sudo cannot ask for a password and exits. Prove it with sudo -n. The safe fix is a sudoers rule that lets the jenkins user run only the commands the job needs without a password. Piping a password into sudo -S works but leaves the password in your job.

  • Applies to sudo 1.9, Jenkins LTS 2.5xx, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run sudo in non-interactive mode as the same user the agent uses. -n makes sudo fail at once instead of prompting, which shows whether a password is needed.

    sudo -n true && echo "no password needed"
  2. 2Match what you see, then jump to its fix

    1. 1sudo: a terminal is required to read the passwordThe jenkins user has no passwordless rule for this commandFix
    2. 2The rule exists in /etc/sudoers.d but sudo still asks for the passwordA later sudoers rule cancels the NOPASSWD ruleFix
    3. 3sudo refuses to run from the job even though a NOPASSWD rule exists and Defaults requiretty appears in sudoersrequiretty is set in sudoersFix

What does the error look like?

  • sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
  • sudo works over SSH or in a terminal but not inside a Jenkins sh step

Cause 1 of 3

The jenkins user has no passwordless rule for this command

1Check

By default sudo asks for a password. In Jenkins there is no terminal to type it in, so sudo gives up. Check what the jenkins user is allowed to run.

sudo -l -U jenkins

2Fix

Create a drop-in file with visudo and give NOPASSWD only for the exact commands the job needs, not for ALL. The tag applies to the commands that follow it.

sudo visudo -f /etc/sudoers.d/jenkins
# contents:
jenkins ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp.service

3Verify

Run the same command as the jenkins user, without a prompt.

sudo -u jenkins sudo -n /usr/bin/systemctl restart myapp.service

Confirmed by sudo.ws

Did this fix it?

Cause 2 of 3

A later sudoers rule cancels the NOPASSWD rule

1Check

sudo applies the last matching entry, which is not necessarily the most specific one. A group line such as %sudo ALL=(ALL) ALL read after your file puts the password requirement back.

sudo -l -U jenkins

2Fix

Make your jenkins line come after the general lines. Files in /etc/sudoers.d are read in sorted order, so give the file a name that sorts last (for example zz-jenkins, with no dot in the name), and keep NOPASSWD before the command.

sudo visudo -f /etc/sudoers.d/zz-jenkins
jenkins ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp.service

3Verify

List the rules again; the command should show NOPASSWD.

sudo -l -U jenkins

Confirmed by sudo.ws

Did this fix it?

Cause 3 of 3

requiretty is set in sudoers

1Check

The requiretty flag makes sudo run only from a real tty, so it cannot run from cron, CGI scripts or a CI agent. It is off by default.

sudo grep -r requiretty /etc/sudoers /etc/sudoers.d/

2Fix

Turn the flag off for the jenkins user only.

sudo visudo -f /etc/sudoers.d/zz-jenkins
Defaults:jenkins !requiretty

3Verify

Run a sudo -n command as jenkins again.

sudo -u jenkins sudo -n true

Confirmed by sudo.ws

Did this fix it?

How do I stop it coming back?

  • Grant NOPASSWD for specific commands with full paths. Never give the jenkins user NOPASSWD: ALL.
  • Edit sudoers only with visudo, so a syntax error cannot lock you out.
  • Do not put the password in a job with echo pass | sudo -S: it ends up in build configuration and logs.
  • Where you can, remove the need for sudo, for example by deploying over SSH as a deploy user that owns the target directory.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.