跳至主要内容

iptables设置一例

只需要一个公网地址设在外接口上,内部用私网地址组网即可,在内部服务器提供Internet服务。


#!/bin/sh

#--------------------------------------------
#
# 外接口eth0,开放 vpn ssh
# 内接口eth1,绑定 dhcp dns squid
# 向内部服务器转发 ftp smtp www pop3
# 支持透明代理
#
# 胖头鱼:pangty@ta139.com
#
#--------------------------------------------

EXT_IF="eth0"
INT_IF="eth1"
EXT_IP="" #公网IP
INT_IP="" #内接口IP
SERVER_IP="" #内部服务器IP

# pptpd_vpn_service ssh
TRUSTED_LOCAL_TCP_PORT="1723 22"
TRUSTED_LOCAL_UDP_PORT="22"

# ftp-data ftp smtp http pop3
FWD_TCP_PORT="20 21 25 80 110"
FWD_UDP_PORT="20 21 25 80 110"

# load any special modules
modprobe ip_nat_ftp
modprobe ip_conntrack_ftp
modprobe ip_nat_irc
modprobe ip_conntrack_irc

# turn on ip forwarding
echo "1" > /proc/sys/net/ipv4/ip_forward

# setting up ip spoofing protection
for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo 1 > $f
done

# delete any existing chains
iptables -F -t filter
iptables -X -t filter
iptables -Z -t filter
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat

# setting up default policies
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT

#---------------------- filter ---------------------

# allow ping from internet
iptables -A INPUT -i $EXT_IF -p icmp -j ACCEPT

# enable local traffic
#------------------------------------------------------------------------
# iptables -A INPUT ! -i $EXT_IF -m state --state NEW -j ACCEPT
# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#
# iptables -A FORWARD ! -i $EXT_IF -m state --state NEW -j ACCEPT
# iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
#------------------------------------------------------------------------

iptables -N allowed
iptables -A allowed ! -i $EXT_IF -m state --state NEW -j ACCEPT
iptables -A allowed -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -A INPUT -j allowed
iptables -A FORWARD -j allowed

for PORT in $TRUSTED_LOCAL_TCP_PORT; do
iptables -A INPUT -i $EXT_IF -p tcp --dport $PORT -m state --state NEW -j
ACCEPT

done

for PORT in $TRUSTED_LOCAL_UDP_PORT; do
iptables -A INPUT -i $EXT_IF -p udp --dport $PORT -m state --state NEW -j
ACCEPT

done

#---------------------- nat ---------------------

# port forwarding
for PORT in $FWD_TCP_PORT; do
iptables -A FORWARD -i $EXT_IF -o $INT_IF -d $SERVER_IP
-p tcp --dport $PORT -m state --state NEW -j ACCEPT
iptables -t nat -A PREROUTING -d $EXT_IP
-p tcp --dport $PORT -j DNAT --to-destination $SERVER_IP
iptables -t nat -A POSTROUTING -d $SERVER_IP
-p tcp --dport $PORT -j SNAT --to-source $INT_IP
done

for PORT in $FWD_UDP_PORT; do
iptables -A FORWARD -i $EXT_IF -o $INT_IF -d $SERVER_IP
-p udp --dport $PORT -m state --state NEW -j ACCEPT
iptables -t nat -A PREROUTING -d $EXT_IP
-p udp --dport $PORT -j DNAT --to-destination $SERVER_IP
iptables -t nat -A POSTROUTING -d $SERVER_IP
-p udp --dport $PORT -j SNAT --to-source $INT_IP
done

# Transparent Proxy
iptables -t nat -A PREROUTING -i $INT_IF -p tcp --dport 80 -j REDIRECT --to-
port 3128


# SNAT or MASQUERADE
#------------------------------------------------------------------------
# iptables -t nat -A POSTROUTING -o $EXT_IF -j SNAT --to-source $EXT_IP
#------------------------------------------------------------------------
iptables -t nat -A POSTROUTING -o $EXT_IF -j MASQUERADE

# THE END

评论

此博客中的热门博文

4 steps to delete account in Gerrit DB

4 steps to delete account in DB. Delete from accounts where preferred_email=’’; delete from account_ssh_keys where account_id=''; delete from account_external_ids where external_id='gerrit:*’; delete from account_external_ids where external_id='username:*’; whatever it was in H2 database and postgres db . H2: ssh -p 24198 localhost gerrit gsql Postgres: psql

繁忙的周末

回了杭州,周五跟老爹吃夜宵,周六跟一群人玩,好乐迪9折加对折,然后夜宵,长肉。 跟老妈逛街。。花了我一千大洋,买了2件外套了受不了了。 结果。。唉。。去赶火车结果没坐上,改签,跟老妈发脾气。。让我多睡了10分钟结果拉下了火车。 然后做七点四十七的车,一路出战一路狂奔赶地铁。。 终于赶上了。 真不容易,还是要提早啊

mod

apache安装后,如果想再添加模块,往往不想重新安装一次,再者,我在安装中发现,并不是安装文件中所有的modules都会被默认安装,即使在安装中使用了 ./configure --prefix=/usr/local/apache --enable-so --enable-modules=all --enable-mods-shared=all也是如此。我使用上述方法安装的apache2.2.6就没有安装proxy相关模块。这时,需要手工生成so文件,但会自动的被复制到你的apache安装目录的modules中,然后修改apache的配置文件,加载相关模块,验证apache配置并重启服务即可。以上描述的具体操作步骤如下: 操作系统:linux redhat 4.5 ES apache版本:2.2.6 可自行下载tar包 1. 拷贝安装包到/usr/local,并在目标目录下进行解压。[root@localhost local]# cd /usr/local[root@localhost local]# tar -zxvf httpd-2.2.4.tar.gz2. 配置安装参数,安装的目标目录为/usr/local/apache,建议设置enable-mods-shared=all,不用的module注释掉就行了![root@localhost local]# cd httpd-2.2.4[root@localhost httpd-2.2.4]# ./configure --prefix=/usr/local/apache --enable-so --enable-mods-shared=all3. 编译并安装。[root@localhost httpd-2.2.4]# make[root@localhost httpd-2.2.4]# make install4. 启动Apache,并测试。[root@localhost httpd-2.2.4]# cd /usr/local/apache/bin[root@localhost bin]# ./apachectl start 打开浏览器,并在地址栏中输入主机的IP地址如 http://192.168.1.254/ 如果出现以下画面则说明安装成功。It works! 注意:安装完成后检查系统服务中的HTTPD服务是否已...