A Jenkins job fails with sudo asking for a terminal and password
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helperShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
sudo: a terminal is required to read the passwordThe jenkins user has no passwordless rule for this commandFix - 2
The rule exists in /etc/sudoers.d but sudo still asks for the passwordA later sudoers rule cancels the NOPASSWD ruleFix - 3
sudo refuses to run from the job even though a NOPASSWD rule exists and Defaults requiretty appears in sudoersrequiretty is set in sudoersFix
- 1
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 helpersudo 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 jenkins2Fix
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.service3Verify
Run the same command as the jenkins user, without a prompt.
sudo -u jenkins sudo -n /usr/bin/systemctl restart myapp.serviceConfirmed by sudo.ws
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 jenkins2Fix
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.service3Verify
List the rules again; the command should show NOPASSWD.
sudo -l -U jenkinsConfirmed by sudo.ws
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 !requiretty3Verify
Run a sudo -n command as jenkins again.
sudo -u jenkins sudo -n trueConfirmed by sudo.ws
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 fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
Apache warns that a ${VAR} config variable is not defined
${VAR} left unchanged in the configuration, with a warning in the error log
Apache expands ${VAR} from Define or from the shell environment that exists when the server starts.
3 causes, each with a fix