Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

txeh

> DevOps
Open source

Go library and CLI utility for /etc/hosts management.

341 stars0 likes4 views
WebsiteGitHub

About

Go library and CLI utility for /etc/hosts management.

Etc Hosts Management Utility & Go Library

Documentation | Go Reference | Releases

/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.

Features:

  • Thread-safe operations (mutex-protected for concurrent use)
  • IPv4 and IPv6 support
  • CIDR range operations for bulk add/remove
  • Preserves comments and file formatting
  • Cross-platform (Linux, macOS, Windows)

txeh Utility

Install

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.

Install with go install

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.

Compile and run from source

dependencies are vendored:

go run ./txeh/txeh.go

Use

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

Example CLI Usage:

bash
 _            _
| |___  _____| |__
| __\ \/ / _ \ '_ \
| |_ >  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.
…

Comments

txeh supports inline comments on host entries, useful for tracking which tool or purpose added specific entries (e.g., docker, development environments).

How Comments Work

Comments use the standard hosts file format: IP HOSTNAME [HOSTNAME...] # comment

Key behavior:

  • Comments are used for grouping: hosts with the same IP AND same comment are placed on the same line
  • Hosts added without a comment only match lines without a comment
  • Hosts added with a comment only match lines with the same comment
  • Comments are never modified on existing lines
  • If no matching line exists, a new line is created

CLI Usage

…

Library Usage

go
// 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"})

Example Scenario

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)

Listing and Removing by Comment

List all hosts with a specific comment:

bash
sudo txeh list bycomment "dev services"

Remove all entries with a specific comment (removes entire lines):

bash
sudo txeh remove bycomment "dev services"

Modifying Comments

Comments are never modified on existing lines. To change a comment, remove the hosts first and re-add them with the new comment:

bash
# 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"

txeh Go Library

Dependency:

bash
go get github.com/txn2/txeh

Example Golang Implementation:

…

Build Release

Build test release:

bash
goreleaser --skip-publish --clean --skip-validate

Build and release:

bash
GITHUB_TOKEN=$GITHUB_TOKEN goreleaser --clean

License

Apache License 2.0


Open source by Craig Johnston, sponsored by Deasil Works, Inc.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Gobinarybrewdeveloper-toolsdevops

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理