8.5 KiB
Host Generator Manager
A small Bash utility to manage local
/etc/hostsentries using an "available / enabled" pattern, local alias management and automatic regeneration with backups.
Table of contents
- What this does
- Quick start / prerequisites
- Repository / folder layout
- How it works — concepts
- Script usage & examples
- Creating a host file
- Local aliases
- Regenerating
/etc/hosts& backups - Implementation notes
- Troubleshooting
- Contributing & License
What this does
generator-hosts.sh helps you manage /etc/hosts entries by keeping individual host definitions in an hosts-available/ directory and enabling them by creating symlinks in hosts-enabled/. The script can regenerate /etc/hosts from enabled entries, keeps automatic backups, and supports a small set of "local aliases" (a simple list of alternate names that are added to the 127.0.0.1 line).
This approach is similar to how some services manage configuration (sites-available / sites-enabled) and is convenient for toggling different host groups on/off without manually editing /etc/hosts.
Quick start / prerequisites
- Tested on systems with Bash (script uses Bash features like arrays and
shopt). - You must run as root (the script writes
/etc/hosts). - Place the script and supporting folders under a single root, the default is:
/opt/hosts_config/
- Make the script executable:
chmod +x /opt/hosts_config/generator-hosts.sh
- Run it as
rootor withsudo:
sudo /opt/hosts_config/generator-hosts.sh -l
Repository / folder layout
By default the script expects the following layout (variables at the top of the script):
/opt/hosts_config/
├─ backups/ # automatic backups of /etc/hosts (hosts.bkp.TIMESTAMP)
├─ hosts-available/ # .conf host definition files (one per host group)
├─ hosts-enabled/ # symlinks to hosts-available to enable them
├─ hosts-main/ # optional: main.conf and endfile.conf (inserted before/after entries)
├─ localhost-aliases/ # .lhost files (first line = alias to add to 127.0.0.1)
└─ generator-hosts.sh # the script
Important variables (top of
generator-hosts.sh) — can be adjusted if you keep the config elsewhere:
ROOTFOLD— root folder of the configuration (default/opt/hosts_config).BKPFOLD— backups directory.AVFOLD— hosts-available directory.ENFOLD— hosts-enabled directory.MAINFOLD— hosts-main directory (containsmain.confandendfile.conf).ALIASFOLD— local aliases directory.
How it works — concepts
- Hosts available: Single-file
.confentries stored inhosts-available/. Each file typically contains a line like:
192.168.1.10 example.local example.com
-
Hosts enabled: Enabling a host creates a symlink in
hosts-enabled/pointing to the available file. The regeneration reads enabled files in order and appends their contents to/etc/hosts. -
Local aliases: Files
*.lhostinsidelocalhost-aliases/are read; the first line of each file is appended as a name to the127.0.0.1line (so you can addmyapp.local,app.test, etc.). 0.lhost is used for the baselocalhostentry and the script prevents removing0.lhost. -
Main and end files: Optional
main.conf(inserted after the127.0.0.1line) andendfile.conf(appended at the end) live inhosts-main/. -
Backups: Before writing
/etc/hoststhe script copies the current/etc/hoststobackups/hosts.bkp.TIMESTAMP.
Script usage & examples
Run the script with one of the supported options (example path adjusted to where the script lives):
# List enabled / disabled hosts
sudo /opt/hosts_config/generator-hosts.sh -l
# Enable a host (interactive index selection)
sudo /opt/hosts_config/generator-hosts.sh -e
# Disable a host (interactive index selection)
sudo /opt/hosts_config/generator-hosts.sh -d
# Create a new host file interactively
sudo /opt/hosts_config/generator-hosts.sh -c
# Regenerate /etc/hosts (writes file and creates backups)
sudo /opt/hosts_config/generator-hosts.sh -r
# Print a simulation / preview of what would be written
sudo /opt/hosts_config/generator-hosts.sh -s
# List local aliases
sudo /opt/hosts_config/generator-hosts.sh -hl
# Add a local alias
sudo /opt/hosts_config/generator-hosts.sh -ha
# Remove a local alias (cannot remove 0.lhost)
sudo /opt/hosts_config/generator-hosts.sh -hr
# Show main.conf and endfile.conf
sudo /opt/hosts_config/generator-hosts.sh -sw
Non-interactive / scripting notes
The script is primarily interactive (it reads user input when enabling/disabling or creating entries). If you plan to script interactions, consider calling the underlying file operations yourself (for example creating .conf files in hosts-available/ and symlinking into hosts-enabled/), then call the -r option to regenerate /etc/hosts.
Creating a host file (format)
Each host file in hosts-available/ is a simple textual .conf file. The script writes files created with the -c option in the following single-line format:
<IP><TAB><space-separated-domains>
Examples:
192.168.1.10 myapp.local myapp.example.com
127.0.0.1 local.test another.local
The script will use the filename (without .conf) as a fallback name if no domains were added during interactive creation.
Local aliases
Place small .lhost files inside localhost-aliases/. The script reads the first line of each file and appends those names to the 127.0.0.1 hosts entry. Example:
# file: localhost-aliases/1.lhost
myproject.local
When regenerating, the first (base) line for loopback becomes:
127.0.0.1 localhost myproject.local anotheralias
The script prevents deleting 0.lhost (index 0) via the remove operation because that file is treated as the system localhost line.
Regenerating /etc/hosts & backups
-rorregenerate_hostswill:- Build a temporary hosts file from
127.0.0.1line +hosts-main/main.conf(if present) + enabled hosts +hosts-main/endfile.conf. - Copy the current
/etc/hostsintobackups/hosts.bkp.<TIMESTAMP>. - Move the generated temporary file to
/etc/hostsand setchmod 644.
- Build a temporary hosts file from
Backups are stored in the backups/ directory under the configured ROOTFOLD.
Implementation notes & internals
- The script uses arrays and
shopt -s nullglobfor safe globbing. refresh_arrays()re-populates all arrays used by the interactive commands (available/enabled/aliases).- The
create_host()function validates filename characters and IP string presence but does not validate IP syntax strictly — if you want strict validation you may add an IP regex check or useipcalc. - Host enable/disable operations are implemented by creating and removing symlinks inside
hosts-enabled/.
Troubleshooting
-
/etc/hosts not updated: Ensure you run the script as root. Check that backups exist under
backups/and inspect the temporary file created by_build_hosts_fileusing the-s(simulate) option. -
Host file not applied: Check the enabled symlink exists in
hosts-enabled/and points to the correcthosts-available/<name>.conffile. -
Aliases not appearing: Ensure each
.lhostfile contains the alias on the first line. Run-sto preview results. -
Permissions:
/etc/hostsis set to644after writing; adjust if your environment requires different permissions.
Contributing
If you want to add features or fix bugs:
- Fork the repository (or copy the folder).
- Make changes and test carefully — the script writes system
hostsso test in a safe environment or VM. - Send pull requests or patches with a clear description of the change.
Suggested improvements:
- Add strict IP validation when creating host files.
- Add a non-interactive mode for bulk enabling/disabling.
- Add an option to preview diffs between current
/etc/hostsand the generated file prior to writing.
License
This README and the accompanying generator-hosts.sh are provided under the MIT license.
If you'd like, I can also:
- generate a compact
CONTRIBUTING.mdorCHANGELOG.md, - add inline comments inside
generator-hosts.shto explain each function, - or convert this README to Italian.