Blocking internet

Last modified:

Going offline

Minimizing escapism is a bit of an obsession. My phone has barely any applications on it. I have almost no social media, and refuse to actively use it. Live in the now. Escapism is a constant battle, and what it is is a matter of perspective.

It can be nice to not use internet during the week unless I had a course to participate in. It is possible to download content you want to learn during the weekend. Tip, try yt-dlp.

Initial findings

I felt more relaxed, well-rested and have less need to use the computer. It seems like a huge improvement. Being bored is great for creativity too.

It is only ideas gained from walking that have any worth.

– Friedrich Nietzsche, Twilight of the Idols

I seem to get more ideas. Nietzsche's quote is good, but I want to expand on it. Good ideas come from engaging in physical activities and when spending time away from the internet or other forms of escapism. That's likely what he meant anyway.

Linux

This script is in bash, and should work on most Linux distributions, but I couldn't make it work on NixOS. Currently, I have internet access during weekdays, but I would prefer not to.

# Define day and time periods for blocking
OFFTIME="--weekdays Mon,Tue,Thu"
# Wed = 3D modeling
# Fri = Drawing

# Submit rules - it does not hurt to block input as well
sudo iptables -A INPUT -p all --match time $OFFTIME  -j DROP
sudo iptables -A OUTPUT -p all --match time $OFFTIME -j DROP

# Review the rules just added
# sudo iptables -L | grep -P -A2 '.+policy.+'

# Save rules (reboot is NOT required)
sudo iptables-save | sudo tee /etc/iptables.rules >/dev/null
echo 'iptables-restore < /etc/iptables.rules' | sudo tee -a /etc/rc.local >/dev/null

# Remove all rules
# sudo iptables -F
# sudo rm /etc/iptables.rules && sudo rm /etc/rc.local

NixOS

On NixOS I found a way to do this too, by adding the following to my configuration.nix. For other Linux systems it may be interesting to look at nftables too. You can probably figure out how to configure it.

  # Nftables is newer and has better performance than iptables, eventually iptables may get deprecated
  networking.nftables = {
    enable = true;
    # filter table which is used in the digital-detox systemd service
    tables = {
      detox-filter = {
        family = "inet";
        content = ''
          chain output {
            type filter hook output priority 0; 
            policy accept;
          }
        '';
      };
    };
  };

  # IMPORTANT: This can disable internet! Copy paste at your own risk.
  systemd.services = {
    digital-detox = {
      description = "Block internet during weekdays.";
      # Nft is not in the path - use the full path
      script = ''
        # There is a race condition with nftables in which the table does not exist.
        # Thus, sleep is required despite having "After" and "Requires".
        sleep 1
        # Fri = Drawing, Thu = German lessons - not much time for internet anyways
        case "$(date +%a)" in
          Mon|Tue|Wed) /run/current-system/sw/bin/nft add rule inet detox-filter output drop || true ;;
          *) /run/current-system/sw/bin/nft flush chain inet detox-filter output || true ;;
        esac
      '';
      serviceConfig = {
        Type = "oneshot"; # Run the script once
        After = [ "nftables.service" ]; # Nftables should be started first
        Requires = [ "nftables.service" ]; # Nftables is required
      };
      wantedBy = [ "multi-user.target" ]; # Start after login
    };
  };

Windows

I made a block internet script for Windows. You can find it here for the people who may be interested in using something similar.

# Network-Blocker.ps1
# Friday is drawing class, so not much time for internet anyways
$DaysToBlock = @("Monday", "Tuesday", "Wednesday", "Thursday")

If ((Get-Date | Select DayOfWeek).DayOfWeek -in $DaysToBlock) {
   Disable-NetAdapter -Name "*" -Confirm:$False
} Else {
   Enable-NetAdapter -Name "*" -Confirm:$False
}

It is possible to execute it on startup by running the following PowerShell code:

# $env:Temp gave me a shortened incorrect path, so I use the dotnet variant
$NetworkBlockerPath = "$env:UserProfile\Documents\PowerShell\Functions\Network-Blocker.ps1"
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "Network-Blocker" -Value "$NetworkBlockerPath" -ExecutionPolicy Unrestricted -WindowStyle Hidden >> `"$([System.IO.Path]::GetTempPath())StartupLog.txt`""

Share

Diaspora X Facebook LinkedIn

Donate