{"record":{"id":"f126ac4b2f3ea5a9","repo":"abiosoft/colima","slug":"error-backing-up-kubeconfig-w","errorCode":null,"errorMessage":"error backing up kubeconfig: %w","messagePattern":"error backing up kubeconfig: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"environment/container/kubernetes/kubeconfig.go","lineNumber":91,"sourceCode":"\t\thost := c.host.WithEnv(envVar)\n\n\t\t// get merged config\n\t\tkubeconfig, err := host.RunOutput(\"kubectl\", \"config\", \"view\", \"--raw\")\n\t\tif err != nil {\n\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}","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/abiosoft/colima/blob/c3a5f9184d83a197184f897a9f07eb3c01b3bc88/environment/container/kubernetes/kubeconfig.go#L73-L109","documentation":"Emitted during kubeconfig persistence: an existing kubeconfig file was detected at kubeconfFile (the first entry of $KUBECONFIG, else ~/.kube/config) and the host-level backup copy ('cp <kubeconfFile> <dir>/config-bak-<unix-time>') failed. Colima backs up your existing kubeconfig before overwriting it with the merged k3s one; a failed backup aborts the chain so your existing config is never clobbered without a safety copy.","triggerScenarios":"~/.kube (or the dir containing the temp profile dir, i.e. ~/.kube/.<profile>/) is read-only or owned by another user (running colima under sudo writes earlier steps as root, leaving root-owned files); KUBECONFIG points to a path in a directory you cannot write to; disk full; the resolved directory was removed between the mkdir step and the cp; macOS SIP/managed corporate machines protecting the path.","commonSituations":"Previously ran colima (or kubectl) with sudo so ~/.kube or ~/.kube/.colima is root-owned; KUBECONFIG set to a file inside a mounted/read-only location; home directory quota exhausted; multiple profiles where an earlier partial run left a stale, unwritable temp dir.","solutions":["Check ownership and permissions: 'ls -la ~/.kube ~/.kube/.colima*' and fix with 'sudo chown -R $(id -u):$(id -g) ~/.kube' if a previous sudo run owned it.","Ensure the directory of $KUBECONFIG (if set) is writable: 'touch \"$(dirname \"${KUBECONFIG%%:*}\")/.wtest\" && rm ...'.","Free disk space ('df -h ~') if the volume is full.","Unset or correct a bogus KUBECONFIG ('echo $KUBECONFIG') and re-run 'colima start --kubernetes' — on success the previous config-bak-* files in ~/.kube/.<profile>/ hold any prior backups."],"exampleFix":"# before\n$ sudo colima start --kubernetes   # later cp fails: ~/.kube/.colima owned by root\n# after\n$ sudo chown -R \"$(id -u):$(id -g)\" ~/.kube\n$ colima start --kubernetes","handlingStrategy":"try-catch","validationCode":"// ensure the backup directory is writable before starting kubernetes\ninfo, err := os.Stat(filepath.Join(hostHome, \".kube\"))\nif err == nil && info.Mode().Perm()&0200 == 0 {\n    return fmt.Errorf(\"~/.kube is not writable; fix permissions first\")\n}","typeGuard":null,"tryCatchPattern":"if err := c.host.Run(\"cp\", kubeconfFile, backup); err != nil {\n    // do not abort the whole start for a backup failure; proceed without backup but warn\n    log.Warnln(fmt.Errorf(\"error backing up kubeconfig: %w\", err))\n}","preventionTips":["Never run colima/kubectl under sudo; it leaves root-owned files in ~/.kube.","Periodically 'ls -l ~/.kube' and chown strays back to your user.","Keep KUBECONFIG entries in directories you own."],"tags":["kubeconfig","kubernetes","permissions","backup","host"],"backgroundTag":null,"analyzedSha":"c3a5f9184d83a197184f897a9f07eb3c01b3bc88","analyzedAt":"2026-08-15T18:58:08.334Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}