Skip to main content
  1. Posts/

How to block internet distractions

·431 words·3 mins·
Technology Computers Digital Minimalism
Joshua Blais
Author
Joshua Blais
Table of Contents

Although I don’t use social media, there are still many distractions to be had on the internet (oh the understatement!)

So, during days of work, I choose to block them all with a custom script and the good ol’ /etc/hosts file.

Between the hours of 4am to 4pm, I have a script that continually checks if the domains are in my hosts file and blocked, if they are not, it will add them and clear the network manager cache for instant blocking.

add_blocks.sh:

#!/bin/bash

# List of domains to block
domains=(
  "4chan.org"
  "lainchan.org"
  "reddit.com"
  # Add more domains as needed
)

# Function to add blocks
add_blocks() {
  changes_made=false
  for domain in "${domains[@]}"; do
    if ! grep -q "$domain" /etc/hosts; then
      echo "127.0.0.1 $domain" >> /etc/hosts
      echo "127.0.0.1 www.$domain" >> /etc/hosts
      changes_made=true
    fi
  done
}

# Function to remove blocks
remove_blocks() {
  if grep -q "${domains[0]}" /etc/hosts; then
    sed -i "/${domains[0]}/d" /etc/hosts
    changes_made=true
  fi
}

# Check current time
current_hour=$(date +%H)

# If it's between 4 AM and 4 PM, add blocks; otherwise, remove blocks
if [ $current_hour -ge 4 ] && [ $current_hour -lt 16 ]; then
  add_blocks
  echo "Blocks added."
else
  remove_blocks
  echo "Blocks removed."
fi

# Flush DNS cache only if changes were made
if [ "$changes_made" = true ]; then
  systemctl restart NetworkManager
  echo "NetworkManager restarted."
else
  echo "No changes made, NetworkManager not restarted."
fi

When you try to go to any of these sites, it will look like this:

reddit blocked

I run this as a systemd service. If I really need to access something (like an answer on reddit for work) I have a script to remove the blocks:

remove_blocks.sh:

# List of domains to unblock
domains=(
  "4chan.org"
  "lainchan.org"
  "reddit.com"
  # Add more domains as needed
)

# Remove blocks from /etc/hosts
for domain in "${domains[@]}"; do
  sudo sed -i "/$domain/d" /etc/hosts
done

# Flush DNS cache
sudo systemctl restart NetworkManager

This removes the blocking for 5 minutes so I can see the post and continue with work.

I have found that this friction alone has led me to working many hours per day without distraction, allowing deep work that I didn’t even know was possible.

On the smartphone
#

I simply use a public DNS configuration to block porn and nasty stuff on mobile, and elect to not use a browser almost ever (I replaced all my scrolling with reading)

You can find the dns list I use here. All you have to do is add it to your DNS configuration in the device and all internet traffic will go through it to block all sites.