Install and Configure Squid Proxy on Ubuntu 20.04

In this article, we want to teach you How To Install and Configure Squid Proxy on Ubuntu 20.04.

Squid is a Unix-based proxy server that caches Internet content closer to a requestor than its original point of origin.

Squid supports caching of many different kinds of Web objects, including those accessed through HTTP and FTP.

Caching frequently requested Web pages, media files, and other content accelerates response time and reduces bandwidth congestion.

How To Install and Configure Squid Proxy on Ubuntu 20.04

Before you start to install Squid Proxy on Ubuntu 20.04, you need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our article the Initial Server Setup with Ubuntu 20.04.

Also, you need a domain name that pointed to your server’s IP address.

Now you can follow the steps below to install Squid proxy on your server.

Set up and Configure Squid Proxy on Ubuntu 20.04

First, you need to update your local package index with the command below:

sudo apt update

Then, you can use the following command to install Squid proxy on your server:

sudo apt install squid

Squid will start automatically after you install it on your server.

To check that your service is active and running on your server, run the following command:

systemctl status squid.service

In your output you will see:

Output
squid.service - Squid Web Proxy Server
Loaded: loaded (/lib/systemd/system/squid.service; enabled; vendor preset:>
Active: active (running) since Tue 2022-01-11 11:12:08 CET; 1min 23s ago
Docs: man:squid(8)
Main PID: 1753 (squid)
Tasks: 4 (limit: 2282)
Memory: 16.5M
CGroup: /system.slice/squid.service
├─1753 /usr/sbin/squid -sYC
├─1755 (squid-1) --kid squid-1 -sYC
├─1758 (logfile-daemon) /var/log/squid/access.log
└─1785 (pinger)

Now you need to make some configuration changes in the Squid configuration file on Ubuntu 20.04 to allow clients to connect to Squid from outside this server.

Open the file with your favorite text editor, here we use vi:

sudo vi /etc/squid/squid.conf

Find the lines below at the file:

...
http_access allow localhost
... http_access deny all
...

You can change the deny all to allow all and anyone can connect to your proxy server. But it’s not recommended to do that. You can add the line below and define your IP address to connect to the Squid proxy.

You can find your IP address from the What’s My IP?

Then, add the below line above the http_access allow localhost line.

...
acl localnet src your_ip_address
http_access allow localhost
...

When you are done, save and close the file.

Secure Squid Proxy on Ubuntu 20.04

At this point, you need to secure your Squid proxy on Ubuntu 20.04. Squid allows you to create username-password pairs using built-in Linux functionality, as an additional or an alternative step to restricting access to your proxy by IP address.

First, you need to install some utilities from Apache in order to have access to a password generator that squid likes:

sudo apt install apache2-utils

Then, you can use the htpasswd command to generate a password for your new Squid user:

sudo htpasswd -c /etc/squid/passwords your_squid_username

You will be asked to enter a password for your Squid user.

This command will store your username along with a hash of your new password in /etc/squid/passwords, which will be used as an authentication source by Squid.

You can use the following command to see what that looks like:

sudo cat /etc/squid/passwords
Output
orca:$apr1$xheiCDJ4$QdsLXYxMmHU2V5Fs3Y25e0

Now you need to open the Squid configuration file on Ubuntu 20.04 again with your favorite text editor, here we use vi:

sudo vi /etc/squid/squid.conf

Then, add the red lines in your file:

…
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
include /etc/squid/conf.d/*
auth_param basic program /usr/lib/squid3/basic_ncsa_auth /etc/squid/passwords
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
# Example rule allowing access from your local networks.
acl localnet src your_ip_address
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
#http_access allow localnet
http_access allow localhost
http_access allow authenticated
# And finally deny all other access to this proxy
http_access deny all
…

When you are done, save and close the file.

To apply the changes, restart your Squid service on Ubuntu 20.04:

sudo systemctl restart squid.service

We assumed that you have enabled the UFW firewall. Now you need to open port 3128 through the firewall with the following command:

sudo ufw allow 3128

Reload the firewall to apply the new rules:

sudo ufw reload

How To Connect through Squid Proxy

To display your Squid server, you can use the curl command on Ubuntu 20.04. To do this, run the following command:

curl -v -x http://your_squid_username:your_squid_password@your_server_ip:3128 http://www.google.com/

In your output you will see:

Output
* Trying server-ip:3128...
* TCP_NODELAY set
* Connected to server-ip (server-ip) port 3128 (#0)
* Proxy auth using Basic with user 'orca'
> GET http://www.google.com/ HTTP/1.1
...

Also, you can access HTTPs sites with your Squid proxy without any configuration changes.

curl -v -x http://your_squid_username:your_squid_password@your_server_ip:3128 https://www.google.com/

In your output you will see:

Output
* Trying server-ip:3128...
* TCP_NODELAY set
* Connected to server-ip (server-ip) port 3128 (#0)
* allocate connect buffer!
* Establish HTTP proxy tunnel to www.google.com:443
* Proxy auth using Basic with user 'orca'
> CONNECT www.google.com:443 HTTP/1.1
> Host: www.google.com:443
> Proxy-Authorization: Basic b3JjYTpvcmNhMTIz
> User-Agent: curl/7.68.0
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 Connection established
<
* Proxy replied 200 to CONNECT request
* CONNECT phase completed!

For more information about Squid proxy, you can visit the Squid Documentation page.

Conclusion

At this point, you learn to set up and configure Squid Proxy on Ubuntu 20.04. Also, you learn to secure your Squid proxy by creating username-password pairs using built-in Linux functionality.

Hope you enjoy it.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular