{"record":{"id":"19814140c5aa34af","repo":"hashicorp/terraform","slug":"error-chmodding-script-file-to-0777-in-remote-mach","errorCode":null,"errorMessage":"Error chmodding script file to 0777 in remote machine: %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":471,"sourceCode":"\tvar script bytes.Buffer\n\n\tif string(prefix) != \"#!\" && c.connInfo.TargetPlatform != TargetPlatformWindows {\n\t\tscript.WriteString(DefaultShebang)\n\t}\n\tscript.ReadFrom(reader)\n\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 {","sourceCodeStart":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/communicator/ssh/communicator.go#L453-L489","documentation":"Raised in Communicator.UploadScript when c.Start(cmd) fails for the remote 'chmod 0777 <path>' command that runs after a successful script upload on unix targets. Start opens a new SSH session and begins the command; a failure here means the session could not be established or the command could not be launched (e.g. connection dropped, session allocation failed, or PTY request failed).","triggerScenarios":"After the script file is uploaded via SCP, the communicator starts a remote chmod command. This error fires if newSession() fails (client disconnected or nil), if session.RequestPty() fails, or if session.Start() returns an error for the chmod invocation.","commonSituations":"The SSH connection dropped between the SCP upload and the chmod call (e.g. server-side idle timeout, network blip), the remote sshd has a low MaxSessions limit and the session cannot be allocated, or the script path contains characters that break the remote shell.","solutions":["Check that the SSH connection is stable and not timing out between upload and chmod (increase connection timeout in the provisioner connection block).","Verify the remote sshd MaxSessions and MaxStartups limits are not exhausted.","Ensure the script_path does not contain characters that break the remote shell.","Retry the provisioner run; transient connection drops during long uploads are common."],"exampleFix":"// before\nconnection {\n  host     = var.host\n  timeout  = \"30s\"\n}\n\n// after\nconnection {\n  host     = var.host\n  timeout  = \"10m\"\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Wrap UploadScript with retry on session-start failure\nfunc uploadScriptWithRetry(comm *Communicator, path string, r io.Reader, maxRetries int) error {\n    for i := 0; i < maxRetries; i++ {\n        err := comm.UploadScript(path, r)\n        if err == nil {\n            return nil\n        }\n        if strings.Contains(err.Error(), \"chmodding script file to 0777\") {\n            time.Sleep(time.Duration(i+1) * 2 * time.Second)\n            continue\n        }\n        return err\n    }\n    return errors.New(\"max retries exceeded for script upload\")\n}","preventionTips":["Set a generous connection timeout in the connection block to survive transient drops.","Keep scripts small to minimize the upload window before chmod runs.","Ensure the remote sshd MaxSessions is high enough for concurrent sessions."],"tags":["ssh","chmod","script-upload","session","terraform"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}