Skip to content

Instantly share code, notes, and snippets.

@Ceesaxp
Forked from Eliastik/update-hosts.sh
Created April 23, 2021 10:56
Show Gist options
  • Select an option

  • Save Ceesaxp/d39546311b3f7e4f7ab75bc91d9171ec to your computer and use it in GitHub Desktop.

Select an option

Save Ceesaxp/d39546311b3f7e4f7ab75bc91d9171ec to your computer and use it in GitHub Desktop.
A quick shell script that will automatically update a Linux HOSTS file to block domains (ads, malwares, ...). Support multiple hosts sources, initial host file and incorrect/malicious entries checking.
#!/usr/bin/env sh
# Filname: update-hosts.sh
# Author: George Lesica <george@lesica.com>
# Description: Replaces the HOSTS file with a customized version that blocks
# domains that serve ads and malicious software, creating a backup of the old
# file.
HOSTS_URL="http://someonewhocares.org/hosts/zero/hosts"
NEW_HOSTS="hosts"
HOSTS_PATH="/etc/hosts"
# Check for root
if [ "$(id -u)" -ne "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Grab hosts file
wget -O $NEW_HOSTS $HOSTS_URL
# Backup old hosts file
cp -v $HOSTS_PATH ${HOSTS_PATH}.bak$(date -u +%s)
cp -v $NEW_HOSTS $HOSTS_PATH
# Clean up old downloads
rm $NEW_HOSTS*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment