fbpx

In this tutorial, we will walk you through the process of installing and configuring a proxy server on Ubuntu using Squid Proxy. Are you looking for a way to improve your internet speed and security? Installing a proxy server on your Ubuntu machine using Squid Proxy can help you achieve both.

What is a Proxy Server? A proxy server is an intermediary server that sits between your device and the internet. It acts as a gateway between your device and the internet, filtering requests and responses to improve performance, security, and privacy. When you request a web page or resource, the proxy server intercepts the request and retrieves the data on your behalf. It then sends the data back to your device, without revealing your IP address to the website you are accessing.

Why use a Proxy Server? There are many reasons why you might want to use a proxy server. Here are some of the most common reasons:

  • Improved speed: A proxy server can cache frequently accessed web pages and resources, reducing the time it takes to access them in the future.
  • Increased security: A proxy server can act as a barrier between your device and the internet, protecting you from malicious attacks and filtering out unwanted content.
  • Enhanced privacy: A proxy server can hide your IP address, making it difficult for websites to track your online activity.

Now, let’s get started with installing and configuring a proxy server on Ubuntu using Squid Proxy.

Step 1: Install Squid Proxy

The first step is to install Squid Proxy on your Ubuntu machine. Open up a terminal window and type the following command:

sudo apt-get install squid
Install Squid Proxy

This will download and install Squid Proxy on your machine.

Step 2: Configure Squid

Next, we need to configure Squid Proxy to work with your network. Open up the Squid configuration file by typing the following command:

sudo nano /etc/squid/squid.conf

This will open up the configuration file in the Nano text editor.

Find the line that reads “http_port 3128” and uncomment it by removing the “#” symbol at the beginning of the line. This line specifies the port number that Squid Proxy will listen on.

Next, find the line that reads “http_access deny all” and change it to “http_access allow all”. This line specifies the access control list for Squid Proxy, allowing all clients to connect to the proxy server.

Save the changes to the configuration file by pressing “Ctrl+X“, then “Y”, and finally “Enter”.

Configure Squid Proxy

Step 3: Start Squid

Now that Squid Proxy is installed and configured, we can start the service by typing the following command:

sudo systemctl start squid
sudo systemctl enable squid

This will start the Squid Proxy service.

Step 4: Test Squid Proxy

To test if Squid Proxy is working correctly, open up a web browser and go to the network settings. Find the proxy settings and enter the IP address of your Ubuntu machine and the port number that Squid Proxy is listening on (in our case, it’s 3128). Save the changes and try accessing a website. If everything is working correctly, the website should load without any issues.

You can also check if the port is open by using nmap:

root@community-tutorials:~# nmap localhost -p3128
Starting Nmap 7.80 ( https://nmap.org ) at 2023-04-12 09:22 CEST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0000020s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 65533 closed ports
PORT STATE SERVICE
3128/tcp open squid-http

Nmap done: 1 IP address (1 host up) scanned in 0.60 seconds
root@community-tutorials:~#

Configure Proxy Authentication

If you want to add authentication to your proxy server, follow these steps:

Step 1: Create a Password File

First, you need to create a password file that will store the usernames and passwords of the users who are allowed to access the proxy server. You can create the password file by typing the following command:

sudo htpasswd -c /etc/squid/passwd username

Replace “username” with the username of the user you want to add. You will be prompted to enter and confirm a password for the user.

If you are receiving the error message “sudo: htpasswd: command not found”, it means that the “htpasswd” command is not installed on your system.

“htpasswd” is a command-line tool used to manage user authentication files for basic authentication with Apache web server or Squid Proxy server.

To resolve this issue, you can try installing the “apache2-utils” package, which contains the “htpasswd” command, by typing the following command:

sudo apt-get install apache2-utils
root@community-tutorials:~# sudo htpasswd -c /etc/squid/passwd virtono
New password:
Re-type new password:
Adding password for user virtono
root@community-tutorials:~#

You can use our password generator tool, click here.

Step 2: Edit the Squid Configuration File

Next, you need to edit the Squid configuration file to enable authentication. Open the configuration file by typing the following command:

sudo nano /etc/squid/squid.conf

Find the line that reads “http_access allow all” and change it to “http_access allow USERNAME”. Replace “USERNAME” with the username you added to the password file.

Edit the Squid Configuration File

Find the line that reads “auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd” and uncomment it by removing the “#” symbol at the beginning of the line. This line specifies the path to the password file.

Save the changes to the configuration file by pressing “Ctrl+X”, then “Y”, and finally “Enter”.

Step 3: Restart Squid Proxy

Now that you have edited the Squid configuration file, you need to restart the Squid Proxy service by typing the following command:

sudo systemctl restart squid

Step 4: Test Authentication


To test if authentication is working correctly, open up a web browser and go to the network settings. Find the proxy settings and enter the IP address of your Ubuntu machine, the port number that Squid Proxy is listening on (in our case, it’s 3128), and the username and password of the user you added to the password file. Save the changes and try accessing a website. If everything is working correctly, the website should load without any issues.

How to Block Websites

To block certain websites on your Ubuntu machine using Squid Proxy, follow these steps:

Step 1: Create an ACL for the Blocked Websites

Next, you need to create an Access Control List (ACL) for the websites you want to block. Add the following lines to the configuration file:

acl blocked_sites dstdomain "/etc/squid/blocked_sites.txt" 
http_access deny blocked_sites

The first line creates an ACL called “blocked_sites” and specifies the path to a text file called “blocked_sites.txt” that contains a list of websites you want to block. The second line denies access to any website that matches the “blocked_sites” ACL.

Step 3: Create the Blocked Sites File

Create the “blocked_sites.txt” file in the /etc/squid/ directory by typing the following command:

sudo nano /etc/squid/blocked_sites.txt

Add the websites you want to block to the file, one website per line. For example, to block Facebook and YouTube, add the following lines:

facebook.com
youtube.com

Save the changes to the file by pressing “Ctrl+X”, then “Y”, and finally “Enter”.

Step 4: Restart Squid Proxy

Restart the Squid Proxy service by typing the following command:

sudo systemctl restart squid

Step 5: Test the Blocked Websites

To test if the websites are blocked, try accessing one of the blocked sites from a web browser. If everything is working correctly, you should see an error message indicating that the website is blocked.

How to Uninstall Squid Proxy Server

If you want to uninstall Squid Proxy Server from your Ubuntu machine, follow these steps:

Step 1: Stop the Squid Proxy Service

Before uninstalling Squid Proxy Server, you need to stop the Squid Proxy service by typing the following command:

sudo systemctl stop squid

Step 2: Remove the Squid Proxy Package

Remove the Squid Proxy package from your Ubuntu machine by typing the following command:

sudo apt-get remove squid

This command will remove the Squid Proxy package from your system, but it will not remove any configuration files or data associated with the package.

Step 3: Remove the Squid Proxy Configuration Files

If you want to remove the Squid Proxy configuration files, type the following command:

sudo apt-get purge squid

This command will remove the Squid Proxy package and all associated configuration files and data from your system.

Step 4: Remove the Squid Proxy Cache

If you want to remove the Squid Proxy cache, type the following command:

sudo rm -rf /var/spool/squid

This command will remove the Squid Proxy cache directory and all its contents.

Step 5: Restart your Ubuntu Machine

Restart your Ubuntu machine by typing the following command:

sudo reboot

0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.