0
votes

I am running below provisioner part in terraform while creating instance. But its not working, showing permission denied error.

provisioner "remote-exec" {
    inline = [
      "sudo cat /etc/ssh/sshd_config | sed -e 's/#MaxStartups 10:30:60/MaxStartups 10:30:60/' >/etc/ssh/sshd_config1",
      "sudo mv -f /etc/ssh/sshd_config1 /etc/ssh/sshd_config"
    ]
  }

or

provisioner "file" {
    source      = "script.sh"
    destination = "/tmp/script.sh"
  }

I am getting below error:

Error :cannot create /etc/ssh/sshd_config: Permission denied or Permission denied

2
the error clearly says you dont have permissions to edit sshd_config file on your local box - 4c74356b41

2 Answers

0
votes

When you use the Terraform provisioner remote-exec to change something in the file /etc/ssh/ssh_config which needs the root permission, the command sudo is interactive that you need to input the password to get the root permission, but the provisioner is not. So you cannot input the password in the provisioner so that it shows you do not have the permission.

The Terraform provisioner just likes running a script inside the VM and it does not support the interactive actions as I know. If you want to do the interactive activities with the VM, just use the SSH connection without Terraform.

Update

Use SSH command to connect into the VM:

ssh user@vm_ip

Then inside the VM you can execute the command as you want.

0
votes

If you want to stick with terraform you could run the remote-exec as root user if root login is allowed until this step - then using sudo would be obsolete.

Another approach could be using the sudo NOPASSWD option (read more).