Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to solve "sudo: no tty present and no askpass program specified" when trying to run a shell from Jenkins
Running shell scripts that have contain sudo commands in them from jenkins might not run as expected. To fix this, follow along
Simple steps:
1. On ubuntu based systems, run " $ sudo visudo "
2. this will open /etc/sudoers file.
3. If your jenkins user is already in that file, then modify to look like this:
jenkins ALL=(ALL) NOPASSWD: ALL
4. save the file by doing Ctrl+O (dont save in tmp file. save in /etc/sudoers, confirm overwrite)
5. Exit by doing Ctrl+X
6. Relaunch your jenkins job
7. you shouldnt see that error message again :)
@saadqc

This comment has been minimized.

Copy link

@saadqc saadqc commented Mar 31, 2016

Thank you! it worked

@dphov

This comment has been minimized.

Copy link

@dphov dphov commented Apr 5, 2017

Thanks a lot!

@jdhillwan

This comment has been minimized.

Copy link

@jdhillwan jdhillwan commented Jun 28, 2017

Thanks!! Worked for me.

@nskforward

This comment has been minimized.

Copy link

@nskforward nskforward commented Jul 30, 2017

It doesn't work in case we SSH connect to server via PHP script. Any ideas?
$ssh->exec("sudo /usr/sbin/nginx -s reload"); // --> sudo: no tty present and no askpass program specified

@bakasmarius

This comment has been minimized.

Copy link

@bakasmarius bakasmarius commented Oct 10, 2017

@nskforward: I'm just guessing here, but try "-S" instead of "-s"

@perguth

This comment has been minimized.

Copy link

@perguth perguth commented Oct 26, 2017

I thinks it's better to grant Jenkins sudo access only for the script that requires it. Eg.:

jenkins ALL=NOPASSWD:/some-path/some-script
@cmpDrone

This comment has been minimized.

Copy link

@cmpDrone cmpDrone commented Dec 13, 2017

whatever the solution worked superb

@0x962

This comment has been minimized.

Copy link

@0x962 0x962 commented Dec 15, 2017

Thank god i found this. :)

@imranhussain293

This comment has been minimized.

Copy link

@imranhussain293 imranhussain293 commented Feb 6, 2018

Thanks! This was very helpful :)

@rwalus

This comment has been minimized.

Copy link

@rwalus rwalus commented Mar 25, 2018

TY! :)

@miqueiasdesouza

This comment has been minimized.

Copy link

@miqueiasdesouza miqueiasdesouza commented Apr 18, 2018

Thanks!

@rashnil-git

This comment has been minimized.

Copy link

@rashnil-git rashnil-git commented May 16, 2018

I was trying to run a shell script that executes a command on remote machine from a spark batch and was getting the above error. For now, the above helped by setting this for hdfs user. Thanks!

@Aranjan21

This comment has been minimized.

Copy link

@Aranjan21 Aranjan21 commented Jun 3, 2018

It worked for me as well

@carmen12

This comment has been minimized.

Copy link

@carmen12 carmen12 commented Jun 12, 2018

Thanks a lot!

@sujataw

This comment has been minimized.

Copy link

@sujataw sujataw commented Sep 3, 2018

Not working in my case, I am executing shell script from Jenkins server.

@sujataw

This comment has been minimized.

Copy link

@sujataw sujataw commented Sep 5, 2018

  • sudo python /home/engineqa/Desktop/LinuxAutomation/execute_test.py
    sudo: no tty present and no askpass program specified
    Build step 'Execute shell' marked build as failure
    Finished: FAILURE

Showing error like that

@NasrSyncBac

This comment has been minimized.

Copy link

@NasrSyncBac NasrSyncBac commented Sep 17, 2018

Thanks!

@UQBuild

This comment has been minimized.

Copy link

@UQBuild UQBuild commented Sep 20, 2018

Not working in my case, I am executing shell script from Jenkins server.

@sovat

This comment has been minimized.

Copy link

@sovat sovat commented Nov 14, 2018

Thanks, it really works!

@AbhiMatcha

This comment has been minimized.

Copy link

@AbhiMatcha AbhiMatcha commented Feb 23, 2019

  • sudo python /home/engineqa/Desktop/LinuxAutomation/execute_test.py
    sudo: no tty present and no askpass program specified
    Build step 'Execute shell' marked build as failure
    Finished: FAILURE

Showing error like that

Can resolve this error. When you run Jenkins build user will be Jenkins so just run command sudo vim /etc/sudoers and mention %jenkins ALL=(ALL) NOPASSWD:ALL and save.

Hope this will work for you :)

@NET-057

This comment has been minimized.

Copy link

@NET-057 NET-057 commented Mar 19, 2019

Thanks it's working... 👍

@tranvanthuc

This comment has been minimized.

Copy link

@tranvanthuc tranvanthuc commented Mar 29, 2019

Thanks it's running ... 💯

@paritalamahi

This comment has been minimized.

Copy link

@paritalamahi paritalamahi commented Apr 1, 2019

Hi, i am running a jmeter script from jenkins aws box.I need to write a report file to jmeter folder.It is pulling issue file not found exception.Even though i gave all access .

@Estefycp

This comment has been minimized.

Copy link

@Estefycp Estefycp commented Aug 13, 2019

thanks!

@BBrown4

This comment has been minimized.

Copy link

@BBrown4 BBrown4 commented Apr 5, 2020

Thanks so much!

@imneonizer

This comment has been minimized.

Copy link

@imneonizer imneonizer commented Apr 8, 2021

In response to the question asked here:
https://stackoverflow.com/questions/25189348/unable-to-provide-password-to-a-process-with-subprocess-python

On linux you can bypass sudo prompt using:

echo <password> | sudo -S whoami

Pass the formatted command to subprocess and you are good to go:

import subprocess as sp

cmd = "whoami"
password= "<password>"

stdout, stderr = sp.Popen("echo {} | sudo -S {}".format(password, cmd), shell=True, stdout=sp.PIPE, stderr=sp.PIPE).communicate()

print(stdout.decode())

if stderr:
    print("error occured:")
    print(stderr.decode())

It should print root as command is executed by sudo user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment