StevenBlack / hosts

🔒 Consolidating and extending hosts files from several well-curated sources. Optionally pick extensions for porn, social media, and other categories.
MIT License
27.01k stars 2.24k forks source link

Bash script to daily update hosts on your Debian/Ubuntu machine (add this file into /etc/cron.daily/) #2357

Open jfoclpf opened 1 year ago

jfoclpf commented 1 year ago

What do you think? Am I missing something?

#!/usr/bin/env bash

# Simple cron job to update host file with porn block list
# /etc/cron.daily/hosts_update

FILE=/etc/hosts

# create empty file if not exists
if [ ! -f $FILE ]
then
  touch $FILE
fi

# remove immutability
chattr -i $FILE

# This will replace /etc/hosts
docker run --pull always --rm -it -v $FILE:/etc/hosts \
ghcr.io/stevenblack/hosts:latest updateHostsFile.py --auto \
--replace --extensions gambling porn &> /dev/null

# make the file immutable, i.e., protect anyone even root from altering it
chattr +i $FILE

# flush dns cache
service network-manager restart
StevenBlack commented 1 year ago

Hi João @jfoclpf this is pretty cool.

If it were me, I might curl https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling-porn/hosts directly. Here's why.

With each new release, I audit the diffs from all our curated sources. Sometimes I'll catch something, and abort the release, and advise the curator of the problem or issue arising.

A blind pull followed by python3 updateHostsFile.py is probably ok, until it's not.

That's just a risk to be aware of...

jfoclpf commented 1 year ago

Great tip @StevenBlack I didn't know you had already these combined files, then I use now this, no need to use docker and it is much faster

#!/usr/bin/env bash

# Simple cron job to update host file with spam block list
# /etc/cron.daily/hosts_update

FILE=/etc/hosts

# create empty file if not exists
if [ ! -f $FILE ]
then
  touch $FILE
fi

# remove immutability
chattr -i $FILE

# This will replace /etc/hosts
wget https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling-porn/hosts -q -O $FILE

# make the file immutable, i.e., protect anyone even sudo from altering it
chattr +i $FILE

# flush dns cache
service network-manager restart