This Gist provides a script that updates host aliases in the /etc/hosts file based on comments following the pattern # alias ${sourcehostname}=${targethostname}. It ensures that entries for the specified source hostnames are updated with the correct IP addresses for their corresponding target hostnames.
- Automatically updates
/etc/hostsbased on alias comments. - Resolves IP addresses using DNS if not found in
/etc/hosts. - Supports a dry-run option to preview changes before applying them.
To install the script and the associated systemd service and timer, follow these steps:
-
Download the Script and Systemd Files
Use
curlorwgetto download the script and the systemd service/timer files from the latest release of this Gist to a temporary location. Run the following commands:curl -L -o /tmp/update-host-aliases.sh https://gist.githubusercontent.com/aslafy-z/527ecafc0e77188448b61749a9ad4b87/raw/update-host-aliases.sh sudo mv /tmp/update-host-aliases.sh /usr/local/bin/ sudo chmod +x /usr/local/bin/update-host-aliases.sh curl -L -o /tmp/update-host-aliases.service https://gist.githubusercontent.com/aslafy-z/527ecafc0e77188448b61749a9ad4b87/raw/update-host-aliases.service sudo mv /tmp/update-host-aliases.service /etc/systemd/system/ curl -L -o /tmp/update-host-aliases.timer https://gist.githubusercontent.com/aslafy-z/527ecafc0e77188448b61749a9ad4b87/raw/update-host-aliases.timer sudo mv /tmp/update-host-aliases.timer /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl enable update-host-aliases.timer sudo systemctl start update-host-aliases.timer
You can run the script directly or allow the timer to handle execution. To run the script manually, use:
sudo /usr/local/bin/update-host-aliases.shTo preview changes without applying them, use the --dry-run option:
sudo /usr/local/bin/update-host-aliases.sh --dry-runThis will display the differences between the current /etc/hosts file and the updated version, allowing you to review what changes would be made.
If you find any issues or want to suggest improvements, feel free to open a comment.