Skip to content
Go library and CLI utilty for /etc/hosts management.
Branch: master
Clone or download
Latest commit 4482b2f Apr 5, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
util Added empty path checking func and adjusted some of the root.go code Apr 5, 2019
vendor/ added deps, updated docs Feb 16, 2019
.gitignore housekeeping Feb 16, 2019
LICENSE Initial commit Feb 15, 2019 Update Feb 17, 2019
glide.lock added deps, updated docs Feb 16, 2019
go.mod updated cli dep Apr 5, 2019
go.sum lib v1.1.1 Apr 5, 2019
hosts.go fixed newline split, windows support Feb 16, 2019
txeh.png txeh command line utility Feb 16, 2019

txeh - /etc/hosts mangement

Etc Hosts Management Utility & Go Library

Go Report Card GoDoc

/etc/hosts Management

It is easy to open your /etc/hosts file in text editor and add or remove entries. However, if you make heavy use of /etc/hosts for software development or DevOps purposes, it can sometimes be difficult to automate and validate large numbers of host entries.

txeh was initially built as a golang library to support kubefwd, a Kubernetes port-forwarding utility utilizing /etc/hosts heavily, to associate custom hostnames with multiple local loopback IP addresses and remove these entries when it terminates.

A computer's /etc/hosts file is a powerful utility for developers and system administrators to create localized, custom DNS entries. This small go library and utility were developed to encapsulate the complexity of working with /etc/hosts directly by providing a simple interface for adding and removing entries in a /etc/hosts file.

txeh Utility


MacOS homebrew users can brew install txn2/tap/txeh, otherwise see releases for packages and binaries for a number of distros and architectures including Windows, Linux and Arm based systems.

Complie and run from source (dependencies are vendored):

go run ./util/txeh.go


The txeh CLI application allows command line or scripted access to /etc/hosts file modification.

Example CLI Usage:

 _            _
| |___  _____| |__
| __\ \/ / _ \ '_ \
| |_ >  <  __/ | | |
 \__/_/\_\___|_| |_| v1.1.0

Add, remove and re-associate hostname entries in your /etc/hosts file.
Read more including useage as a Go library at

  txeh [flags]
  txeh [command]

Available Commands:
  add         Add hostnames to /etc/hosts
  help        Help about any command
  remove      Remove a hostname or ip address
  version     Print the version number of txeh

  -d, --dryrun         dry run, output to stdout (ignores quiet)
  -h, --help           help for txeh
  -q, --quiet          no output
  -r, --read string    (override) Path to read /etc/hosts file.
  -w, --write string   (override) Path to write /etc/hosts file.
# point the hostnames "test" and "test.two" to the local loopback
sudo txeh add test test.two

# remove the hostname "test"
sudo txeh remove host test

# remove multiple hostnames 
sudo txeh remove host test test2 test.two

# remove an IP address and all the hosts that point to it
sudo txeh remove ip

# remove multiple IP addresses
sudo txeh remove ip

# remove CIDR ranges
sudo txeh remove cidr

# quiet mode will suppress output
sudo txeh remove ip -q

# dry run will print a rendered /etc/hosts with your changes without
# saving it.
sudo txeh remove ip -d

# use quiet mode and dry-run to direct the rendered /etc/hosts file
# to another file
sudo txeh add -q -d > hosts.test

# specify an alternate /etc/hosts file to read. writing will
# default to the specified read path.
txeh add -q -r ./hosts.test

# specify a seperate read and write oath
txeh add -r ./hosts.test -w ./hosts.test2

txeh Go Library


go get

Example Golang Implementation:

package main

import (


func main() {
    hosts, err := txeh.NewHostsDefault()
    if err != nil {

    hosts.AddHost("", "test")
    hosts.AddHost("", "logstash")
    hosts.AddHosts("", []string{"a", "b", "c"})
    hosts.RemoveHosts([]string{"example", "example.machine", ""})
    hosts.RemoveHosts(strings.Fields("example2 example.machine2 example.machine.example.com2"))

    removeList := []string{
    hfData := hosts.RenderHostsFile()

    // if you like to see what the outcome will
    // look like
    // or hosts.SaveAs("./test.hosts")

Build Release

Build test release:

goreleaser --skip-publish --rm-dist --skip-validate

Build and release:

GITHUB_TOKEN=$GITHUB_TOKEN goreleaser --rm-dist


Apache License 2.0

You can’t perform that action at this time.