You can delete/clear cache and re-create the cache directories on a server using the following procedure. Please note that you need to run additional commands if you are using SELinux on a CentOS/RHEL/Fedora Linux.
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | Yes |
Requirements | None |
Time | 5m |
Step 1: Find Squid cache dir location
Open a terminal app/windows and then type:
# grep cache_dir /etc/squid/squid.conf
Sample outputs:
cache_dir ufs /var/spool/squid 100 16 256
For remote server use the ssh command:
$ ssh vivek@server1.cyberciti.biz
$ sudo grep cache_dir /etc/squid/squid.conf
We can see squid cahce size using the du command:
$ sudo du -ch /var/spool/squid
2.8M /var/spool/squid/00/00 4.0K /var/spool/squid/00/BA 4.0K /var/spool/squid/00/17 4.0K /var/spool/squid/00/34 4.0K /var/spool/squid/00/33 4.0K /var/spool/squid/00/32 4.0K /var/spool/squid/00/49 4.0K /var/spool/squid/00/E0 4.0K /var/spool/squid/00/3D 4.0K /var/spool/squid/00/44 4.0K /var/spool/squid/00/98 4.0K /var/spool/squid/00/2B 3.8M /var/spool/squid/00 28M /var/spool/squid/ 28M total
Step 2: Restart Squid with an empty cache
Open a terminal and login as root user. Or use the ssh command to login to the remote server. Once logged in type the following command to shutdown squid proxy server:
# squid -k shutdown
Use the following rm command to delete the directory:
# rm -rf /path/to/dir/cache/
# rm -rf /var/spool/squid/
Please note that you need repeat the rm command for each cache_dir location you wish to empty. Next, use the following mkdir command to create directory and set permission using chown command:
# mkdir /var/spool/squid/
# chown squid:squid /var/spool/squid/
On a Debian or Ubuntu Linux squid server run by a user named proxy. In other words type the following chown command:
# chown proxy:proxy /var/spool/squid/
A better option is to search user name using the grep command/egrep command as follows and then apply that username with the chown command:
# grep cache_effective_user /path/to/your/squid.conf
# grep -i --color cache_effective_user /etc/squid/squid.conf
A note about SELinux user
If you are using SELinux on a CentOS/RHEL/Fedora Linux, type the following command to restestore file(s) default SELinux security contexts:
# restorecon -vr /var/spool/squid
Do not skip above steps.
Creating cache_dir
Finally, to re-create the cache directories, enter:
# squid -z
Sample outputs:
2020/06/02 14:01:55 kid1| Creating missing swap directories 2020/06/02 14:01:55 kid1| /var/spool/squid exists 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/00 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/01 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/02 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/03 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/04 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/05 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/06 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/07 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/08 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/09 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/0A 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/0B 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/0C 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/0D 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/0E 2020/06/02 14:01:55 kid1| Making directories in /var/spool/squid/0F 2020/06/02 14:01:56| Removing PID file (/var/run/squid.pid)
Starting squid proxy server
To start squid type:
# squid
OR
# service squid start
Sample outputs:
Starting squid: [ OK ]
Modern Linux distro user need to use the systemctl command as follows:
# systemctl start squid.service
## Check status too ##
# systemctl status squid.service
● squid.service - Squid Web Proxy Server
Loaded: loaded (/lib/systemd/system/squid.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-06-02 14:02:53 UTC; 36s ago
Docs: man:squid(8)
Process: 554185 ExecStartPre=/usr/sbin/squid --foreground -z (code=exited, status=0/SUCCESS)
Process: 554188 ExecStart=/usr/sbin/squid -sYC (code=exited, status=0/SUCCESS)
Main PID: 554189 (squid)
Tasks: 6 (limit: 2282)
Memory: 16.7M
CGroup: /system.slice/squid.service
├─554189 /usr/sbin/squid -sYC
├─554191 (squid-1) --kid squid-1 -sYC
├─554192 (logfile-daemon) /var/log/squid/access.log
├─554193 (unlinkd)
├─554194 diskd 567491588 567491589 567491590
└─554195 (pinger)
Jun 02 14:02:54 sg-vpn-1 squid[554191]: 0 Objects expired.
Jun 02 14:02:54 sg-vpn-1 squid[554191]: 0 Objects cancelled.
Jun 02 14:02:54 sg-vpn-1 squid[554191]: 0 Duplicate URLs purged.
Jun 02 14:02:54 sg-vpn-1 squid[554191]: 0 Swapfile clashes avoided.
Jun 02 14:02:54 sg-vpn-1 squid[554191]: Took 0.25 seconds ( 0.00 objects/sec).
Jun 02 14:02:54 sg-vpn-1 squid[554191]: Beginning Validation Procedure
Jun 02 14:02:54 sg-vpn-1 squid[554191]: Completed Validation Procedure
Jun 02 14:02:54 sg-vpn-1 squid[554191]: Validated 0 Entries
Jun 02 14:02:54 sg-vpn-1 squid[554191]: store_swap_size = 0.00 KB
Jun 02 14:02:54 sg-vpn-1 squid[554191]: storeLateRelease: released 0 objects
Step 3: Verify Squid is running
Type the following commands:
# netstat -tulpn | grep :3128
# ss -tulpn | grep :3128
# tail -f /var/log/squid/access.log
# tail -f /var/log/squid/cache.log
Conclusion
In this tutorial, you learned how to shutdown running Squid proxy server, delete cache directory to clear squid proxy cache and start the server again. For more information see Squid proxy server docs here.
🐧 10 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
.. thanks but this is the kinda useless bit of recycled and generic documentation which gets novice admins into trouble.
Be weary of doing this on a server hardned with selinux.
Squid will not be able to access the freshly created cache.
EG (centos 6.x) :
You can simply run restorecon command. The faq has been updated to include a note about SELinux user. Hope this helps and I appreciate your post.
Novice sysadmins set SELinux to disabled, so, there is no problem at all ;)
lol
This is incorrect information. The user to own the cache directory is not necessarily “squid” as listed above.
On Ubuntu, by default it is “proxy“, other distros might use “nobody“.
To find the proper user you need to locate the variable “cache_effective_user” in the config file squid.conf. If this setting is commented out, you’ll have to look at the comments in that file to find the default.
i follewd all stpes but squid failed to start.
[root@server ~]# squid -z
2015/10/02 18:01:30| Creating Swap Directories
[root@server ~]#
please help me out
squid -z makes the directories
you then have to run the following:
service squid start
Hallo,my squid cache is empty.what could cause this?10x
Thank you very much, this article was very helpful for me.
Finally I have had recreated cache and started service.
In my previous tries to recreate cache directory I have had missed restoring security context.
But one remark to the command for restoring SeLinux security context, we must use ‘R’ recursive flag not an ‘r’ since commands and flags in linux are case sensitive.
Good Luck!
Another funny I got was when changing to coss cache_dir. Squiddy would for some unexplained reason not start, until I switched back to a folder based option (anyone really, e.g. ufs) and run
#> squid -z
to create the cache folders. Now switch to coss and squid starts! Seemed like it was looking for the directories although it was not going to use them under coss.
I was using squid 2.7 STABLE 5, 7, and 12 at the time on SLES 10 and 11.
When you create a cache, do not overfill the file system. If you have for instance 20GB partition, the cache should use less than 10GB. To quickly clear the cache you can then rename the cache directory, e.g. /var/cache/squid to /var/cache/squid1, recreate the first, chown as above, ‘squid -z’ for the sub directories and then start squid. So less than 10GB old cache can be deleted later in the background, but you have enough space to start growing a new cache.
Waiting for a rm command to finish off a 40GB cache often takes too much time (although only minutes) while squid is not running, hence the rename and recreate option. Rebuilding a cache of the same size could also take hours with no service to the users, so rather create a new folder to get going.
Hello..
After 2 hour searching in all posts in internet, i finally found in your post! Thanks!
Error :
Solution : (after create squid spool dir and squid caches dir)
# restorecon -vr /var/spool/squid
Thanks again!