Host Generator Manager

Screenshot about the usage of the script in the terminal

Description

A small Bash utility to manage local /etc/hosts entries using an 'available / enabled' pattern, local alias management and automatic regeneration with backups.

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

Quick start / prerequisites

How it works — concepts

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 generator-hosts -l

# Enable a host (interactive index selection)
sudo generator-hosts -e

# Disable a host (interactive index selection)
sudo generator-hosts -d

# Create a new host file interactively
sudo generator-hosts -c

# Regenerate /etc/hosts (writes file and creates backups)
sudo generator-hosts -r

# Print a simulation / preview of what would be written
sudo generator-hosts -s

# List local aliases
sudo generator-hosts -hl

# Add a local alias
sudo generator-hosts -ha

# Remove a local alias (cannot remove 0.lhost)
sudo generator-hosts -hr

# Show main.conf and endfile.conf
sudo generator-hosts -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 | space | 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

-r or regenerate_hosts will:

  1. Build a temporary hosts file from 127.0.0.1 line + hosts-main/main.conf (if present) + enabled hosts + hosts-main/endfile.conf.
  2. Copy the current /etc/hosts into backups/hosts.bkp.###TIMESTAMP.
  3. Move the generated temporary file to /etc/hosts and set chmod 644.

Backups are stored in the backups/ directory under the configured ROOTFOLD.

Implementation notes & internals

Troubleshooting

Contributing

If you want to add features or fix bugs:

Suggested improvements:

License

This README and the accompanying generator-hosts.sh are provided under the MIT license.


Details

Name: Host Generator Manager

Categories: bash, scripts, network

Size: 24KB

Created: 01/11/2025

Updated: 01/11/2025

Autor: 'Simone Cusano'

Author Website: https://sld-server.org

Gitea Repository: Gitea Link