当前位置: > Linux服务器 > iptables >

iptable+squid实现双网卡上网

时间:2015-01-19 17:22来源:www.it.net.cn 作者:IT

===================================================
服务器:
debian(hiweed)
双网卡配置/etc/network/interface:
auto lo eth0 eth1
iface lo inet loopback
iface eth0 inet static
address 210.**.**.**
netmask 255.255.255.0
network 210.**.**.0
gateway 210.**.**.254

iface eth1 inet static
address 192.168.0.1
netmask 255.255.255.0

客户机:
window xp
网络设置:ip 192.168.0.2
gateway 192.168.0.1
dns 202.38.64.1
====================================================

squid的安装和配置:
从Squid的官方站点http://www.squid-cache.org下载该软件; 

1) 将该文件拷贝到/usr/local目录。 

2) 解开该文件 tar xvzf squid-2.3.STABLE4-src.tar.gz。 

3) 解开后,在/usr/local生成一个新的目录squid-2.3.STABLE4 

4) 进入squid 
cd squid-2.3.STABLE4 

5) 执行./configure 可以用./confgure --prefix=/directory/you/want指定安装目录。系统缺省安装目录为/usr/local/squid。 

6) 执行 make all 

7) 执行 make install 

Squid配置文件为:/usr/local/squid/etc/squid.conf。安装成功以后,系统已经有了一个缺省的配置文件,用户仅仅需要修改该配置文件即可。首先我将Squid用在透明代理时的配置文件中必须打开的选项的内容列举如下: 
http_port 8080 
cache_mem 32 MB 
cache_swap_low 90 
cache_swap_high 95 
maximum_object_size 4096 KB 
cache_dir ufs /usr/local/squid/cache 1200 16 256 
cache_access_log /usr/local/squid/logs/access.log 
cache_log /usr/local/squid/logs/cache.log 
dns_nameservers 210.12.114.130 
unlinkd_program /usr/local/squid/bin/unlinkd 
acl all src 0.0.0.0/0.0.0.0 
http_access allow all 
cache_effective_user nobody 
cache_effective_group nobody 
httpd_accel_host virtual 
httpd_accel_port 80 
httpd_accel_with_proxy on 
httpd_accel_uses_host_header on 
visible_hostname 210.**.**.**

然后增加nobody用户及组:
groupadd nobody
useradd -g nobody -d /usr/local/squid/var/cache nobody
修改/etc/services建立端口3128的tcp侦听,加上一行:
squid 3128/tcp #squid proxy

然后运行squid,利用ps -aux |grep squid看系统中是否出现进程。

在客户机中ie链接中设置代理210.**.**.**,端口3128,然后如果可以访问外面网络,则squid配置成功。
====================================================

问题:
每次重新启动后,不能自动运行squid,需要
#su nobody
$/usr/local/squid/sbin/squid -z
$/usr/local/squid/sbin/squid 
后,客户机才可以访问??

下面配置透明代理:iptables
编写一个开机脚本: /etc/rc.local

echo 1 > /proc/sys/net/ipv4/ip_forward

/sbin/modprobe  ip_tables

/sbin/modprobe  iptable_filter

/sbin/modprobe  iptable_nat

/sbin/modprobe  ip_conntrack

/sbin/modprobe ip_conntrack_ftp

/sbin/iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE

然后,运行
touch /etc/rc.local

chmod 755 /etc/rc.local

cd /etc/rcS.d

ln -s ../rc.local ./S99rc.local

这样每次启动脚本自动运行.

==============================================

其实不需要配置squid,如果只希望两台双机互连的机器上网,而不做任何限制,对squid是没有需求的,只要配置rc.local脚本就可以了。

要注意的是,我的linux没有开dns,因此我的子机的dns我开始设定的是主机的子网ip(192.168.0.1), 此时访问ip的网页正常,无法进行dns解析,将子机的dns设为学校的dns地址202.38.64.1后,一切正常


(责任编辑:IT)
------分隔线----------------------------