Block URLs/Websites
  • 10 Oct 2024
  • 1 Minute to read
  • PDF

Block URLs/Websites

  • PDF

Article summary

Use this script to block the websites defined in the script on the Linux machine.

  1. Copy and save the contents below to a UTF-8 editor like notepad++ OR Sublime Text in Windows or gedit in Ubuntu.

    1. If you are using notepad++ then use the bottom right panel to change the type to Unix (LF).

  2. Or click here to download the file.

    #!/bin/bash
    
    # Get the current username
    USERNAME=$(whoami)
    
    # Add the user to sudoers.d directory to restrict their permissions
    echo "$USERNAME ALL=(ALL) /bin/echo 'Permission denied'" | sudo tee /etc/sudoers.d/block_websites >/dev/null
    
    # Define the list of websites to block
    WEBSITES=(
        "www.example.com"
        "www.facebook.com")
    
    # Add entries to block the websites to hosts file
    for website in "${WEBSITES[@]}"; do
        echo "0.0.0.0 $website" | sudo tee -a /etc/hosts >/dev/null
    done
    
    # Flush DNS cache
    sudo systemctl restart systemd-resolved.service
    
    echo "Websites are now blocked."
  3. In the script, replace the following placeholder:

    1. WEBSITES=(): Provide a list of URLs with each URL in a new line, that you want to block.

  4. Follow our guide to upload & publish the script using Scalefusion Dashboard.

Note:

  1. Some of the scripts and their contents are sourced from internet and yes, our new friend ChatGPT.

  2. Please validate the scripts on a test machine before deploying them on all your managed devices.

  3. Scalefusion has tested these scripts, however Scalefusion will not be responsible for any loss of data or system malfunction that may arise due to the usage of these scripts.


Was this article helpful?