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

When you are setting up a Jenkins job, you can add a build step to run ssh commands. There is known issue that you can encounter too when doing this. Your job will probably fail with the following error message:-

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

Actually this happens because the Jenkins tries to execute a shell command without a terminal session, hence there is no password that can be used by this process.You will somehow need to grant jenkins user to execute these shell commands without prompting for passwords. When your search the internet, you will find unanimous solution which is as following.

Edit the sudoers file by executing the following (Note that you need to execute this command through ssh and not through jenkins)

sudo visudo

Now add the following line to the very end of this file

jenkins ALL=(ALL) NOPASSWD: ALL

Press CTRL+O to save, it will prompt you to save to /etc/sudoers.tmp. Do not save to the tmp file or it will not change anything for you. Remove ‘.tmp’ from the end and then save to /etc/sudoers. Press CTRL+X to exit.

There is no restart needed after this change. Your very next sudo command should get impact of this.

But wait ! You may still get the same error as happened to me and you will end up banging your head against a wall near you.

Don’t worry, if its not working then it means that you have not added the correct user in sudoers file. This is quite possible that jekins is executing commands with a different user. In my case there was no user with name ‘jenkins’ at all. I then console logged the user within jenkins to actually find which user is trying to execute my shell commands from within jenkins. Simply add a build step in jenkins “Execute shell” and execute the following command:-

whoami

In the console output of your next build run, you will find the output of the above command which will reveal the hidden user sitting within jenkins and executing commands. In my case it was ‘tomcat’. So I simply replaced the above solution line with the following in sudoers file:-

tomcat ALL=(ALL) NOPASSWD: ALL

Hurrahhh !! the issue was solved.

1 thought on “Jenkins sudo: no tty present and no askpass program specified with NOPASSWD”

  1. You should specify exactly which commands Jenkins can use in the sudoers file instead of specifying ALL.
    Otherwise, anyone with access to modify a job can add a command to do anything on your system.

Leave a Reply

Your email address will not be published. Required fields are marked *