A reminder on how to use a host file.


For the security aware among you.

HOWTO: Amend your hosts file to block ads (by site).
Please take note of comments 2 and 8 (the latter especially if you like me are using Lucid Lynx) *Yeah for LTS* 😀

Reason for this post? I want to make it easier (for myself amongst things) to grab scripts I’ve used previously, just in case I ever need to reinstall my box and in the unlikely event I may have forgotten to back up my stuff. >-)

Related links to further indulge you in your learning:
Ubuntu Security
Stricter defaults

This is what I use because I have a touchpad and a wacom tablet (pressumably?) depending on it.

# This will mount /dev/shm writable, but without permission to execute programs and without permission to change the UID of running programs.
tmpfs     /dev/shm     tmpfs     defaults,noexec,nosuid     0     0

How to secure Firefox

Aside from me using AdBlock Plus:
(My subscriptions)

Overkill you say? Maybe but it is imo always better to be on the safe side.

I prefer a hosts file as it protects more then just Firefox.

[start of me bitching]
I agree! Furthermore if advertisers, marketeers and the likes want to make a profit of Internet users (this of course includes me) by selling people’s ‘surf habits’ amongst things. I am of the opinion that they: one should have to ask us first for permission to profile us and two compensate us for our ‘labor’, but hey that’s just my opinion. 😉 Of course I can’t speak on behalf of other people, though I am quite certain I am not alone in this.
[/start of me bitching]

Due to the nature of this script requiring elevated privileges aka root/admin privileges, I am going to say that this is for “informational purposes only” especially because it concerns your ‘inevitable’ usage/curiosity. 😉 I.e. you have been properly warned (see the reason for this post for my view.).

Anyway the original script can be found here http://hostsfile.mine.nu/downloads/updatehosts.sh.txt because I’ve to give credit/respect to those who deserve to be credited/respected!

You can easily view the source, copy the code to the clipboard or print the code by hovering over the script in the box below (this does require javascript, mind you :lol:). If copied to clipboard; then Press: Alt+F2 –> In the appearing box type: gedit (or insert any other editor here) Updatehosts.sh and choose paste (Ctrl+v) and save; else save as with a name of your choosing); done (fi)

#!/bin/bash
# Hosts file updater
# Originally written by Andy Short circa 2002
# You will probably need root or su rights to access /etc/hosts
# modified by killermist with (what seem to be) some logical tweaks
# 20090712 alfredo (1) Added checks for required applications;
#                  (2) put 'footprint' text into the modified hosts file.
# Debian users will need the package "sysutils" as this script uses dos2unix
# $ sudo apt-get install sysutils
#
## Invoke as sudo ./Updatehosts.sh
YELLOW="\033[1;33m"
RED="\033[0;31m"
ENDCOLOR="\033[0m"
# Root check.
# Kindly ripped from the '71529-ubucleaner.sh' script I once obtained from?
if [ $USER != root ]; then
  echo -e $RED"Error: only root can do this, hence the Capital first letter of this script."
  echo -e $YELLOW"Either cd into the path where this script is kept..."$ENDCOLOR
  echo -e $YELLOW"Then run:  sudo ./Updatehosts.sh"$ENDCOLOR  
  echo -e $YELLOW"Or: sudo -s (where I assume this script is in YOUR path)"$ENDCOLOR  
  echo -e $YELLOW"Invoke it as you normally would"$ENDCOLOR  
  echo -e $YELLOW"invoke a command at the prompt (e.g. wget http://somesite)"$ENDCOLOR  
  echo -e $YELLOW"That would be the one you're at.  ; - )"$ENDCOLOR    
  echo -e $YELLOW"Exiting..."$ENDCOLOR
  exit 0
fi
# http://ubuntuforums.org/showthread.php?t=241460#2
# Comment 2: Re: HOWTO: Amend your hosts file to block ads (by site)
# Hint: 0.0.0.0 = nowhere
HFSERVER="http://hostsfile.mine.nu.nyud.net"
HFILE="hosts.zip"
ORIGFILE="/etc/hosts.original"

clear
echo "-------------------------------------------------------------"
echo "This script will update your Hosts file to the latest version"
echo "Your original Hosts file will be renamed to $ORIGFILE"
echo "-------------------------------------------------------------"
echo ""

echo "Checking for required applications..."; ABORT=0
echo ... wget    ; builtin type -P wget     &>/dev/null || { echo "wget is missing."; ABORT=1; }
echo ... unzip   ; builtin type -P unzip    &>/dev/null || { echo "unzip is missing."; ABORT=1; }
# http://ubuntuforums.org/showpost.php?p=9153699&postcount=8
# Comment 8: Re: HOWTO: Amend your hosts file to block ads (by site)
# On Ubuntu Lucid Lynx 10.04 the script is currently broken because of the following changes to the tofrodos package.
## Commented out due to recurring error.  Manual conversion works\!
## Just as commands used in this scripts simply will work 'automagically' now.  
#echo ... dos2unix; builtin type -P dos2unix &>/dev/null || { echo "tofrodos (which contains dos2unix) is missing."; ABORT=1; }
echo ... grep    ; builtin type -P grep     &>/dev/null || { echo "grep is missing."; ABORT=1; }
# this is just to test the test!
#echo ... crap     ; builtin type -P crap     &>/dev/null || { echo "crap is missing."; ABORT=1; }
#echo ABORT is $ABORT
if [ $ABORT != 0 ] ; then 
	echo One or more required applications are missing. Aborting now ...
	exit 1
fi
echo "OK"
echo ""

if [ ! -f "$ORIGFILE" ] ; then
  echo "Backing up your previous hosts file.." 
  cp -v /etc/hosts $ORIGFILE # I like verbose file operations.  Can be less verbose if necessary.
fi

echo "Retrieving $HFILE from $HFSERVER"
echo ""
wget -O /tmp/$HFILE $HFSERVER/$HFILE
unzip -p /tmp/$HFILE >  | /usr/bin/fromdos -d /tmp/hosts
if [ 'grep -c "banner" /tmp/hosts' ];then 
    echo "Downloaded and unpacked $HFILE OK"
    echo "Appending host list to original content"  # which was probably there for a reason, like to make sure localhost worked, and possibly even more stuff if part of a corporate LAN
    #cp -f -u /tmp/hosts /etc/hosts
    cat $ORIGFILE  >/etc/hosts
    echo "" >>/etc/hosts # to make sure the original file ends in a new-line so that 2 entries don't end up on the same line, either causing unexpected behavior or not working at all
    echo "#============================================================="   >>/etc/hosts
    echo "# This hosts file has been modified by the script:"               >>/etc/hosts
    echo "#     $0"                                                         >>/etc/hosts
    echo "# with a list of blocked sites obtained from $HFSERVER"           >>/etc/hosts
    echo "# from the original file now saved as $ORIGFILE"                  >>/etc/hosts
    echo "# As a side-effect of this script, any changes you wish to make"  >>/etc/hosts
    echo "# persistent in the hosts file should be made to $ORIGFILE"       >>/etc/hosts
    echo "# because /etc/hosts will be respawned from that file and the"    >>/etc/hosts
    echo "# new list from the server each time this script runs."           >>/etc/hosts
    echo "#============================================================="   >>/etc/hosts
    cat /tmp/hosts >>/etc/hosts
    rm -fv /tmp/hosts* # again, I like verbose file operations.  I like to know what my system is doing.
    echo "Update process complete"
    #echo "-------------------------------------------------------------"
    echo "As a side-effect of this script, any changes you wish to make"
    echo "persistent in the hosts file should be made to $ORIGFILE"
    echo "because /etc/hosts will be respawned from that file and the "
    echo "new list from the server each time this script runs."
    exit
else
    echo "Update failed"
fi
exit 0

For what it is worth: I ran this script no more than a month ago.

127.0.0.1	localhost
127.0.1.1	server.engage.net	server
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

#=============================================================
# This hosts file has been modified by the script:
#     ./Updatehosts.sh
# with a list of blocked sites obtained from http://hostsfile.mine.nu.nyud.net
# from the original file now saved as /etc/hosts.original
# As a side-effect of this script, any changes you wish to make
# persistent in the hosts file should be made to /etc/hosts.original
# because /etc/hosts will be respawned from that file and the
# new list from the server each time this script runs.
#=============================================================
#####################################################################
# The Hosts File Project http://hostsfile.mine.nu
# Global Advert Servers Blocklist - Personal Edition
#####################################################################
# Release 2010-7-23
# Servers Verified as up and running 2010-1-26 (by dns exploration)
# Updated sorted and maintained by Andrew Short (sh0rtie)
# Contact: wduk10@hotmail.com
#####################################################################
etc etc etc...

PS:
As for the syntax highlighting:
Posting source code