Installing and Configuring Squid Proxy Server on Ubuntu: Effective Internet Access Control

Ubuntu tutorial - IT technology blog
Ubuntu tutorial - IT technology blog

The Real-World Problem: Why Control Internet Access?

When working with enterprise network systems, a recurring concern for me is how to effectively manage and control employee internet access. I’ve often witnessed significant drops in productivity, sometimes by 20-30%, simply because employees spend too much time on social media and entertainment sites. Even worse, accessing unsafe websites poses a risk of malware infection or leakage of important data.

Bandwidth management is also a significant challenge. Imagine a network with hundreds of users simultaneously streaming HD videos or downloading large data files. The internet connection will undoubtedly become overloaded, seriously affecting core business applications like ERP or CRM. Clearly, I need a tool to help block inappropriate websites while also improving overall internet access speed for the entire enterprise.

Root Cause Analysis

The root of the problem lies in the lack of a centralized control point for all internet traffic within the internal network. Most computers are granted direct internet access without any filtering or monitoring mechanisms. This situation is akin to leaving the door wide open for everyone to come and go without security, and without any logs being recorded.

  • **Lack of filtering mechanism**: No tools to block specific websites or inappropriate content types.
  • **Inability to monitor**: Difficult to know what users are accessing, when, and how much bandwidth they are consuming.
  • **Wasted resources**: Network bandwidth is wasted on non-work-related purposes.
  • **Security risks**: Accessing malicious websites can lead to malware infection, phishing attacks, undermining overall security.

Managing each individual computer is not only time-consuming but also impractical, especially for systems with hundreds or even thousands of devices. What I need is a comprehensive, automated, and simple-to-operate solution.

Common Solutions

I have researched and tried a few methods to solve this problem:

1. Configuring Firewall Rules

Firewalls can block specific IP addresses or domain names. This method is quite simple if you only need to block a few fixed addresses. However, when the blacklist expands to hundreds or thousands of websites, managing firewall rules becomes extremely complex and prone to errors. More importantly, firewalls do not have caching features to speed up web browsing for users.

2. DNS Filtering

This is a rather neat and easy-to-deploy method, by configuring the DNS server to resolve unwanted domain names incorrectly. Some public DNS services like OpenDNS or Cloudflare DNS also provide effective content filtering features. Advantages include fast speed and minimal client-side configuration. Disadvantages are the lack of necessary granularity, difficulty in applying different policies for different user groups, and no support for caching.

3. Using a Proxy Server

After careful consideration, I realized that deploying a Proxy Server is the most effective and comprehensive solution for these issues. A proxy server acts as an intermediary server; all client internet access requests must pass through it. This allows me to:

  • **Access Control**: Easily block or allow access to websites and applications based on flexible policies.
  • **Logging**: Record all user access activities for monitoring and internal auditing purposes.
  • **Bandwidth Optimization (Caching)**: Store frequently accessed content (like images, static files). When another user requests them, the proxy immediately returns them without re-downloading from the internet, helping to speed up web browsing and reduce network load.
  • **Anonymity**: Can help hide the client’s real IP address when accessing the internet, enhancing privacy.

The Best Solution: Installing and Configuring Squid Proxy on Ubuntu

Among numerous open-source proxy servers, Squid consistently stands out as a top choice. It is renowned for its robustness, flexibility, and high popularity within the IT community. Based on my experience deploying Squid, I tested this configuration in a staging environment running Ubuntu 22.04. The results showed it worked smoothly, perfectly meeting all team requirements before going into production.

Step 1: Update the System

Before installing any software, updating the system is always the first and crucial step:


sudo apt update
sudo apt upgrade -y

Step 2: Install Squid Proxy Server

Squid is available in Ubuntu’s default repositories, making the installation process incredibly simple:


sudo apt install squid -y

After installation, the Squid service will automatically start. You can check its status:


sudo systemctl status squid

Ensure that the status is active (running).

Step 3: Configure Squid

Squid’s main configuration file is located at /etc/squid/squid.conf. This file is quite long and contains numerous options. I strongly recommend backing up the original file before making any modifications:


sudo cp /etc/squid/squid.conf /etc/squid/squid.conf.bak
sudo nano /etc/squid/squid.conf

Basic Configuration: Change Port and Allow Internal Network

By default, Squid listens for requests on port 3128. You can change this port if desired. Find the line http_port 3128 and modify it to your preferred port number, for example, 8080:


# Change proxy port from 3128 to 8080
http_port 8080

Next, we need to define ACLs (Access Control Lists) to determine who is allowed to use the proxy. Find the lines defining acl localnet src and uncomment them (remove the #), or add a new line suitable for your internal network range. For example, if your network is 192.168.1.0/24:


# Example: Your internal network
acl localnet src 192.168.1.0/24

# Ensure these lines are present and in the correct order
http_access allow localhost manager
http_access deny manager
http_access allow localnet
http_access deny all

It’s important to note that ACL rules are processed sequentially from top to bottom. The line http_access allow localnet means allowing all machines within the 192.168.1.0/24 network range to access the internet via the proxy. Conversely, http_access deny all will reject all other requests that do not match any allow rules defined above.

Access Control: Blocking Unwanted Websites

This is a critically important section for addressing time wastage and enhancing security. We will create a separate file to contain the list of blocked websites.


sudo nano /etc/squid/blocked_sites.acl

In this file, add the domain names you wish to block, one domain per line. Importantly, you can add a dot (.) before the domain name to block all its subdomains as well:


.facebook.com
.youtube.com
.tiktok.com
.torrentfreak.com

Save and close the file. Then, edit /etc/squid/squid.conf again to use this ACL:


# Add ACL for the list of banned sites
acl blocked_domains dstdomain "/etc/squid/blocked_sites.acl"

# The http_access rule must come after the ACL definitions
http_access deny blocked_domains

# Ensure these lines are present and in the correct order (example modified to fit)
http_access allow localhost manager
http_access deny manager
acl localnet src 192.168.1.0/24
http_access allow localnet
http_access deny all

Extremely important note: The rule http_access deny blocked_domains MUST be placed before the rule http_access allow localnet. If you place it in reverse order, allow localnet will take effect first, allowing access and completely bypassing the blocking rules you have set up.

Step 4: Restart the Squid Service

After each configuration change, you need to restart the Squid service for the changes to take effect:


sudo systemctl restart squid

If there are any errors in the configuration file, Squid will fail to start. To identify the cause, you can check the service’s logs:


sudo journalctl -u squid -f

Step 5: Configure Clients to Use the Proxy

For client machines on the network to use the Squid proxy, you need to configure their web browsers or directly on the operating system. For example, in Google Chrome browser:

  1. Go to Settings -> System -> Open your computer's proxy settings.
  2. In the operating system’s proxy settings, select Manual proxy setup.
  3. Enter the IP address of the Ubuntu server (running Squid) and the proxy port you configured (e.g., 8080).
  4. Apply for HTTP and HTTPS.

After configuration, internet access requests from this browser will pass through the Squid proxy.

Step 6: Test and Monitor Operations

You can check if the proxy is working by attempting to access a blocked website (like facebook.com) from a client machine configured to use the proxy. You should see an Access Denied message or the page failing to load.

To monitor requests passing through the proxy, you can view Squid’s access log file:


sudo tail -f /var/log/squid/access.log

Each line in this log file will display detailed information about the access request, including time, client IP address, accessed website, and processing status (e.g., TCP_DENIED if the request was blocked).

Advanced Configuration (A Few Points to Know)

  • **User Authentication**: To control internet access based on specific users or user groups, you can integrate Squid with robust authentication mechanisms like NCSA (using htpasswd) or LDAP.
  • **Transparent Proxy**: Configuring a Transparent Proxy allows clients to automatically use the proxy without any manual configuration on each device. To implement this, you need to fine-tune routing or firewall configurations to redirect all HTTP/HTTPS traffic through Squid. This is a more complex topic, and I will dedicate a separate article to share the details later.
  • **Caching Policy**: You can fine-tune caching parameters such as cache_mem (RAM allocated for cache) or maximum_object_size (maximum size of cached objects) to achieve optimal caching performance.
  • **SSL Bump (HTTPS Interception)**: If you want Squid to control and filter HTTPS content, you need to configure SSL Bump. However, this requires installing the proxy’s certificate on all client machines. Otherwise, users will constantly receive security warnings from their browsers.

Conclusion

In summary, deploying a Squid Proxy Server on Ubuntu is an extremely powerful and flexible solution. It not only helps tightly control internet access but also enhances security and optimizes bandwidth efficiency within the internal network. By following the steps I’ve outlined, you can quickly have an effectively operating proxy system. Don’t forget to always thoroughly check configuration changes and regularly monitor logs to ensure the system operates as expected!

Share: