{"record":{"id":"f053895bd80fa607","repo":"kubernetes/kops","slug":"error-writing-script-to-ssh-target-w","errorCode":null,"errorMessage":"error writing script to SSH target: %w","messagePattern":"error writing script to SSH target: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/toolbox_enroll.go","lineNumber":370,"sourceCode":"\t\tb := make([]byte, 32)\n\t\tif _, err := cryptorand.Read(b); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error getting random data: %w\", err)\n\t\t}\n\t\ttempDir = path.Join(\"/tmp\", hex.EncodeToString(b))\n\t}\n\n\tscriptPath := path.Join(tempDir, \"script.sh\")\n\n\tp := vfs.NewSSHPath(s.sshClient, s.hostname, scriptPath, s.sudo)\n\n\tdefer func() {\n\t\tif _, err := s.runCommand(ctx, \"rm -rf \"+tempDir, ExecOptions{Echo: false}); err != nil {\n\t\t\tklog.Warningf(\"error cleaning up temp directory %q: %v\", tempDir, err)\n\t\t}\n\t}()\n\n\tif err := p.WriteFile(ctx, bytes.NewReader([]byte(script)), nil); err != nil {\n\t\treturn nil, fmt.Errorf(\"error writing script to SSH target: %w\", err)\n\t}\n\n\tscriptCommand := \"/bin/bash \" + scriptPath\n\treturn s.runCommand(ctx, scriptCommand, options)\n}\n\n// CommandOutput holds the results of running a command.\ntype CommandOutput struct {\n\tStdout bytes.Buffer\n\tStderr bytes.Buffer\n}\n\n// ExecOptions holds options for running a command remotely.\ntype ExecOptions struct {\n\tEcho bool\n}\n\nfunc (s *SSHHost) runCommand(ctx context.Context, command string, options ExecOptions) (*CommandOutput, error) {","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/commands/toolbox_enroll.go#L352-L388","documentation":"runScript writes the shell script to scriptPath on the remote host using a vfs SSH path writer. If p.WriteFile fails — session I/O error, permission denied (root-owned dir), or disk full — this error wraps it; the deferred cleanup removes the temp dir either way.","triggerScenarios":"p.WriteFile(ctx, bytes.NewReader([]byte(script)), nil) fails: SSH session dropped mid-write, /tmp not writable, noexec/no-space /tmp, or sudo wrapping failing so the write cannot create directories under /tmp.","commonSituations":"Remote /tmp mounted noexec or full; enroll run against a host whose SSH user lacks sudo rights to create the temp dir; long-running script staging interrupted by network flaps.","solutions":["Read the wrapped error: 'permission denied' → ensure passwordless sudo for the SSH user (visudo)","Check remote /tmp: df -h /tmp and mount | grep /tmp (watch for noexec/nosuid restrictions)","Verify network stability and retry enroll","Manually test the equivalent: ssh <user>@<host> 'mkdir -p /tmp/test && echo ok > /tmp/test/f'"],"exampleFix":"// before\n$ kops toolbox enroll ...\nError: error writing script to SSH target: mkdir /tmp/abc...: permission denied\n// after\n$ ssh admin@node 'sudo visudo'  # add: admin ALL=(ALL) NOPASSWD:ALL\n$ df -h /tmp                    # ensure space, and /tmp not mounted noexec\n$ kops toolbox enroll ...","handlingStrategy":"validation","validationCode":"if err := exec.Command(\"ssh\", user+\"@\"+host,\n    \"test -w /tmp && [ $(df -k --output=avail /tmp | tail -1) -gt 10240 ]\").Run(); err != nil {\n    return fmt.Errorf(\"remote /tmp not writable or <10MB free\")\n}","typeGuard":"func remoteTmpWritable(ctx context.Context, h *SSHHost) bool {\n    _, err := h.runCommand(ctx, \"test -w /tmp\", ExecOptions{Echo: false})\n    return err == nil\n}","tryCatchPattern":"_, err := sshTarget.runScript(ctx, script, ExecOptions{Echo: true})\nif err != nil {\n    if strings.Contains(err.Error(), \"error writing script to SSH target\") {\n        return fmt.Errorf(\"check remote /tmp permissions and free space: %w\", err)\n    }\n    return err\n}","preventionTips":["Ensure the SSH user has passwordless sudo for staging under /tmp","Verify /tmp is writable and has free space on target nodes","Retry on transient network drops; check mount flags (noexec) if exec fails next"],"tags":["ssh","script-execution","file-write"],"backgroundTag":"ssh-script-upload-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}