{"record":{"id":"f106691611aba153","repo":"decolua/9router","slug":"wrong-sudo-password-failed-to-add-dns-entry-e","errorCode":null,"errorMessage":"Wrong sudo password | Failed to add DNS entry: ${error.message}","messagePattern":"Wrong sudo password \\| Failed to add DNS entry: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/mitm/dns/dnsConfig.js","lineNumber":178,"sourceCode":"      const trimmed = current.replace(/[\\r\\n\\s]+$/g, \"\");\n      const toAppend = entriesToAdd.map(h => `127.0.0.1 ${h}`).join(\"\\r\\n\");\n      const next = `${trimmed}\\r\\n${toAppend}\\r\\n`;\n      atomicWriteHostsWin(HOSTS_FILE, current, next);\n      await runElevatedPowerShell(\"ipconfig /flushdns | Out-Null\");\n    } else {\n      const current = fs.readFileSync(HOSTS_FILE, \"utf8\");\n      const trimmed = current.replace(/[\\r\\n\\s]+$/g, \"\");\n      const toAppend = entriesToAdd.map(h => `127.0.0.1 ${h}`).join(\"\\n\");\n      const next = `${trimmed}\\n${toAppend}\\n`;\n      // Use tee via sudo to overwrite atomically — escape single quotes in content\n      const escaped = next.replace(/'/g, \"'\\\\''\");\n      await execWithPassword(`printf '%s' '${escaped}' | tee ${HOSTS_FILE} > /dev/null`, sudoPassword);\n      await flushDNS(sudoPassword);\n    }\n    log(`🌐 DNS ${tool}: ✅ added ${entriesToAdd.join(\", \")}`);\n  } catch (error) {\n    const msg = error.message?.includes(\"incorrect password\") ? \"Wrong sudo password\" : `Failed to add DNS entry: ${error.message}`;\n    throw new Error(msg);\n  }\n}\n\n/**\n * Remove DNS entries for a specific tool\n */\nasync function removeDNSEntry(tool, sudoPassword) {\n  const hosts = TOOL_HOSTS[tool];\n  if (!hosts) throw new Error(`Unknown tool: ${tool}`);\n\n  const entriesToRemove = hosts.filter(h => checkDNSEntry(h));\n  if (entriesToRemove.length === 0) {\n    log(`🌐 DNS ${tool}: already inactive`);\n    return;\n  }\n\n  try {\n    if (IS_WIN) {","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/src/mitm/dns/dnsConfig.js#L160-L196","documentation":"After building the new hosts-file content, addDNSEntry() writes it via `tee` through execWithPassword and flushes the DNS cache. On failure it inspects the error: if it mentions 'incorrect password' it throws 'Wrong sudo password', otherwise `Failed to add DNS entry: <detail>`. It normalizes sudo auth failures vs hosts-write/flush failures.","triggerScenarios":"addDNSEntry() when: the supplied sudoPassword is wrong, sudo auth is canceled, the hosts file (HOSTS_FILE) is not writable even with sudo, tee fails, or the DNS flush command fails.","commonSituations":"User typed the wrong sudo password in the prompt or stored password changed after an OS password reset; running in a container without a writable /etc/hosts; macOS/Linux DNS flush command unavailable on the distro; sudo TTY requirement in headless sessions.","solutions":["Re-run with the correct sudo password (verify with `sudo -v`)","Check /etc/hosts is writable by root and not immutable (`lsattr /etc/hosts`)","Verify the DNS flush command exists on this OS/distro","Run from a session where sudo can prompt (TTY available)","Inspect the wrapped error detail after 'Failed to add DNS entry:' for the root cause"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Verify sudo works before touching /etc/hosts\ntry {\n  await execWithPassword('sudo -v', sudoPassword);\n} catch {\n  throw new Error('Sudo password invalid; cannot modify /etc/hosts');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await addDNSEntry(tool, sudoPassword);\n} catch (e) {\n  if (e.message === 'Wrong sudo password') {\n    // re-prompt the user for the password and retry once\n  } else if (e.message.startsWith('Failed to add DNS entry:')) {\n    console.error('hosts write/flush failed:', e.message);\n  } else throw e;\n}","preventionTips":["Validate the sudo password with a cheap `sudo -v` before batch DNS operations","Check /etc/hosts is not immutable (chattr +i) before writing","On headless systems ensure sudo works without interactive TTY","Verify the DNS flush command exists for the current OS"],"tags":["dns","sudo","hosts-file","permissions"],"backgroundTag":"sudo-authentication-failed","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}