bettercap/bettercap · error

error reading hosts from file %s: %v

Error message

error reading hosts from file %s: %v

What it means

Error wrapping in the dns_spoof module's Configure (module startup parameter handling): HostsFromFile failed while parsing the file given via dns.spoof.hosts (missing file, bad syntax, or lines that do not map to the configured address). The original error is wrapped with the file path and returned, aborting module configuration; Configure is invoked by Start.

Source

Thrown at modules/dns_spoof/dns_spoof.go:128

	} else if err, address = mod.IPParam("dns.spoof.address"); err != nil {
		return err
	} else if err, domains = mod.ListParam("dns.spoof.domains"); err != nil {
		return err
	} else if err, hostsFile = mod.StringParam("dns.spoof.hosts"); err != nil {
		return err
	} else if err, ttl = mod.StringParam("dns.spoof.ttl"); err != nil {
		return err
	}

	mod.Hosts = Hosts{}
	for _, domain := range domains {
		mod.Hosts = append(mod.Hosts, NewHostEntry(domain, address))
	}

	if hostsFile != "" {
		mod.Info("loading hosts from file %s ...", hostsFile)
		if err, hosts := HostsFromFile(hostsFile, address); err != nil {
			return fmt.Errorf("error reading hosts from file %s: %v", hostsFile, err)
		} else {
			mod.Hosts = append(mod.Hosts, hosts...)
		}
	}

	if len(mod.Hosts) == 0 {
		return fmt.Errorf("at least dns.spoof.hosts or dns.spoof.domains must be filled")
	}

	for _, entry := range mod.Hosts {
		mod.Info("%s -> %s", entry.Host, entry.Address)
	}

	if !mod.Session.Firewall.IsForwardingEnabled() {
		mod.Info("enabling forwarding.")
		mod.Session.Firewall.EnableForwarding(true)
	}

View on GitHub (pinned to 8eca2820f3)

Solutions

  1. Verify the path in dns.spoof.hosts exists and is readable by the bettercap process
  2. Fix syntax errors in the hosts file (each line must contain a valid hostname/IP mapping)
  3. Check file permissions and, if run as root, that the path is not on an unmounted volume
  4. Test the file with a minimal one-entry hosts file to isolate the failing line
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at modules/dns_spoof/dns_spoof.go:128 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of bettercap/bettercap@8eca2820f3 (2026-09-02). Data as JSON: /api/errors/fa039e449b324e45. Report an issue: GitHub.