Simple command-line script for managing entries in /etc/hosts on macOS.
- Add hostnames to 127.0.0.1
- Remove hostnames
- List existing entries
- Color-coded output
- Designed specifically for macOS
-
Save the script to a local path:
mkdir -p $HOME/bin cp addhost.sh $HOME/bin/ chmod +x $HOME/bin/addhost.sh
-
Add this alias to your shell config file (
~/.bash_profile,~/.zshrc, etc.):alias addhost="$HOME/bin/addhost.sh"
-
Reload your shell:
source ~/.bash_profile # or source ~/.zshrc
This script modifies your /etc/hosts file. Use it at your own risk.
Always back up your existing hosts file before using:
sudo cp /etc/hosts /etc/hosts.backup.$(date +%s)addhost add <hostname> # Add a hostname to 127.0.0.1
addhost rm <hostname> # Remove a hostname
addhost list # List all hostnames
addhost help # Show usage info
addhost ver # Show versionaddhost add test.local
addhost list
addhost rm test.local- Script modifies
/etc/hosts, sosudois required for write operations. - Only works on macOS (uses
sed -i ""syntax). - You should back up your hosts file if you're using this in a production-like setup.
Łukasz 'Alien' Kosma
https://pear.pl
https://github.com/pearpl
If this script saved you time, you can buy me a coffee.