{"record":{"id":"4bfd84c9380f9027","repo":"hashicorp/terraform","slug":"error-chmodding-script-file-to-0777-in-remote-mach-4bfd84","errorCode":null,"errorMessage":"Error chmodding script file to 0777 in remote machine %v: %s %s","messagePattern":"Error chmodding script file to 0777 in remote machine (.+?): (.+?) (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/communicator/ssh/communicator.go","lineNumber":477,"sourceCode":"\n\tif err := c.Upload(path, &script); err != nil {\n\t\treturn err\n\t}\n\tif c.connInfo.TargetPlatform != TargetPlatformWindows {\n\t\tvar stdout, stderr bytes.Buffer\n\t\tcmd := &remote.Cmd{\n\t\t\tCommand: fmt.Sprintf(\"chmod 0777 %s\", path),\n\t\t\tStdout:  &stdout,\n\t\t\tStderr:  &stderr,\n\t\t}\n\t\tif err := c.Start(cmd); err != nil {\n\t\t\treturn fmt.Errorf(\n\t\t\t\t\"Error chmodding script file to 0777 in remote \"+\n\t\t\t\t\t\"machine: %s\", err)\n\t\t}\n\n\t\tif err := cmd.Wait(); err != nil {\n\t\t\treturn fmt.Errorf(\n\t\t\t\t\"Error chmodding script file to 0777 in remote \"+\n\t\t\t\t\t\"machine %v: %s %s\", err, stdout.String(), stderr.String())\n\t\t}\n\t}\n\treturn nil\n}\n\n// UploadDir implementation of communicator.Communicator interface\nfunc (c *Communicator) UploadDir(dst string, src string) error {\n\tlog.Printf(\"[DEBUG] Uploading dir '%s' to '%s'\", src, dst)\n\tscpFunc := func(w io.Writer, r *bufio.Reader) error {\n\t\tuploadEntries := func() error {\n\t\t\tf, err := os.Open(src)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tdefer f.Close()\n","sourceCodeStart":459,"sourceCodeEnd":495,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/communicator/ssh/communicator.go#L459-L495","documentation":"Returned when the remote chmod 0777 command ran but cmd.Wait() reported a failure (non-zero exit), including captured stdout and stderr for diagnosis. This means the SSH exec succeeded but the chmod itself was rejected by the remote shell — e.g. the uploaded path does not exist, the user lacks permission, or the path contains characters the shell interprets.","triggerScenarios":"script_path resolves to a directory the SSH user cannot chmod (owned by root, read-only filesystem, noexec mount); the script path contains spaces/shell metacharacters not quoted; SELinux/AppArmor denying mode change; the file was uploaded to a path that differs from the chmod path.","commonSituations":"Default ScriptPath under /tmp on a hardened host that mounts /tmp noexec or with restrictive owner; a custom script_path with spaces; running as a non-root user against a root-owned path.","solutions":["Inspect the embedded stdout/stderr in the message for the remote error (e.g. 'Operation not permitted').","Set script_path to a writable, executable location for the SSH user (e.g. /home/<user>/terraform_<random>.sh).","Ensure the SSH user owns the upload directory or has appropriate sudo rights.","Avoid spaces/special characters in script_path, or move uploads under the user's home."],"exampleFix":"// before\nconnection {\n  host        = aws_instance.web.public_ip\n  user        = \"ubuntu\"\n  script_path = \"/tmp/my script.sh\"\n}\n\n// after\nconnection {\n  host        = aws_instance.web.public_ip\n  user        = \"ubuntu\"\n  script_path = \"/home/ubuntu/tf-run.sh\"\n}","handlingStrategy":"validation","validationCode":"# Before apply, ensure the script_path directory is writable+executable\n# by the SSH user, and avoid spaces in script_path.\n# In HCL: script_path = \"/home/${var.ssh_user}/tf-${random_id.run.hex}.sh\"","typeGuard":null,"tryCatchPattern":"// In Go, parse the embedded stdout/stderr to surface the remote reason:\nif strings.Contains(err.Error(), \"Error chmodding script file to 0777 in remote machine\") {\n    return fmt.Errorf(\"remote chmod failed (check perms/path): %w\", err)\n}","preventionTips":["Use a script_path under the SSH user's home directory.","Avoid spaces and shell metacharacters in script_path.","Confirm the user owns the upload target or has write+chmod rights.","Watch for noexec mounts on /tmp when running scripts."],"tags":["terraform","ssh","chmod","script","exec","permissions","provisioner"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}