5

I have a EC2 instance running. How can I run commands with sudo through Jenkins? When I try sudo touch /home/ec2-user/foo.bar, I get the following error: sudo: no tty present and no askpass program specified.

What am I doing wrong?

whirlwin
  • 183
  • 1
  • 1
  • 5
  • A complete guess (I know nothing about Jenkins) - check for `Defaults requiretty` in sudoers (using visudo) and comment it out (probably bad security practise though, but it might help narrow down your problem). – cyberx86 Feb 14 '12 at 16:04
  • @cyberex86 I should have mentioned I already did that, that's why I get the error above. Having `requiretty` yields `sudo: sorry, you must have a tty to run sudo`. – whirlwin Feb 14 '12 at 16:16
  • Does your user have `NOPASSWD` set (I believe that is the default for Amazon's Linux AMI) and did you set `visiblepw`? – cyberx86 Feb 14 '12 at 16:34
  • To bank off @cyberx86, you should be able to run (on the Jenkins box) as `sudo -u jenkins touch /home/ec2-user/foo.bar` without having to enter a password. – Andrew M. Feb 14 '12 at 16:56
  • 1
    Thanks the both of you! I used the solution by @cyberx86 (I'll accept if you post an answer), in addition I had to create the user `jenkins`. – whirlwin Feb 14 '12 at 21:18

1 Answers1

9

By default sudo cannot be used without a TTY. In order to do so:

  • Disable 'requiretty' in sudoers (using visudo)
    • This should amount to commenting out 'Defaults requiretty' (using visudo)

  • Ensure that your user is able to login without entering a password:
    • Set 'NOPASSWD' in sudoers
    • Create the user if the user does not exist

  • Set visiblepw - this will allow sudo to work, even if the password entered is displayed
    • (required in some cases when echo cannot be disabled).
cyberx86
  • 20,805
  • 1
  • 62
  • 81
  • What do you mean by using visudo? – Dejel Nov 27 '14 at 11:29
  • 1
    @Odelya: visudo is the program used to edit the sudoers file. It is similar to vi(m), however, it performs some checks to catch invalid entries. http://www.sudo.ws/visudo.man.html – cyberx86 Nov 27 '14 at 21:27