Page 1 of 8 123 ... LastLast
Results 1 to 10 of 71

Thread: HOWTO: Amend your hosts file to block ads (by site)

  1. #1
    Join Date
    May 2006
    Beans
    42

    HOWTO: Amend your hosts file to block ads (by site)

    Note: this is very much a beginner's guide.


    Q: What is a HOSTS file / Why might I want to do this?
    A: It's explained nicely at http://www.mvps.org/winhelp2002/hosts.htm


    Q: But why do this in Ubuntu? My Ubuntu is like a shield of steel!
    A: Many reasons. But mainly to save bandwidth by not loading lots of rubbish. For each site listed as 127.0.0.1, content (usually ad banners etc) from that site isn't loaded. In a web browser that picture/frame will 404.

    So, how to do it:
    The host file in Linux lives at /etc/hosts. We are going to add some lines to it.

    Disclaimer:

    ***** NOW HEAR THIS *****

    A) ***BACK UP*** your /etc/hosts BEFORE YOU START

    B) You must APPEND to (not overwrite) the file. You need the stuff that's already in there! That means: paste in the additional lines AFTER what's already in there!

    C) I do not recommend doing this if you HOST WEB PAGES from your PC.


    End of disclaimer.



    1) >>>>>>> BACKUP YOUR HOSTS FILE <<<<<<<<<

    $ sudo cp /etc/hosts /etc/hosts.old

    2) Download a good hosts file. I recommend the MVPs one:

    http://www.mvps.org/winhelp2002/hosts.txt

    3) Open your hosts file.

    $ sudo gedit /etc/hosts

    4) Paste in the contents of hosts.txt into the bottom of /etc/hosts.

    5) It should take effect immediately. Close and reload your web browser then try to go to one of the blocked sites. It should 404.

    Luc

  2. #2
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: HOWTO: Amend your hosts file to block ads (by site)

    Here is a link to a site with a script to update your hosts file:

    hostsfile.mine

    Look on the very bottom of the page for this:

    Unix/Linux bash Hosts updater script (txt) (m)

    Here is the script if you would like to examine the code:

    Hosts update script

    • Download the script
    • Make the script executable (chmod a+x updatehosts.sh)
    • Install tofrodos (sudo apt-get install tofrodos)
    • sudo ./updatehosts.sh


    Optional
    127.0.0.1 = your computer
    0.0.0.0 = nowhere

    "0.0.0.0" is faster as "127.0.0.1" will wait for a timeout


    This command will change 127.0.0.1 to 0.0.0.0 :
    Code:
    sudo sed -i -e 's_127.0.0.1_0.0.0.0_g' /etc/hosts
    WARNING: We now need to manually edit /etc/hosts and change the line(s) at the top with localhost and your hostname back to 127.0.0.1 localhost

    #These may be on one line or multiple lines
    127.0.0.1 localhost
    127.0.0.1 Ubuntu # Change Ubuntu to your hostname
    It is best to do this from the terminal with vim or nano as graphical editors will take forever to load (the hosts file is large).
    Last edited by bodhi.zazen; January 19th, 2008 at 12:20 AM.
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  3. #3
    Join Date
    Oct 2006
    Beans
    9

    Re: HOWTO: Amend your hosts file to block ads (by site)

    After running the script, which ran seemlessly, I don't see any changes to the HOSTS file... which one should it change? I was hoping to do like I did in ******* and cut/paste the loopbacks into the file, but I lost admin rights when I did it. Maybe I made a n00b mistake, I'll try it again. thanks

  4. #4
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: HOWTO: Amend your hosts file to block ads (by site)

    it modifies /etc/hosts

    If you lost admin rights, boot to failsafe.

    If you do not know, to get your host name, at the CLI type:
    Code:
    hostname
    Code:
    nano /etc/hosts
    Add the line
    127.0.0.1 localhost.localdomain <hostname>
    <hostname> = your host name
    At the top fo the file.

    Crtl-X to exit, type Y to save.

    Reboot
    Code:
    reboot
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  5. #5
    Join Date
    Aug 2008
    Beans
    31
    Distro
    Ubuntu 10.04 Lucid Lynx

    Thumbs up Re: HOWTO: Amend your hosts file to block ads (by site)

    Thank you very much.

  6. #6
    Join Date
    Mar 2006
    Location
    Rumplestiltskin, Cal.
    Beans
    Hidden!
    Distro
    Ubuntu 18.04 Bionic Beaver

    Re: HOWTO: Amend your hosts file to block ads (by site)

    Following this post, I read:

    Unix/Linux bash Hosts updater script (txt) (m)

    Here is the script if you would like to examine the code:

    Hosts update script

    * Download the script
    * Make the script executable (chmod a+x updatehosts.sh)
    * Install tofrodos (sudo apt-get install tofrodos)
    * sudo ./updatehosts.sh


    I downloaded the updatehosts.sh.txt

    but, doing: chmod a+x updatehosts.sh -- did not remove the .txt. So there I quit and am asking for clarification:

    Do I rename the file updatehosts.sh.txt manually before or after running chomd a+x?
    AMD FX-6300, 8gig ddr3, MSI 970 Gaming, 256G WD blue SSD, GeForce GT710, HP LJ Pro M277-fdw

  7. #7
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: HOWTO: Amend your hosts file to block ads (by site)

    Quote Originally Posted by Mark_in_Hollywood View Post
    Do I rename the file updatehosts.sh.txt manually before or after running chomd a+x?
    It does not matter if you rename the file before or after the chomd.
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  8. #8
    Join Date
    Apr 2006
    Beans
    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.

    Quote Originally Posted by /usr/share/doc/tofrodos/NEWS.Debian.gz

    tofrodos (1.7.8.debian.1-2) unstable; urgency=low

    With this release the symlinks "unix2dos" and "dos2unix" are dropped from the
    package. This will allow the introduction of the original dos2unix package,
    which also supports conversion to MacOS style files.

    Should you have scripts depending on these symlinks, I recommend using
    shell-aliases, like in the following example for bash:

    alias unix2dos="/usr/bin/fromdos -u" (or todos)
    alias dos2unix="/usr/bin/fromdos -d" (or just fromdos)

    -- Alexander Reichle-Schmehl <tolimar@debian.org> Thu, 21 Jan 2010 20:59:11 +0100
    I simply updated the script by replacing all occurrences of dos2unix with fromdos using gedit (CTRL+H). Works like a charm now.

  9. #9
    Join Date
    Feb 2010
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: HOWTO: Amend your hosts file to block ads (by site)

    Just want to put a reminder here for all who are looking into using /etc/hosts to block ads: this seem to break desktopcouch, and with that UbuntuOne and Gwibber and Evolution. Multiple beam.smp processes will be spawned, sucking up CPU cycles, and seem to take forever to parse lines in /etc/hosts

    The bug is here: https://bugs.launchpad.net/ubuntu/lu...ch/+bug/530541

    Chad on the page suggested dnsmasq or similar as an alternative to /etc/hosts for the time being...
    The early bird gets the worm. . . But the second mouse gets the cheese, and the early worm gets eaten.
    Unwarranted Self Promotion

  10. #10
    Join Date
    Jan 2008
    Beans
    12

    Re: HOWTO: Amend your hosts file to block ads (by site)

    Awesome! I love this script! Thanks!

Page 1 of 8 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •