Go library and CLI utility for /etc/hosts management.
Documentation | Go Reference | Releases
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.
Features:
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.
When installing with Go please use the latest stable Go release. At least go1.24 or greater is required.
To install use: go install github.com/txn2/txeh/txeh@master
If you are building from a local source clone, use go install ./txeh from the top-level directory of the clone.
go install will typically put the txeh binary inside the bin directory under go env GOPATH, see Go’s Compile and install packages and dependencies for more on this. You may need to add that directory to your $PATH if you encounter the error txeh: command not found after installation, you can find a guide for adding a directory to your PATH at https://gist.github.com/nex3/c395b2f8fd4b02068be37c961301caa7#file-path-md.
dependencies are vendored:
go run ./txeh/txeh.goThe txeh CLI application allows command line or scripted access to /etc/hosts file modification.
Example CLI Usage:
_ _
| |___ _____| |__
| __\ \/ / _ \ '_ \
| |_ > 0=explicit)
-q, --quiet no output
-r, --read string (override) Path to read /etc/hosts file.
-w, --write string (override) Path to write /etc/hosts file.…txeh supports inline comments on host entries, useful for tracking which tool or purpose added specific entries (e.g., docker, development environments).
Comments use the standard hosts file format: IP HOSTNAME [HOSTNAME...] # comment
Key behavior:
…// Add hosts with a comment
hosts.AddHostWithComment("127.0.0.1", "myapp", "local development")
hosts.AddHostsWithComment("127.0.0.1", []string{"svc1", "svc2"}, "my project services")
// Add hosts without a comment (original behavior)
hosts.AddHost("127.0.0.1", "myhost")
hosts.AddHosts("127.0.0.1", []string{"a", "b", "c"})Starting hosts file:
127.0.0.1 localhost
127.0.0.1 app1 app2 # dev services| Command | Result |
|---|---|
txeh add 127.0.0.1 app3 -c "dev services" |
app3 added to "dev services" line |
txeh add 127.0.0.1 api -c "staging env" |
New line: 127.0.0.1 api # staging env |
txeh add 127.0.0.1 test |
test added to localhost line (no comment) |
List all hosts with a specific comment:
sudo txeh list bycomment "dev services"Remove all entries with a specific comment (removes entire lines):
sudo txeh remove bycomment "dev services"Comments are never modified on existing lines. To change a comment, remove the hosts first and re-add them with the new comment:
# Remove all entries with the old comment
sudo txeh remove bycomment "old comment"
# Re-add with the new comment
sudo txeh add 127.0.0.1 app1 app2 --comment "new comment"Dependency:
go get github.com/txn2/txehExample Golang Implementation:
…Build test release:
goreleaser --skip-publish --clean --skip-validateBuild and release:
GITHUB_TOKEN=$GITHUB_TOKEN goreleaser --cleanApache License 2.0
Open source by Craig Johnston, sponsored by Deasil Works, Inc.
No open issues yet, or sync has not completed.