Skip to content

Instantly share code, notes, and snippets.

@aslafy-z
Last active October 3, 2024 10:44
Show Gist options
  • Select an option

  • Save aslafy-z/527ecafc0e77188448b61749a9ad4b87 to your computer and use it in GitHub Desktop.

Select an option

Save aslafy-z/527ecafc0e77188448b61749a9ad4b87 to your computer and use it in GitHub Desktop.

update-host-aliases

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.

Features

  • Automatically updates /etc/hosts based 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.

Installation Steps

To install the script and the associated systemd service and timer, follow these steps:

  1. Download the Script and Systemd Files

    Use curl or wget to 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

Usage

Running the Script

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

Dry Run Option

To preview changes without applying them, use the --dry-run option:

sudo /usr/local/bin/update-host-aliases.sh --dry-run

This will display the differences between the current /etc/hosts file and the updated version, allowing you to review what changes would be made.

Contributing

If you find any issues or want to suggest improvements, feel free to open a comment.

[Unit]
Description=Update host aliases based on alias comments in /etc/hosts
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/update-host-aliases.sh
#!/bin/bash
HOSTFILE="/etc/hosts"
TEMPFILE=$(mktemp)
DIG_TIMEOUT=1
# Create a new temp file to build updated host file
cp $HOSTFILE $TEMPFILE
# Loop through each line of the host file
while read -r line; do
# Check if the line is an alias comment
if [[ $line =~ \#\ alias\ ([^=]+)=([^[:space:]]+) ]]; then
source_hostname="${BASH_REMATCH[1]}"
target_hostname="${BASH_REMATCH[2]}"
# Find the IP of the target hostname
target_ip=$(grep -Ew "[0-9]+.[0-9]+.[0-9]+.[0-9]+ $target_hostname" "$TEMPFILE" | head -n 1 | awk '{print $1}')
if [ -z "$target_ip" ]; then
# If target_hostname is not in /etc/hosts, resolve it using DNS
target_ip=$(dig +short "$target_hostname" -4 +time=$DIG_TIMEOUT | head -n 1)
fi
if [ -n "$target_ip" ]; then
sed -i "/# alias $source_hostname=/d" $TEMPFILE
sed -i "/$source_hostname # alias/d" $TEMPFILE
echo "# alias $source_hostname=$target_hostname" >> $TEMPFILE
echo "$target_ip $source_hostname # alias" >> $TEMPFILE
fi
fi
done < "$HOSTFILE"
if [ -n "$1" ]; then
# Dry run
diff -u "$HOSTFILE" "$TEMPFILE"
rm $TEMPFILE
exit 0
fi
if ! cmp -s $HOSTFILE $TEMPFILE; then
# Replace the old host file with the updated one
mv $TEMPFILE $HOSTFILE
chmod 644 $HOSTFILE
else
# No changes detected, remove the temp file
rm $TEMPFILE
fi
[Unit]
Description=Run update-host-aliases script every 10 seconds
[Timer]
OnBootSec=10sec
OnUnitActiveSec=10sec
Persistent=true
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment