ChinaUnix首页 > 精华文章 > Proxy服务器 > 正文

[原创] Squid+iptables的透明代理配置


http://www.chinaunix.net 作者:firer2000  发表于:2007-05-20 17:02:18
发表评论】 【查看原文】 【Proxy服务器讨论区】【关闭

Squid+iptables的透明代理配置
Firer2000
2003-06-27
硬件环境:兼容机 双网卡
软件环境:linux7.1+squid-2.3.STABLE4-src.tar
一.squid的安装配置
1.下载squid
  可以从squid主站下载:http://www.squid-cache.org
2.编译安装squid
  第一步:[root@www root]#tar xzvf squid-2.3.STABLE4-src.tar.gz
  第二步:[root@www root]#cd squid-2.3.STABLE4
  第三步:[root@www squid-2.3.STABLE4]#./configure --prefix=/usr/localsquid enable-ipf-transparent 

/*指定squid的安装目录和启用透明代理*/
  第四步:[root@www squid-2.3.STABLE4]#make all
  第五步:[root@www squid-2.3.STABLE4]#make install
以上五步执行完毕,squid整个程序就会被安装在/usr/local/squid目录下.接下来再执行以下几步:
  第六步:进入目录/usr/local,以root身份执行下面的命令,创建cache目录和改变整个squid目录的所有者为

nobody.nobody:
         [root@www squid-2.3.STABLE4]#cd /usr/local/squid
         [root@www squid]# mkdir cache
         [root@www squid]# cd ..
         [root@www local]# chown nobody.nobody -R squid
  第七步:改变用户为nobody,进入/usr/local/squid/bin目录,执行./squid -z创建cache交换目录
         [root@www local]# su nobody
         [root@www local]$cd /usr/local/squid/bin
         [root@www local]$./squid -z
  第八步:修改squid.conf文件,确保以下配置:
         httpd_accel_host virtual(记得把一句加上,我用的这个squid版本没有这一句)
         httpd_accel_port 80
         httpd_accel_with_proxy on
         httpd_accel_uses_host_header on
 cache_effective_user nobody
 cache_effective_group nobody
  http_access allow all
 cache_dir ufs /usr/local/squid/cache 100 16 256
         ...
  最后启动squid:
        [root@www local]#/usr/local/squid/bin/squid
  查看进程列表: 
        [root@www local]#px ax
  应该出现如下几个进程:
......... usr/local/squid
......... squid
......... unlink
 并且系统中应该有如端口被监听:
tcp 0 0 0.0.0.0:3128 0.0.0.0:* LISTEN
udp 0 0 0.0.0.0:3130 0.0.0.0:*
这些说明squid正常启动了.
###如果squid不能正常工作,可能是域名的设置问题,需要设置域名。
让系统启动时自动运行squid
编辑/ect/rc.d/local文件,在末尾加上:
su nobody -c "/usr/local/squid/bin/squid"
------------------------------------------------------------------------------------------------
OK,通过以上设置我们就以就squid代理上网了.
可以在ie浏览器中设置使用代理服务器,添入192.168.0.101:3128就可以上网了.
但这一步还没有实现透明代理,接下来我们开始设置iptables

二.设置iptables
1、 首先使用linuxconf工具将enable routing 项打开。在configure linuxconf modules 里选择firewall的项。
2、 #setup
进入services 去掉ipchains。
3、在/etc/rc.d/目录下用touch命令建立firewall文件,执行chmod u+x firewall以改变文件属性,编辑

/etc/rc.d/rc.local文件,在末尾加上/etc/rc.d/firewall以确保开机时能自动运行该脚本.
firewall内容为:
modprobe ip_tables
modprobe ip_nat_ftp
modprobe ip_conntrack
modprobe ip_conntrack_ftp
echo 1>;/proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -i eht1 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
iptables -t nat -A POSTROUTING -s 172.16.0.0/23 -o eth0 -j SNAT --to 211.141.67.10
##############################################################################
下面是原文给出的firewall脚本,共参考
#!/bin/sh
echo "Enable IP Forwarding..."
echo "1">;/proc/sys/net/ipv4/ip_forward
echo "Starting iptables rules..."
/sbin/modprobe iptable_filter
/sbin/modprobe ip_tables 
/sbin/modprobe iptable_nat
#Refresh all chains
/sbin/iptables -F -t nat
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -t nat -A PREROUTING -i eth1 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j SNAT --to a.b.c.d
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
#######################################################################################
其中,eth1是内部网卡,eth0是外部网卡。
内网ip地址为172.16.0.0/23 
eth0对应的ip地址为:211.141.67.10
结束.
然后设置网关和dns后就可上网了. 
如需要还可以添加一些防火墙以增强安全性,具体参考本站:
http://www.linuxaid.com.cn/engineer/bye2000/doc/iptables1.htm
http://www.linuxaid.com.cn/engineer/bye2000/doc/iptables2.htm
三:设置squid支持用户认证:
说明:透明代理和用户认证功能不能同时应用。否则用户认证功能不起作用。
假定squid source目录在/tmp/ squid-2.3.STABLE4
2. # cd /tmp/ squid-2.3.STABLE4/auth_modules/NCSA
# make ncsa_auth

3. 拷贝生成的执行文件ncsa_auth到squid执行文件目录
# cp ncsa_auth /usr/local/squid/bin

4. 从Apache软件包中得到程序htpasswd
假设apache软件安装在/var/www目录下
#cd /usr/www/bin
5.用htpasswd生成供Squid利用的用户名和密码认证数据库文件
生成的密码文件放在/usr/local/squid/etc 下。
 [root@mail bin]# ./htpasswd -c /usr/local/squid/etc/passwd test
New password:
Re-type new password:
Adding password for user test
这时在/usr/local/squid下就生成了passwd文件,并且加入了用户test。
6. 加其它更多的用户
#htpasswd /usr/loal/squid/etc/passwd newuser

7. 修改squid.conf,enable ncsa_auth用户认证功能

[root@linux etc]# more squid.conf | grep ncsa_auth
authenticate_program /usr/local/squid/bin/ncsa_auth /usr/local/squid/etc/passwd

8.修改ACL部分,象下面这样:
定义相关的用户类
acl auth_user proxy_auth REQUIRED
注意,REQUIRED关键字指明了接收所有合法用户的访问。
7.设置http_access
http_access allow auth_user
注意,如果你在改行中指定了多个允许访问的用户类的话,应该把要认证的用户类放在第一个。如下所示:
错误的配置:http_access allow auth_user all manager
正确的配置:http_access allow auth_user manager all



 firer2000 回复于:2003-07-04 15:36:02

有问题请指出~


 段誉 回复于:2003-07-07 17:57:42

暂时没有,长期关注。


 workaholic 回复于:2003-07-08 09:29:51

我也慢慢看着呢  :)


 firer2000 回复于:2003-07-09 13:42:21

引用:原帖由 "workaholic"]我也慢慢看着呢  :)
 发表:

    

呵呵,多谢关心呀!


 startdd 回复于:2003-07-11 02:28:54

支持!


 linuxs 回复于:2003-07-14 10:50:48

关注


 jzcjy 回复于:2003-08-26 12:35:42

如何实现在多网段下的支持。


 firer2000 回复于:2003-08-26 16:21:08

我采用的方法是安装多个网卡,然后在iptables的规则里面添加相应的项目。


 windywolf 回复于:2003-08-27 11:30:30

你说的
7. 修改squid.conf,enable ncsa_auth用户认证功能 
我用过,觉得不好,因为整个内网的机子,只要知道
用户口令就能使用代理了
我一直想用mac来限制机子,可是,用带enable-acl-arp
选项安装squid,始终在squid.conf中没有acl arp 条目出现
自己加又不生效,现在只能用限ip的方法了
不知道大家还有没有更好的管理方法(单指squid
不包括iptables)


 windywolf 回复于:2003-08-27 11:34:58

firer2000   
你的ftp网站密吗是什么呀
想上去看看


 firer2000 回复于:2003-08-28 10:11:03

你用的是那个版本的squid?用2.5的试试!


 firer2000 回复于:2003-08-28 10:11:38

我的ftp用户名和密码都是firer2000


 windywolf 回复于:2003-08-28 14:12:14

我用的是squid-2.5.STABLE3.tar.gz
这个版本应该没有什么问题吧
你能用acl advance arp xx.xx.xx.xx吗???


 firer2000 回复于:2003-08-29 07:49:59

这个我没有试过,你看看精华贴,里面有这方面的说明,我见过


 repairer 回复于:2003-09-05 22:18:43

请问通过以上设置,客户机可以用qq和msn吗?谢谢!


 antsnm 回复于:2003-09-06 19:23:22

我作了密码认证可是怎么就是通不过认证呢?是不是对加密进行设置?


 firer2000 回复于:2003-09-08 08:33:43

你是说输入用户名和密码后不能通过?


 lpy001 回复于:2003-09-10 08:53:23

root@localhost squid]# cd bin
[root@localhost bin]# ls
RunAccel  RunCache  squidclient
[root@localhost bin]# ./squid -z
bash: ./squid: No such file or directory
[root@localhost bin]# cd..
bash: cd..: command not found
[root@localhost bin]# cd..
bash: cd..: command not found
[root@localhost bin]# cd ..
[root@localhost squid]# cd ..
[root@localhost local]# su nobody
This account is currently not available

  在执行./squid和su nobody时出错;
1.cache目录是建在/usr/local下,是还是建在/usr/local/squid目录下?
2.chown nobody.nobody -R squid是什么意思?


 段誉 回复于:2003-09-11 13:06:37

缺省情况下,squid是在/usr/sbin目录下,至于cache目录,和你的配置文件——squid.conf有关系,在配置文件里面定义这个目录的位置。


 forx86 回复于:2003-10-03 09:10:29

"下面是原文给出的firewall脚本,共参考 
#!/bin/sh 
echo "Enable IP Forwarding..." 
echo "1">;/proc/sys/net/ipv4/ip_forward 
echo "Starting iptables rules..." 
/sbin/modprobe iptable_filter 
/sbin/modprobe ip_tables  
/sbin/modprobe iptable_nat 
#Refresh all chains 
/sbin/iptables -F -t nat 
iptables -P INPUT DROP 
iptables -P FORWARD DROP 
iptables -t nat -A PREROUTING -i eth1 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128 
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j SNAT --to a.b.c.d 
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT "
上边的iptables -P INPUT DROP 
这样用这个脚本能打开网页吗?试试这个脚本怎么打不开网页啊???
请老大指点。


 NetDC 回复于:2003-11-12 10:22:27

引用:原帖由 "firer2000" 发表:
iptables -P INPUT DROP 
iptables -P FORWARD DROP 



这里我不是很明白,INPUT和FORWARD规则都DROP掉了,nat还能用吗?虽然POSTROUTING 在INPUT之前,可是我当时做的一个nat好像不能把所有的INPUT都drop掉,要不nat就不能工作。

讨教一下,谢谢。


 paulwang 回复于:2004-05-21 10:06:11


这里我不是很明白,INPUT和FORWARD规则都DROP掉了,nat还能用吗?虽然POSTROUTING 在INPUT之前,可是我当时做的一个nat好像不能把所有的INPUT都drop掉,要不nat就不能工作。 

讨教一下,谢谢。


听说, INPUT, OUTPUT,FORWARD 这三条链对同一个数据包, 只有其中一条有效, 既NAT不关 INPUT和OUTPUT的事,
见别人的文章:http://www.linuxforum.net/doc/iptable-wu.html

另外:上面的解决方案中好象同时起用了透明代理和NAT吧, 不光是透明代理吧。

还有: 我很菜,请指教


 wingger 回复于:2005-02-03 15:51:33

引用:原帖由 "NetDC" 发表:


这里我不是很明白,INPUT和FORWARD规则都DROP掉了,nat还能用吗?虽然POSTROUTING 在INPUT之前,可是我当时做的一个nat好像不能把所有的INPUT都drop掉,要不nat就不能工作。

讨教一下,谢谢。



-p是缺省规则,表示,不区配的包使用的规则


 wingger 回复于:2005-02-04 13:37:17

引用:3、在/etc/rc.d/目录下用touch命令建立firewall文件,执行chmod u+x firewall以改变文件属性,编辑 

/etc/rc.d/rc.local文件,在末尾加上/etc/rc.d/firewall以确保开机时能自动运行该脚本. 
firewall内容为: 
modprobe ip_tables 
modprobe ip_nat_ftp 
modprobe ip_conntrack 
modprobe ip_conntrack_ftp 
echo 1>;/proc/sys/net/ipv4/ip_forward 
iptables -t nat -A PREROUTING -i eht1 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128 
iptables -t nat -A POSTROUTING -s 172.16.0.0/23 -o eth0 -j SNAT --to 211.141.67.10 
###########################################




引用:下面是原文给出的firewall脚本,共参考 
#!/bin/sh 
echo "Enable IP Forwarding..." 
echo "1">;/proc/sys/net/ipv4/ip_forward 
echo "Starting iptables rules..." 
/sbin/modprobe iptable_filter 
/sbin/modprobe ip_tables 
/sbin/modprobe iptable_nat 
#Refresh all chains 
/sbin/iptables -F -t nat 
iptables -P INPUT DROP 
iptables -P FORWARD DROP 
iptables -t nat -A PREROUTING -i eth1 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128 
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j SNAT --to a.b.c.d 
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT 
###########################################




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

有差别哦!我的MSN不通过代理就不能上线!为啥呢,要什么特别的设置吗


 xxd1019 回复于:2007-02-02 15:13:16

有没有单网卡的帖子????????????


 liheng 回复于:2007-05-20 17:01:07

引用:原帖由 forx86 于 2003-10-3 09:10 发表
"下面是原文给出的firewall脚本,共参考 
#!/bin/sh 
echo "Enable IP Forwarding..." 
echo "1">;/proc/sys/net/ipv4/ip_forward 
echo "Starting iptables rules..." ... 


他这个脚本是有一点问题,打不开网页也纯属正常
因为设置的 INPUT 为默认拒绝,而又没有 在 INPUT 规则链里设置相应的 ACCEPT 的,自然就 DROP 所有的数据包,包括客户端的请求包,所以呢,在这里要在 INPUT 规则链里增加相应的 ACCEPT 的就 OK 了


 liheng 回复于:2007-05-20 17:02:18

引用:原帖由 wingger 于 2005-2-3 15:51 发表


-p是缺省规则,表示,不区配的包使用的规则 


没有设置相应的  ACCEPT 匹配规则,还不是和 DROP 所有是一样的概念




原文链接:http://bbs.chinaunix.net/viewthread.php?tid=107156
转载请注明作者名及原文出处