Protect your privacy using Peerguardian in Ubuntu
PeerGuardian is a free, open source, IP address blocking software programs capable of blocking incoming and outgoing addresses. The application uses a blocklist of IP addresses to filter the computers of several organisations. The system is also capable of blocking advertising, spyware and government upon user preferences.

Installation
Go to sourceforge.net and download the file peerguardnf-1.5beta.i386.deb. Install it using
sudo dpkg -i peerguardnf-1.5beta.i386.deb
Now, create the directory /etc/peerguardian. This is where the list of blocked hosts will be stored
sudo mkdir /etc/peerguardian
Next, open the file /usr/local/bin/peerguardian.sh with your favorite text editor
sudo vim /usr/local/bin/peerguardian.sh
Paste the following code into the file and save it:
# version for bluetack.co.uk lists!
#!/bin/sh
# Update new blocklists and start/stop/restart PeerGuardian
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# testdescription
#
#CONFIGURATION
# Make sure PG_ETC points to the directory where
# you want to put your downloaded blocklists.
PG_ETC=/etc/peerguardian/
# Remove the lists you don't want to download and
# use from BLOCKLISTS.
BLOCKLISTS="level1"
PG_CONF=/etc/PG.conf
PG_LOG=/var/log/PG.log
PG_LIST=/etc/p2p.p2b.p2p
#The URL where the blocklists reside
URL=http://www.bluetack.co.uk/config
#The format of the lists to download
SUFFIX=gz
#The format after unpacking
SUFFIX2=txt
endscript () {
date +"------------ "%F" "%X" "%Z" End PeerGuardian Script"
exit $1
}
date +"------------ "%F" "%X" "%Z" Begin PeerGuardian $1"
case "$1" in
'start')
cd "$PG_ETC"
# check if blockfiles were updated:
UPDATED=""
for i in $BLOCKLISTS ; do
TIMESTAMP=0
if [ -e $i.$SUFFIX ] ; then
TIMESTAMP=`stat --format=%y $i.$SUFFIX`
echo "File $i.$SUFFIX last updated $TIMESTAMP"
TIMESTAMP=`stat --format=%Y $i.$SUFFIX`
fi
wget -N $URL/$i.$SUFFIX
if [ `stat --format=%Y $i.$SUFFIX` -gt $TIMESTAMP ] ; then
UPDATED=$i
fi
done
# if none of the blockfiles were updated:
if [ -z $UPDATED ] ; then
echo "No blocklists needed updating."
echo "Starting PeerGuardian"
mv $PG_LOG $PG_LOG.backup
peerguardnf -h -m -d -c "$PG_CONF" -l "$PG_LOG"
endscript 0
fi
# if any blockfiles were updated:
for i in $BLOCKLISTS ; do
gunzip -c $i.$SUFFIX > $i.$SUFFIX2
BLOCKLISTSCAT="$BLOCKLISTSCAT $i.$SUFFIX2"
done
cat $BLOCKLISTSCAT | peerguardnf -f merged.p2b.p2p
for i in $BLOCKLISTS ; do
rm $i.$SUFFIX2
done
# uncomment below to unblock Yahoo! Mail and whatever
# else needs unblocking here. Do this also in the
# restart section.
grep -v -i "yahoo\!" merged.p2b.p2p | grep -v -i "Microsoft" | grep -v "Google" > merged.p2b.p2p.tmp
mv merged.p2b.p2p.tmp merged.p2b.p2p
mv $PG_LIST $PG_LIST.backup
mv merged.p2b.p2p $PG_LIST
mv $PG_LOG $PG_LOG.backup
echo "Starting PeerGuardian"
peerguardnf -h -m -d -c "$PG_CONF" -l "$PG_LOG"
endscript 0
;;
'stop')
echo "Stopping PeerGuardian"
killall peerguardnf > /dev/null 2>&1
endscript 0
;;
'restart')
cd "$PG_ETC"
# check if blockfiles were updated:
UPDATED=""
for i in $BLOCKLISTS ; do
TIMESTAMP=0
if [ -e $i.$SUFFIX ] ; then
TIMESTAMP=`stat --format=%y $i.$SUFFIX`
echo "File $i.$SUFFIX last updated $TIMESTAMP"
TIMESTAMP=`stat --format=%Y $i.$SUFFIX`
fi
wget -N $URL/$i.$SUFFIX
if [ `stat --format=%Y $i.$SUFFIX` -gt $TIMESTAMP ] ; then
UPDATED=$i
fi
done
# if none of the blockfiles were updated:
if [ -z $UPDATED ] ; then
echo "No blocklists needed updating."
echo "Stopping PeerGuardian"
killall peerguardnf > /dev/null 2>&1
mv $PG_LOG $PG_LOG.backup
sleep 4
echo "Starting PeerGuardian"
peerguardnf -h -m -d -c "$PG_CONF" -l "$PG_LOG"
endscript 0
fi
# if any blockfiles were updated:
for i in $BLOCKLISTS ; do
gunzip -c $i.$SUFFIX > $i.$SUFFIX2
BLOCKLISTSCAT="$BLOCKLISTSCAT $i.$SUFFIX2"
done
cat $BLOCKLISTSCAT | peerguardnf -f merged.p2b.p2p
for i in $BLOCKLISTS ; do
rm $i.$SUFFIX2
done
# uncomment below to unblock Yahoo! Mail and whatever
# else needs unblocking here. Do this also in the
# restart section.
grep -v -i "yahoo\!" merged.p2b.p2p | grep -v -i "Microsoft" | grep -v "Google" > merged.p2b.p2p.tmp
mv merged.p2b.p2p.tmp merged.p2b.p2p
echo "Stopping PeerGuardian"
killall peerguardnf > /dev/null 2>&1
mv $PG_LIST $PG_LIST.backup
mv merged.p2b.p2p $PG_LIST
mv $PG_LOG $PG_LOG.backup
sleep 4
echo "Starting PeerGuardian"
peerguardnf -h -m -d -c "$PG_CONF" -l "$PG_LOG"
endscript 0
;;
*)
echo "Usage: $0 { start | stop | restart }"
;;
esac
exit 0
Now, make it executable by running
sudo chmod -c 755 /usr/local/bin/peerguardian.sh
Using Peerguardian
To start Peerguardian run
sudo peerguardian.sh start
Stopping it goes as
sudo peerguardian.sh stop
To update the blocking lists run
sudo peerguardian.sh restart
Updating blocklists automatically
Add the following line to the file /etc/crontab
0 13 * * * root peerguardian.sh restart
Monitoring
You can monitor Peerguardian by inspecting the log file /var/log/PG.log
watch tail /var/log/PG.log
References
[1] This post on ubuntuforums.org




Nice article! A small error in the first command – it should be dpkg and not dpki. For this to work on a Ubuntu 8.04 setup I needed to install the package libstdc++5 and create the /var/log/PG.log before I started the script.