设为首页 收藏本站
查看: 1549|回复: 0

[经验分享] ubuntu下的squid

[复制链接]

尚未签到

发表于 2015-11-19 10:02:52 | 显示全部楼层 |阅读模式
1.安装linux系统,选择自定义安装,选择开发工具包,webSERVER服务,去掉squid,安装的时候把SELINUX选成DISABLE

你也可以不用做以下的步骤,用我提供的脚本安装一下的一些配置!!!setup.tar.bz2拷贝到
/root目录下,然后用
tar –jxvf setup.tar.bz2
cd ./setup
sh ./setup
这三个命令就可以完成下面的配置了.安装完毕后系统会重新启动,启动后进入系统,配置ipmacfile文件(ipmac地址绑定文件)和初始化你用户和密码(具体请看服务器使用说明)


2.下载源代码并解压

cd /usr/src
wget -c http://www.squid-cache.org/Versions/v2/2.5/squid-2.5.STABLE14.tar.gz
tar -zxvf squid-2.5.STABLE14.tar.gz


3.安装Squid Proxy Server

cd /usr/src/squid-2.5.STABLE14

./configure --prefix=/usr/local/squid --sysconfdir=/etc/squid --bindir=/usr/bin --sbindir=/usr/sbin --mandir=/usr/share/man --enable-gnuregex --enable-carp --enable-async-io=80 --enable-removal-policies=heap,lru --enable-icmp --enable-delay-pools --enable-useragent-log --enable-referer-log --enable-kill-parent-hack --enable-snmp --enable-arp-acl --enable-htcp --enable-cache-digests --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese" --enable-poll --enable-linux-netfilter --disable-ident-lookups --enable-underscores --enable-auth="basic" --enable-basic-auth-helpers="NCSA" --enable-external-acl-helpers="ip_user" --enable-x-accelerator-vary

make && make install


4.设置squid.conf

cd
mv /etc/squid/squid.conf /etc/squid/squid.conf.bak
vi /etc/squid/squid.conf  #在按下i
==============================/etc/squid/squid.conf=========================
http_port 100.100.100.1:808
cache_mem 64 MB
dns_nameservers 202.98.0.68 202.98.5.68
visible_hostname 100.100.100.1
cache_mgr murong@ccds.gov.cn
cache_dir aufs /usr/local/squid/var/cache 25000 16 256
error_directory /usr/local/squid/share/errors/Simplify_Chinese
emulate_httpd_log on
cache_store_log none
logfile_rotate 1
hierarchy_stoplist cgi-bin ?
hierarchy_stoplist -i ^https:// ?
acl QUERY urlpath_regex -i cgi-bin /? /.asp /.php /.jsp /.cgi
acl denyssl urlpath_regex -i ^https://
no_cache deny QUERY
no_cache deny denyssl
auth_param basic program /usr/local/squid/libexec/ncsa_auth /etc/squid/passwd.sq
auth_param basic children 10
auth_param basic realm proxy.linuxserver.com
auth_param basic credentialsttl 2 hours
auth_param basic casesensitive off
refresh_pattern ^ftp:             1440       20% 10080
refresh_pattern ^gopher: 1440       0%  1440
refresh_pattern .            0     20% 4320
acl all src 0.0.0.0/0.0.0.0
acl OverConnLimit maxconn 5
acl auth_user proxy_auth REQUIRED
#acl macaddress arp 00:16:36:05:a6:54
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443 563
acl Safe_ports port 80           # http
acl Safe_ports port 21           # ftp
acl Safe_ports port 443 563   # https, snews
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
http_access allow auth_user
http_access deny OverConnLimit
#http_access allow macaddress
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access deny all
http_reply_access allow all
icp_access allow all
coredump_dir /usr/local/squid/var/cache
===============================脚本结束================================


5.初始化cache目录和启动squid

cd
htpasswd -cb /etc/squid/passwd.sq proxy proxy
chmod 666 /etc/squid/passwd.sq
mkdir -p /usr/local/squid/var/cache
cd /usr/local/squid/var
chown -R nobody.nobody cache logs
squid -z
squid start
squid -k parse        #检查配置是否有错误,没有任何输出,证明配置基本是正确的!!!
squid -zX               #可以加一个X参数,看看初始化具体的项目.   
如果因为DNS无法启动squid,可以用下面的命令启动squid
squid -D


6.安装squid密码修改工具

修改/etc/httpd/conf/httpd.conf的内容,"AddDefaultCharset" 设置成 GB2312,WEB显示中文.
cd /usr/src
vi /var/www/html/index.html

============================/var/www/html/index.html=======================<a href=&quot;../cgi-bin/chpasswd.cgi&quot;>请单击这里修改您的用户密码</a>
===================================配置结束============================

wget -c http://jaist.dl.sourceforge.net/sourceforge/orsochpasswd/chpasswd-2.2.3.tar.gz
wget -c http://sarg.sourceforge.net/htoi.patch
tar -zxvf chpasswd-2.2.3.tar.gz
cd /usr/src/chpasswd-2.2.3
patch -p0 < ../htoi.patch
./configure --enable-language=Chinese --enable-cgidir=/var/www/cgi-bin
make && make install
vi /usr/local/etc/chpasswd/chpasswd.conf

=================/usr/local/etc/chpasswd/chpasswd.conf=========================
password_file /etc/squid/passwd.sq
=================================脚本结束==============================


7.防火墙设置及IP地址与MAC地址绑定

touch /usr /sbin/firewall
chmod u+x /usr/sbin/firewall
vi /usr/sbin/firewall

===============================/usr/sbin/firewall===========================
#!/bin/sh

UPLINK=&quot;eth0&quot;
LANLINK=&quot;eth1&quot;
INTERFACES=&quot;lo eth0 eth1&quot;

case &quot;$@&quot; in
start)
echo -n &quot;Starting firewall...&quot;
iptables -F
iptables -X
iptables -Z
iptables -t nat -F
iptables -t nat -X
iptables -t nat -Z
iptables -t mangle -F
iptables -t mangle -X
iptables -t mangle -Z
iptables -P INPUT DROP

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#enable public access to certain services
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -i ! ${UPLINK} -m state --state NEW -j ACCEPT
iptables -A INPUT -p tcp -i ${UPLINK} -j REJECT --reject-with tcp-reset

while read ipaddr macaddr
do
iptables -A INPUT -s ${ipaddr} -m mac --mac-source ${macaddr} -j ACCEPT
done < /etc/ipmacfile

#enable system-log
#iptables -A INPUT -j LOG --log-prefix &quot;bad input:&quot;
#iptables -A INPUT -p udp -i ${UPLINK} -j REJECT --reject-with icmp-port-unreachable

#explicitly disable ECN
if [ -e /proc/sys/net/ipv4/tcp_ecn ]
then
echo 0 > /proc/sys/net/ipv4/tcp_ecn
fi

#disable spoofing on all interfaces
for x in ${INTERFACES}
do
echo 1 > /proc/sys/net/ipv4/conf/${x}/rp_filter
done

echo &quot;OK!&quot;
exit 0
;;
stop)
echo -n &quot;Stopping firewall...&quot;
iptables -F
iptables -X
iptables -Z
iptables -t nat -F
iptables -t nat -X
iptables -t nat -Z
iptables -t mangle -F
iptables -t mangle -X
iptables -t mangle -Z
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
echo &quot;OK!&quot;
exit 0
;;
restart)
$0 stop
$0 start

;;
show)
clear
echo &quot;>------------------------------------------------------&quot;
iptables -nvL
echo &quot;>------------------------------------------------------&quot;
iptables -t nat -nvL
exit 0
;;
*)
echo &quot;Usage: $0 {start|stop|restart|show}&quot;
exit 1
esac
=================================脚本结束==============================
:
建立IP地址与MAC地址对应关系表
touch /etc/ipmacfile
vi /etc/ipmacfile
ipmacfile文件格式
100.100.100.100 00:16:36:05:a6:54
100.100.100.101 00:16:36:05:a6:53

8.配置系统日志查看
cd
mkdir /var/www/html/logs
touch /usr/sbin/sqbak
chmod u+x /usr/sbin/sqbak
vi /etc/squid/index.html

===============================/etc/squid/index.html========================
<H1>客户端网络访问日志</H1>
<HR WIDTH=&quot;100%&quot;>
======================================================================

cp /etc/squid/index.html /var/www/html/logs/index.html

===============================/usr/sbin/sqbak============================
#!/bin/sh
logdir=/usr/local/squid/var/logs/
fl=access.log.0
bakpath=/logbak/
wwwpath=/var/www/html/logs/
sysdate=`date +%F`
day=`date +%d`
#lanip=`ifconfig eth1 | awk '/inet/{print $2}' | awk -F: '{print $2}' | grep -v ^$`
if [ &quot;$day&quot; = &quot;01&quot; ]
then
rm -f /var/www/html/logs/*
cp /etc/squid/index.html ${wwwpath}
fi
if [ -s &quot;${logdir}&quot;&quot;${fl}&quot; ]
then
cd ${bakpath}
mv ${logdir}${fl} ${sysdate}.log
cat ${sysdate}.log | awk '{print NR &quot; &quot; substr($4,2) &quot; &quot; $3 &quot; &quot; $1 &quot; &quot; $7 &quot; &quot; $11 &quot;/n&quot;}' | awk '{printf &quot;%-16s %-24s %-16s %-24s %-150s %-s/n&quot;,$1,$2,$3,$4,$5,$6}' > ${sysdate}.txt
tar -cvf ${sysdate}.tar ${sysdate}.txt
bzip2 -f ${sysdate}.tar
cp ${sysdate}.txt ${wwwpath}
echo &quot;<br><a href=../logs/$sysdate.txt>[$sysdate]</a></br>&quot; >> ${wwwpath}index.html
rm -f ${sysdate}.log ${sysdate}.txt
fi
======================================================================

crontab -u root -e
======================================================================
01 0 * * * /usr/sbin/squid -k rotate
15 0 * * * /usr/sbin/sqbak
======================================================================


9.设置启动项目

vi /etc/rc.d/rc.local

/usr/sbin/squid start
/sbin/firewall start
/usr/sbin/httpd start
10.批量生成用户名密码脚本

脚本一.

touch /usr/sbin/uuadd
chmod u+x /usr/sbin/uuadd
vi /usr/sbin/uuadd

=========================/usr/sbin/uuadd==================================
#!/bin/sh
#uuadd: batch add squid user.
#install step.
#chmod u+x uuadd
#./uuadd userfile
if [ $# -lt 1 ]
then
echo &quot;Usage: Please set user's name file.&quot;
echo &quot;Exmple: `basename $0` userfile.&quot;
echo &quot;######userfile######&quot;
echo &quot;      username1&quot;
echo &quot;      username2&quot;
echo &quot;      username3&quot;
echo &quot;          .&quot;
echo &quot;          .&quot;
echo &quot;          .&quot;
echo &quot;      usernamen&quot;
echo &quot;#####################&quot;
exit 1
fi
userfile=$1
uuadd=/usr/bin/htpasswd
while read line
do
$uuadd -b /etc/squid/passwd.sq $line $line
done < $userfile
=============================脚本结束==================================
:
userfile文件内容的格式
username1
username2
    .
    .
    .
usernamen
脚本二.

touch /usr/sbin/upadd
chmod u+x /usr/sbin/upadd
vi /usr/sbin/upadd

===================================/usr/sbin/upadd========================
#!/bin/sh
#upadd: batch add squid user.
#install step.
#chmod u+x upadd
#./upadd userfile
if [ $# -lt 2 ]
then
echo &quot;usage: $0 userfile&quot;
exit 1
fi
userfile=$1
upadd=/usr/bin/htpasswd
while read username passwd
do
$upadd -b /etc/squid/passwd.sq $username $passwd
done < $userfile
=================================脚本结束==============================
:
userfile文件内容的格式
username1 passwd1
username2 passwd2


请帮用户名设置为小写字母,批量生成的密码与用户名相同

单独更改用户密码:
htpasswd -b /etc/squid/passwd.sq 用户名 密码










Squid proxy server 使用FAQ


1.怎样添加新用户?

htpasswd -b /etc/squid/passwd.sq username password

说明:htpasswd工具生成用户名为username密码为password的用户,保存在用户数据库/etc/squid/passwd.sq.

2.怎样批量添加新用户?

1)      生成用户名和密码一样,然后用户自己更改

uuadd userfile

说明:uuadd命令批量生成密码与用户名一样的初始用户,用此命令时请指定userfile文件,userfile文件格式如下,是多行单列的内容,请注意在初始化时,如果用户名里包含大写字母的,初始化时密码被初始成小写.
:
userfile文件内容如下
username1
username2
    .   
    .               
Usernamen

最好是所有用户都用小写字母的用户名!!!
注意:
当下次在想批量添加用户时请指定你新的用户文件,因为如果你在还用以前的userfile文件的话,那么原来你的用户数据将被重新初始化.所以请每次批量添加玩用户时,把此文件删除!!!   

2)生成用户自己设的的用户名和密码
upadd userfile

说明:upadd命令批量生成密码与用户名,用此命令时请指定userfile文件,userfile文件格式如下,是多行单列的内容,请注意在初始化时,如果用户名里包含大写字母的,初始化时密码被初始成小写.
:
userfile文件内容的格式
username1 passwd1
username2 passwd2
3.用户初始化密码的更改和当用户想更换密码怎么办?

IE浏览器里输入http://服务器ip地址 点击&quot;请单击这里修改您的代理服务器密码&quot;,看说明修改你的密码!!!

4.如果有用户忘记自己的密码怎么办?

请用第1个问题里方法给用户设置密码.

5.怎样查看系统日志?

请在IE浏览器里输入http://服务器ip/logs 能够查看一个月内的用户上网日志.
如果想查看全部的用户上网日志请用WINSCP连接上服务器,在目录/logbak目录下是以日期命名的日志.请你下载想看的日志到本地机器.然后用WINRAR解压后,用记事本即可查看日志了!


6.如果客户端上不去网怎么办?
1)
可以重新启动服务器 用命令 reboot或者shutdown -r now

2)  可能是硬盘空间不足,系统无法写入日志而导致代理服务器进程无法启动,请把/logbak目录下的日志文件拷贝到别的机器上,再把此目录里的文件删除,然后用reboot命令重新启动系统.

3)
也可能是突然断电,导致系统文件丢失,这中情况一般是不会发生,应为LINUX有自我修复功能.如果真的启动不起来,那么请用一张别的LINUX启动光盘启动,把系统里面的重要数据备份出来.重新安装系统吧!!!
要备份的文件包括:用户数据库文件/etc/squid/passwd.sq   系统日志文件/logbak目录下的所有文件和/usr/local/squid/var/logs/access.log文件 /etc/ipmacfile ip地址与mac地址绑定文件

4)网络不通,请检查网络状况

5)硬件系统出现故障,请检查内存,网卡等

6ipMAC的地址绑定问题

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-141062-1-1.html 上篇帖子: squid+icap测试配置 下篇帖子: squid 日誌
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表