A modern, safe, and efficient command-line tool for managing /etc/hosts files. Built in Go with safety features like atomic writes, automatic backups, and file locking.
- 🔒 Safe Operations: Atomic writes with automatic backups
- 🔐 File Locking: Prevents corruption from concurrent access
- ✅ Validation: RFC-compliant IP address and hostname validation
- 📦 Import/Export: JSON and YAML support for profile management
- 🎯 Flexible Operations: Add, remove, enable/disable entries
- 🧪 Testing-Friendly: Custom hosts file support for development
- 🐳 Docker Ready: Containerized execution support
git clone https://github.com/vaxvhbe/hostsctl.git
cd hostsctl
make build
sudo make installgo install github.com/vaxvhbe/hostsctl/cmd/hostsctl@latesthostsctl is available as a Nix flake for easy installation and reproducible builds:
# Install from GitHub
nix profile install github:vaxvhbe/hostsctl
# Install from local directory
nix profile install .
# Run without installing
nix run github:vaxvhbe/hostsctl -- --help
# Development environment
nix developdocker build -t hostsctl .
docker run --rm -v /etc/hosts:/etc/hosts hostsctl list# List current entries
sudo hostsctl list
# Add a new entry
sudo hostsctl add --ip 127.0.0.1 --name myapp.local --comment "Local development"
# Disable an entry
sudo hostsctl disable --name myapp.local
# Create a backup
sudo hostsctl backup
# Verify hosts file integrity
sudo hostsctl verify# Show active entries only
sudo hostsctl list
# Show all entries including disabled ones
sudo hostsctl list --all
# JSON output for scripting
sudo hostsctl list --json# Add single hostname
sudo hostsctl add --ip 192.168.1.100 --name server.local
# Add multiple hostnames
sudo hostsctl add --ip 192.168.1.100 --name server.local --name api.local
# Add with comment
sudo hostsctl add --ip 127.0.0.1 --name app.local --comment "Development server"# Remove by hostname
sudo hostsctl rm --name server.local
# Remove by ID
sudo hostsctl rm --id 5# Disable entry (comments it out)
sudo hostsctl disable --name server.local
# Enable entry (uncomments it)
sudo hostsctl enable --name server.local
# Use ID instead of name
sudo hostsctl enable --id 3# Create timestamped backup
sudo hostsctl backup
# Create backup to specific location
sudo hostsctl backup --out /tmp/hosts.backup
# Restore from backup
sudo hostsctl restore --file /etc/hosts.hostsctl.20240101-120000.bak# Export current entries to JSON
sudo hostsctl export --file development.json --format json
# Import entries from YAML
sudo hostsctl import --file production.yaml --format yaml
# Export to YAML
sudo hostsctl export --file current.yaml --format yaml# Check for syntax errors and duplicates
sudo hostsctl verify
# JSON output for automation
sudo hostsctl verify --json--hosts-file PATH: Use custom hosts file (default:/etc/hosts)--json: Output results in JSON format--no-color: Disable colored output
Perfect for development and testing:
# Create a test hosts file
echo "127.0.0.1 localhost" > test_hosts
# Use hostsctl with custom file (no sudo needed)
hostsctl --hosts-file test_hosts list
hostsctl --hosts-file test_hosts add --ip 192.168.1.1 --name test.local
hostsctl --hosts-file test_hosts verifyhostsctl supports importing and exporting groups of hosts entries as profiles:
{
"name": "development",
"description": "Development environment hosts",
"entries": [
{
"ip": "127.0.0.1",
"names": ["api.local", "app.local"],
"comment": "Local services",
"disabled": false
}
]
}name: production
description: Production environment hosts
entries:
- ip: "10.0.1.50"
names:
- api.prod.company.com
- api-v1.prod.company.com
comment: "Production API"
disabled: falseBefore any modification, hostsctl automatically creates a timestamped backup:
/etc/hosts.hostsctl.20240101-120000.bak
All file writes use atomic operations (write to temp file + rename) to prevent corruption.
Concurrent access is prevented using file locking mechanisms.
- IPv4 and IPv6 address validation
- RFC-compliant hostname validation
- Duplicate detection
- Syntax verification
- Go 1.21 or later
- Make (optional, for build automation)
- Nix (optional, for reproducible builds)
# Clone repository
git clone https://github.com/vaxvhbe/hostsctl.git
cd hostsctl
# Install dependencies
make deps
# Build binary
make build
# Run tests
make test
# Run all checks (format, vet, lint, test)
make checkFor reproducible builds and development environments:
# Clone repository
git clone https://github.com/vaxvhbe/hostsctl.git
cd hostsctl
# Enter development shell with all tools
nix develop
# Build with Nix
make nix-build
# or directly: nix build
# Run tests
make test
# Install locally
make nix-installThe Nix flake provides:
- Reproducible builds across different systems
- Development shell with Go, golangci-lint, and all tools
- Cross-platform support (Linux, macOS, x86_64, ARM64)
- Isolated environment with pinned dependencies
# Run unit tests
make test
# Run tests with coverage
make test-coverage
# Create test hosts file for development
make test-hostshostsctl/
├── cmd/hostsctl/ # CLI entrypoint
├── internal/
│ ├── hosts/ # Core hosts file operations
│ ├── cli/ # CLI command implementations
│ └── lock/ # File locking utilities
├── pkg/ # Public utilities (validation)
├── configs/ # Example profiles
├── test/ # Unit tests
├── Makefile # Build automation
├── Dockerfile # Container support
└── README.md
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Run tests (
make check) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Please report security vulnerabilities to security@example.com.
- Always run with minimum required privileges
- Use
--hosts-filefor testing instead of modifying system files - Regularly backup your hosts file
- Verify imports from untrusted sources
hostsctl requires root privileges only when modifying /etc/hosts. You can use custom hosts files without sudo for development.
Yes! Use --json output and check exit codes:
if hostsctl verify --json; then
echo "Hosts file is valid"
else
echo "Hosts file has issues"
fihostsctl automatically creates backups before any modification. Find them with:
ls /etc/hosts.hostsctl.*.bak
sudo hostsctl restore --file /etc/hosts.hostsctl.TIMESTAMP.bakYes, use the --hosts-file flag:
hostsctl --hosts-file /path/to/custom/hosts list- IPv6 support improvements
- Remote profile management (Git-backed)
- Daemon mode with REST API
- Integration with common development tools
- Advanced filtering and search capabilities
Made with ❤️ by the hostsctl team