RedHat7.2下使用IPTABLES實現IP轉發
RedHat7.2下使用IPTABLES實現IP轉發其實挺簡單的,只要你按照下面文章一步一步走就行!
1.內核編譯
一般來講,透明代理所在機器往往是帶動整個局域網聯入互聯網的入口,因此該機器往往需要配置防火墻規則以對內部網絡進行防護。因此在編譯內核時也許要考慮將防火墻支持選項編譯進去。
rpm -q kernel-headers kernel-source make dev86 #檢查RPM包,如果不齊的補齊。
cd /usr/src/linux-xxxx #進入內核所在目錄
一般來說需要在使用make menuconfig命令配置時打開如下選項:
- [*]Networking support
- [*]Sysctl support
- [*]Network packet filtering
- [*]TCP/IP networking
- [*]/proc filesystem support
- [*] Kernel/User netlink socket
- [*] Netlink device emulation
上面的部原本上幾乎不用改,以下部份在IP: Netfilter Configuration
- [*] Connection tracking (required for masq/NAT)
- [*] FTP protocol support
- [*] IP tables support (required for filtering/masq/NAT)
- <*> limit match support
- [*] MAC address match support
- [*] Netfilter MARK match support
- [*] Multiple port match support
- [*] TOS match support
- [*] Connection state match support
- [*] Packet filtering
- [*] REJECT target support
- [*] Full NAT
- [*] MASQUERADE target support
- [*] REDIRECT target support
- [*] Packet mangling
- [*] TOS target support
- [*] MARK target support
- [*] LOG target support
然后make dep ; make clean ;make bzImage命令來編譯內核。如果使用到了模塊,還需要使用下面命令生成和安裝模塊make modules;make modules-install。
安裝新內核后重新起動
另:/etc/sysconfig/network 為:
NETWORKING=yes
HOSTNAME="gateway"
FORWARD_IPV4=true
GATEWAYDEV=eth1
#p#
2.iptables的設置
在/etc/rc.d/目錄下用touch命令建立firewall文件,執行chmod u+x firewall以更改文件屬性,編輯/etc/rc.d/rc.local文件,在末尾加上/etc/rc.d/firewall以確保開機時能自動執行該腳本?! irewall文件內容為:
#!/bin/sh
echo "Enabling IP Forwarding..."
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "Starting iptables rules..."
#Refresh all chains
/sbin/rmmod ipchains
/sbin/modprobe ip_tables
/sbin/modprobe iptable_filter
/sbin/modprobe iptable_nat
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
/sbin/modprboe ip_nat_ftp
/sbin/iptables -F INPUT
/sbin/iptables -F FORWARD
/sbin/iptables -F POSTROUTING -t nat
/sbin/iptables -P FORWARD DROP
/sbin/iptables -t nat -A POSTROUTING -o eth1 -s 192.168.1.0/24 -j MASQUERADE #eth1為接Internet的網卡
#/sbin/iptables –t nat –A POSTROUTING –o ppp0 –j MASQUERADE #用于ppp0撥號
/sbin/iptables -A FORWARD -i eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/iptables -A FORWARD -s 192.168.1.0/24 -j ACCEPT
通過正文的介紹和了解,大家都能在自己的RedHat7.2系統下使用IPTABLES來實現IP轉發了,感興趣的朋友可以試試!
【編輯推薦】
- iptables-restore中文手冊
- 一些iptables的常用應用
- 動態iptables防火墻之dynfw
- Linux防火墻的IPtables設置與用法
- 巧用iptables建立Linux防火墻
- 使用iptables封殺內網的bt軟件
- 用iptables來防止web服務器被CC攻擊