Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packer build fails due to tty needed for sudo

My packer build is failing with the following message:

sudo: sorry, you must have a tty to run sudo.

My host is Windows 8 with vagrant and virtualbox, my guest is centos7. On researching it is my understanding that not requiring tty for sudo is the reason for the message. But I have the following in ks.cfg:

sed -i 's/^.*requiretty/#Defaults requiretty/' /etc/sudoers

Could the issue be that there's something I need to set on the windows vagrant ssh side so that a psuedo-tty is created?

This is my first go at packer.

I am using a packer build that I downloaded.

packer.json below:

{
  "variables": {
    "version": "{{env `VERSION`}}"
  },
  "provisioners": [
    {
      "type": "shell",
      "execute_command": "sudo {{.Vars}} sh {{.Path}}",
      "scripts": [
        "scripts/vagrant.sh",
        "scripts/vmtools.sh",
        "scripts/cleanup.sh",
        "scripts/zerodisk.sh"
      ]
    }
  ],
  "post-processors": [
    {
      "type": "vagrant",
      "output": "INSANEWORKS-CentOS-7.0-x86_64-{{user `version`}}-{{.Provider}}.box"
    }
  ],
  "builders": [
    {
      "type": "virtualbox-iso",
      "iso_url": "http://ftp.iij.ad.jp/pub/linux/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1503.iso",
      "iso_checksum": "498bb78789ddc7973fe14358822eb1b48521bbaca91c17bd132c7f8c903d79b3",
      "iso_checksum_type": "sha256",
      "ssh_username": "vagrant",
      "ssh_password": "vagrant",
      "ssh_wait_timeout": "45m",
      "ssh_disable_agent": "true",
      "boot_command": [
        "<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg<enter><wait>"
      ],
      "disk_size": "40000",
      "hard_drive_interface": "sata",
      "guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",
      "guest_additions_sha256": "7b61f523db7ba75aebc4c7bb0cae2da92674fa72299e4a006c5c67517f7d786b",
      "guest_os_type": "RedHat_64",
      "headless": "true",
      "http_directory": "http",
      "shutdown_command": "sudo /sbin/halt -p",
      "vboxmanage": [
        [ "modifyvm", "{{.Name}}", "--memory", "1024" ],
        [ "modifyvm", "{{.Name}}", "--cpus", "1" ]
      ]
    }
  ]
}

Thanks in advance.

like image 464
Harris.Atlarge Avatar asked Aug 03 '15 13:08

Harris.Atlarge


People also ask

Should Sudo commands run from a TTY?

Requiring sudo commands to run from a real tty was generally done as a matter of security concern. In reality, this does not provide any real security benefit. This is generally enforced by setting Defaults requiretty in the /etc/sudoers.

Why do I keep getting Sudo-sorry you must have TTY?

Another scenario could be when you are trying to run some sudo command on remote host which requires a password to run that command but there is no terminal assigned so it will again throw "sudo: Sorry, you must have a tty to run sudo" error as shown below.

What is Sudo no TTY present and no ASKPASS program specified?

Check my post on this other common error: sudo: no tty present and no askpass program specified. What are sudo and tty? sudo is a command line programm that allow users to run programms with the security privileges of another user or group which default to the superuser (i.e. root ).

What is requiretty in Sudo config file?

The requiretty if set in sudo config file sudoers, will only run when the user is logged in to a real tty. When this flag is set, sudo can only run from a login session and not via other means such as cron, shell/perl/python, or cgi-bin scripts. Eventually, save and close the file.


1 Answers

You have to enable a PTY in your ssh connection. Add in your builders section following configuration item:

"ssh_pty" : "true"

See also https://packer.io/docs/templates/communicator.html#ssh_pty

Your "execute_command" in provisioner section should be "execute_command" : "echo 'vagrant' | {{ .Vars }} sudo -E -S sh '{{ .Path }}'"

like image 159
Sandra Parsick Avatar answered Oct 20 '22 06:10

Sandra Parsick