Skip to content

Instantly share code, notes, and snippets.

@zeugor
Forked from glesica/update-hosts.sh
Created August 17, 2019 18:12
Show Gist options
  • Select an option

  • Save zeugor/a6ad0eb83af59e03d52d2968e1c80e92 to your computer and use it in GitHub Desktop.

Select an option

Save zeugor/a6ad0eb83af59e03d52d2968e1c80e92 to your computer and use it in GitHub Desktop.
A quick shell script that will automatically update a Linux HOSTS file to block ads and malicious domains.
#!/usr/bin/env sh
# Filename: 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