If you have been requested to implement an internet access solution and you are passionate about OpenSource and GNU/Linux you are going to love this tutorial, we are going to do so by implementen a squid proxy server on Centos/REHL 7.

Must of you will be thinking about implementing a firewall solution, but keep in mind that firewalls are an amazing way to keep your internet safe and under control, but in some cases, they are very expensive and hard to manage (there are OpenSource firewalls as well), but if you are thinking about a little environment or would like to experiment some there is nothing better then using a Proxy Server.

Important: Firewalls and Proxy Servers are not equaled but have some features that share in common like allowing/restricting internet access which is what we are going to today without squid server.

This will be running on a CENTOS/RHEL 7 VM with minimum specs.

Make sure to have your server up-to-date by running

#yum -y update

Next, we need to install Squid Proxy by running the following command:

#yum -y install squid

Once your Squid has been successfully installed you can go ahead and start the program immediately

#systemctl start squid

Do not forget to enable the squid service in order to start automatically at boot.

#systemctl enable squid

It is important to check on the Squid status just to make sure that everything is working fine.

#systemctl status squid

You will see a pretty similar output like this:

[[email protected] ~]# systemctl status squid
● squid.service - Squid caching proxy
   Loaded: loaded (/usr/lib/systemd/system/squid.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-12-29 20:11:04 EST; 13s ago
 Main PID: 1210 (squid)
   CGroup: /system.slice/squid.service
           ├─1210 /usr/sbin/squid -f /etc/squid/squid.conf
           └─1212 (squid-1) -f /etc/squid/squid.conf

Dec 29 20:10:44 squidserver systemd[1]: Starting Squid caching proxy...
Dec 29 20:11:04 squidserver systemd[1]: Started Squid caching proxy.
Dec 29 20:11:04 squidserver squid[1210]: Squid Parent: will start 1 kids
Dec 29 20:11:04 squidserver squid[1210]: Squid Parent: (squid-1) process 121...d
Hint: Some lines were ellipsized, use -l to show in full.

If you are now to Squid you might want to have a look at the squid options by running:

#squid -h

and you will get:

[[email protected] ~]# squid -h
Usage: squid [-cdhvzCFNRVYX] [-n name] [-s | -l facility] [-f config-file] [-[au] port] [-k signal]
       -a port   Specify HTTP port number (default: 3128).
       -d level  Write debugging to stderr also.
       -f file   Use given config-file instead of
                 /etc/squid/squid.conf
       -h        Print help message.
       -k reconfigure|rotate|shutdown|restart|interrupt|kill|debug|check|parse
                 Parse configuration file, then send signal to
                 running copy (except -k parse) and exit.
       -n name   Specify service name to use for service operations
                 default is: squid.
       -s | -l facility
                 Enable logging to syslog.
       -u port   Specify ICP port number (default: 3130), disable with 0.
       -v        Print version.
       -z        Create missing swap directories and then exit.
       -C        Do not catch fatal signals.
       -D        OBSOLETE. Scheduled for removal.
       -F        Don't serve any requests until store is rebuilt.
       -N        No daemon mode.
       -R        Do not set REUSEADDR on port.
       -S        Double-check swap during rebuild.
       -X        Force full debugging.
       -Y        Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.

for example:

#squid -v
[[email protected] ~]# squid -v
Squid Cache: Version 3.5.20
Service Name: squid

Time to configure Squid

You can use whatever text editor you feel more comfortable, I like vi/vim, we need to modify the global configuration file /etc/squid/squid.conf

# vi /etc/squid/squid.conf

Do not get overwhelmed by the amount of information, this is just the default configuration file that will be working on in order to configure the proxy server.

#
# Recommended minimum configuration:
#

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports

# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# 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

# And finally deny all other access to this proxy
http_access deny all

# Squid normally listens to port 3128
http_port 3128

# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /var/spool/squid 100 16 256

# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid

#
# Add any of your own refresh_pattern entries above these.
#
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern .               0       20%     4320

How to allow a range of IP addresses to access the Internet through your proxy server

It is just as simple as adding a new ACL entry, Squid supports CIDR notation which makes the job more simple, for example, if you want to allow a range of IPs from 192.168.241.1 to 192.168.241.255 then you will need to add the following entry:

acl localnet src 110.220.330.0/24

Your acl list will be looking like this:

acl localnet src 192.168.241.0/24 # My new ACL
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

Save your changes and move forward to restart the squid service.

Important: You just can reload in order for the service to read the global configuration file, using reload won’t restart the service.

#systemctl reload squid

Or

#systemctl restart squid

Before we start using our proxy server we must consider that Squid only allows a few ports as “Safe ports” in order to allow connections through, here are the list of default allowed ports:

acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http

If you need any other port in specific that is not listed in your squid global configuration file feel free to add it otherwise your client won’t be able to connect to the internet through you Proxy Server.

Restart or reload your squid after performing any changes.

If you want to restrict access to several websites this can be easily done vi creating a new file called blocked_sites under /etc/squid

#vi /etc/squid/blocked_sites

Here are some websites, for example, we wish to block (we actually do not want to block these but it is for educational purposes)

facebook.com
youtube.com

Save the file and open the /etc/squid/squid.conf in order to add the new rule.

#vi /ect/squid/squid.conf

Enter the following lines under acl and http_access lists

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

If you feel the need of changing the Squid default port, you just need to modify:

# Squid normally listens to port 3128
http_port 3128

There is nothing else to say at this moment if you need to go a little bit more in deep about Squid go and check out their website http://www.squid-cache.org/ their documentation is pretty good.

Hope you enjoyed this tutorial and do not forget to share it.

Buy me a coffeeBuy me a coffee