Skip to content

Instantly share code, notes, and snippets.

@adrian-martinez-dev
Forked from glesica/update-hosts.sh
Created February 22, 2017 04:33
Show Gist options
  • Select an option

  • Save adrian-martinez-dev/ce2d000507e4169770a20915001999eb to your computer and use it in GitHub Desktop.

Select an option

Save adrian-martinez-dev/ce2d000507e4169770a20915001999eb 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="https://raw.githubusercontent.com/StevenBlack/hosts/master/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