{"record":{"id":"0de76daeebf0d413","repo":"abiosoft/colima","slug":"error-updating-kubeconfig-w","errorCode":null,"errorMessage":"error updating kubeconfig: %w","messagePattern":"error updating kubeconfig: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"environment/container/kubernetes/kubeconfig.go","lineNumber":96,"sourceCode":"\t\t\treturn err\n\t\t}\n\n\t\t// save\n\t\treturn host.Write(tmpkubeconfFile, []byte(kubeconfig))\n\t})\n\n\t// backup current settings and save new config\n\ta.Add(func() error {\n\t\t// backup existing file if exists\n\t\tif stat, err := c.host.Stat(kubeconfFile); err == nil && !stat.IsDir() {\n\t\t\tbackup := filepath.Join(filepath.Dir(tmpkubeconfFile), fmt.Sprintf(\"config-bak-%d\", time.Now().Unix()))\n\t\t\tif err := c.host.Run(\"cp\", kubeconfFile, backup); err != nil {\n\t\t\t\treturn fmt.Errorf(\"error backing up kubeconfig: %w\", err)\n\t\t\t}\n\t\t}\n\t\t// save new config\n\t\tif err := c.host.Run(\"cp\", tmpkubeconfFile, kubeconfFile); err != nil {\n\t\t\treturn fmt.Errorf(\"error updating kubeconfig: %w\", err)\n\t\t}\n\n\t\treturn nil\n\t})\n\n\t// set new context\n\tconf, _ := ctx.Value(config.CtxKey()).(config.Config)\n\tif conf.AutoActivate() {\n\t\ta.Add(func() error {\n\t\t\tout, err := c.host.RunOutput(\"kubectl\", \"config\", \"use-context\", profile)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tlog.Println(out)\n\t\t\treturn nil\n\t\t})\n\t}\n","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/abiosoft/colima/blob/c3a5f9184d83a197184f897a9f07eb3c01b3bc88/environment/container/kubernetes/kubeconfig.go#L78-L114","documentation":"Raised at the final kubeconfig install step: 'cp <tmpkubeconfFile> <kubeconfFile>' on the host failed, i.e. moving the prepared temp kubeconfig (~/.kube/.<profile>/colima-temp) into place at $KUBECONFIG-first-entry or ~/.kube/config. The backup step (if any) already succeeded; only the final overwrite failed.","triggerScenarios":"Target kubeconfig path is read-only (chmod 444, or on a read-only mount); the file is root-owned from earlier sudo usage; KUBECONFIG's first entry lives in a non-writable directory; target file is immutable (chflags on macOS); antivirus/file-lock (corporate endpoint software) blocking writes; the temp file was deleted between steps.","commonSituations":"Ran kubectl/colima as root previously so ~/.kube/config is root-owned; KUBECONFIG points into a synced/protected folder (OneDrive, Dropbox, managed device); read-only home in hardened CI environments.","solutions":["Fix ownership/permissions of the target: 'sudo chown \"$(id -u)\" \"$KUBECONFIG\"' (or ~/.kube/config) and ensure it is writable ('chmod u+w').","Verify the first KUBECONFIG entry is a writable location ('echo \"$KUBECONFIG\"'; on failure use only writable paths, colon-separated lists are split via filepath.SplitList and only the first is used).","Clear file immutability on macOS: 'sudo chflags nouchg ~/.kube/config' if 'ls -lO' shows uchg.","Temporarily unset KUBECONFIG to let colima write the default ~/.kube/config, then merge manually."],"exampleFix":"# before\n$ ls -l ~/.kube/config\n-rw-r--r--  1 root  staff  1234  Jan 1 00:00 /Users/me/.kube/config\n# after\n$ sudo chown \"$(id -u):$(id -g)\" ~/.kube/config\n$ colima start --kubernetes","handlingStrategy":"validation","validationCode":"// verify the target kubeconfig path is writable before the chain runs\nif fi, err := os.Stat(kubeconfFile); err == nil {\n    if fi.Mode().Perm()&0222 == 0 {\n        return fmt.Errorf(\"%s is read-only; chmod u+w before continuing\", kubeconfFile)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := c.host.Run(\"cp\", tmpkubeconfFile, kubeconfFile); err != nil {\n    if strings.Contains(err.Error(), \"Permission denied\") {\n        return fmt.Errorf(\"error updating kubeconfig (fix ownership of %s): %w\", kubeconfFile, err)\n    }\n    return fmt.Errorf(\"error updating kubeconfig: %w\", err)\n}","preventionTips":["Check 'ls -l ${KUBECONFIG:-~/.kube/config}' ownership after any sudo usage.","Avoid pointing KUBECONFIG at synced/managed folders.","Keep ~/.kube on the native home volume, not a read-only mount."],"tags":["kubeconfig","kubernetes","permissions","host","file-write"],"backgroundTag":null,"analyzedSha":"c3a5f9184d83a197184f897a9f07eb3c01b3bc88","analyzedAt":"2026-08-15T18:58:08.334Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}