How to temporarily disable Hosts file in Fatdog? (Solved)

New to Puppy and have questions? Start here

Moderator: Forum moderators

Post Reply
User avatar
p310don
Posts: 310
Joined: Tue Dec 03, 2019 7:17 am
Location: Brisbane, Australia
Has thanked: 58 times
Been thanked: 92 times

How to temporarily disable Hosts file in Fatdog? (Solved)

Post by p310don »

I have kids in the house who I want to stop from watching stuff on youtube and other sites.

I have added youtube to my hosts file, which blocks it. Works well until I want to watch something myself.

I'd like to have a script that I can invoke the host with the block and then run it again and it lifts the block.

My thought is to have two files, hosts and hosts.BAK

The idea is to have the script rename hosts to hosts.BAK and hosts.BAK to hosts, with one file having blocked content and the other file not having blocked stuff. I don't know how to make the files change simultaneously, being that I cannot copy hosts to hosts.BAK then hosts.BAK already exists.

I'm probably missing something really quite simple, but I'm still missing it.

User avatar
fredx181
Posts: 2606
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 283 times
Been thanked: 1014 times
Contact:

Re: Help Needed With Hosts Blocking Script

Post by fredx181 »

I'd say with two scripts to toggle blocking (or just run the command line inside) (different way than toggling with a .BAK file)

To add youtube block:

Code: Select all

#!/bin/bash
# add youtube block
echo "#Youtube block
0.0.0.0 www.youtube.com
" >> /etc/hosts

To remove youtube block:

Code: Select all

#!/bin/bash
# remove youtube block
sed -i '/^#Youtube block/,/^$/d' /etc/hosts

Probably possible also to have one script that allows to choose between blocking or not (edit: and checking if already blocked would be nice too), but requires more code.

EDIT; you can add more sites to be blocked , just example:

Code: Select all

#!/bin/bash
# add youtube block (and more)
echo "#Youtube block
0.0.0.0 www.youtube.com
0.0.0.0 googleadapis.l.google.com
0.0.0.0 googleadapis.l.google.com
0.0.0.0 googleads.g.doubleclick.net
0.0.0.0 pagead2.googlesyndication.com
0.0.0.0 pagead46.l.doubleclick.net
" >> /etc/hosts

And that list will all be removed with the second script (so everything below "#Youtube block")

Trapster
Posts: 148
Joined: Sat Aug 01, 2020 7:44 pm
Has thanked: 1 time
Been thanked: 40 times

Re: Help Needed With Hosts Blocking Script

Post by Trapster »

Create an /etc/hosts.BLOCK and an /etc/hosts.UNBLOCK
Run your script(s) to delete the current /etc/hosts and then copy either of the above files to /etc/hosts.

User avatar
Flash
Moderator
Posts: 901
Joined: Tue Dec 03, 2019 3:13 pm
Location: Arizona, U.S.
Has thanked: 46 times
Been thanked: 105 times

Re: How to temporarily disable Hosts file?

Post by Flash »

Rather then move or block the files, could they be renamed?

Chaos coordinator :?
williwaw
Posts: 1627
Joined: Tue Jul 14, 2020 11:24 pm
Has thanked: 147 times
Been thanked: 295 times

Re: How to temporarily disable Hosts file?

Post by williwaw »

include the blocked sites in /etc/hosts making it the default bootup condition
have a ~/hosts which contains the original hosts file
put alias hosts="cp hosts /etc/hosts" in your .bashrc
when you want to watch your content, open a terminal and run # hosts
shutdown when you are done.

by the time the kids figure this out they will be watching youtube anyways on their phone

User avatar
p310don
Posts: 310
Joined: Tue Dec 03, 2019 7:17 am
Location: Brisbane, Australia
Has thanked: 58 times
Been thanked: 92 times

Re: How to temporarily disable Hosts file?

Post by p310don »

@fredx181 your solutions worked perfectly, with a little adjustment...

My original hosts file had two entries, youtube and crazygames. Each URL was separated by a line. Your unblock script stops at the first line space, so it simply needed to have the line removed. With the two entries immediately one after the other, it works perfectly.

I use Fatdog, so have used SVEN to create keyboard shortcuts, win+b blocks youtube and win+u unblocks youtube.

I'll probably put the block script in the ~/Startup directory so that youtube is blocked by default.

Thanks for all the suggestions :)

Post Reply

Return to “Beginners Help”