歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> Squid+iptables的透明代理配置

Squid+iptables的透明代理配置

日期:2017/2/27 14:21:15   编辑:更多Linux
  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



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



Copyright © Linux教程網 All Rights Reserved