Ubuntu iptables 防火墙设置
新增 iptables 目录与设置档
$ mkdir /etc/iptables
$ vim /etc/iptables/iptables.rule
===========以下是 iptables.rule 的内容================
#!/bin/bash
# 请先输入您的相关参数,不要输入错误了!
EXTIF="eth0" # 这个是可以连上 Public IP 的网络介面
INIF="" # 内部 LAN 的连接介面;若无请填 ""
INNET="" # 内部 LAN 的域,若没有内部 LAN 请设置为 ""
export EXTIF INIF INNET
# 第一部份,针对本机的防火墙设置!###########################
# 1. 先设置好核心的网络功能:
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
for i in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo "1" > $i
done
for i in /proc/sys/net/ipv4/conf/*/log_martians; do
echo "1" > $i
done
for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do
echo "0" > $i
done
for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo "0" > $i
done
for i in /proc/sys/net/ipv4/conf/*/send_redirects; do
echo "0" > $i
done
# 2. 清除规则、设置默认政策及开放 lo 与相关的设置值
PATH=/sbin:/usr/sbin:/bin:/usr/bin; export PATH
iptables -F
iptables -X
iptables -Z
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# 3. 启动额外的防火墙 script 模块
if [ -f /etc/iptables/iptables.deny ]; then
sh /etc/iptables/iptables.deny
fi
if [ -f /etc/iptables/iptables.allow ]; then
sh /etc/iptables/iptables.allow
fi
if [ -f /etc/iptables/iptables.http ]; then
sh /etc/iptables/iptables.http
fi
# 4. 允许某些类型的 ICMP 封包进入
AICMP="0 3 3/4 4 8 11 12 14 16 18"
for tyicmp in $AICMP
do
iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
done
# 5. 允许某些服务的进入,请依照您自己的环境开启
iptables -A INPUT -p TCP -i $EXTIF --dport 22 -j ACCEPT # SSH
#iptables -A INPUT -p TCP -i $EXTIF --dport 21 -j ACCEPT # FTP
#iptables -A INPUT -p TCP -i $EXTIF --dport 25 -j ACCEPT # SMTP
# iptables -A INPUT -p UDP -i $EXTIF --sport 53 -j ACCEPT # DNS
# iptables -A INPUT -p TCP -i $EXTIF --sport 53 -j ACCEPT # DNS
iptables -A INPUT -p TCP -i $EXTIF --dport 80 -j ACCEPT # HTTP
#iptables -A INPUT -p TCP -i $EXTIF --dport 443 -j ACCEPT # HTTPS
# iptables -A INPUT -p TCP -i $EXTIF --dport 110 -j ACCEPT # POP3
#iptables -A INPUT -p TCP -i $EXTIF --dport 3128 -j ACCEPT # proxy
#iptables -A INPUT -p TCP -i $EXTIF --dport 1723 -j ACCEPT # vpn
#iptables -A INPUT -p gre -i $EXTIF -j ACCEPT
#iptables -A INPUT -p TCP -i $EXTIF --dport 3306 -j ACCEPT # MySQL
# 第二部份,针对后端主机的防火墙设置!##############################
# 1. 先载入一些有用的模块 (好像都不能用...先注解掉)
# modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack ip_conntrack_ftp ip_conntrack_irc"
for mod in $modules
do
testmod=`lsmod | grep "^${mod} " | awk '{print $1}'`
if [ "$testmod" == "" ]; then
modprobe $mod
fi
done
# 2. 清除 NAT table 的规则吧!
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
# 3. 若有双网卡开放成为路由器,且为 IP 分享器
if [ "$INIF" != "" ]; then
iptables -A INPUT -i $INIF -j ACCEPT
echo "1" > /proc/sys/net/ipv4/ip_forward
if [ "$INNET" != "" ]; then
for innet in $INNET
do
iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE
done
fi
fi
# 如果你的 MSN 一直无法连线,或者是某些网站 OK 某些网站不 OK,
# 可能是 MTU 的问题,那你可以将底下这一行给他取消注解来启动 MTU 限制范围
# iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss \
# --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu
# 4. NAT 服务器后端的 LAN 内对外之服务器设置
# iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 \
# -j DNAT --to 192.168.1.210:80
# 有 VPN 要设置这个
#iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE
#iptables -A FORWARD -s 192.168.0.0/24 -p tcp -m tcp --tcp-flags SYN,RST SYN \
# -j TCPMSS --set-mss 1200
# 5. 特殊的功能,包括 Windows 远端桌面所产生的规则,假设桌面主机为 1.2.3.4
# iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4 --dport 6000 \
# -j DNAT --to-destination 192.168.100.10
# iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4 --sport 3389 \
# -j DNAT --to-destination 192.168.100.20
echo 'iptables has changed'
============================================
# 执行让设置生效
$ sh /etc/iptables/iptables.rule
# 设置开机时自动执行一次设置档
$ vim /etc/rc.local
加上
sh /etc/iptables/iptables.rule
如果要建立允许与拒绝连线清单的话
$ vim /etc/iptables/iptables.allow
=========iptables.allow的内容================
#!/bin/bash
# 一律允许进入的主机地址
iptables -A INPUT -i $EXTIF -s 140.112.175.130 -j ACCEPT
=============================================
$ vim /etc/iptables/iptables.deny
=========iptables.deny的内容=================
#!/bin/bash
# 一律阻挡进入的主机地址
# iptables -A INPUT -i $EXTIF -s xxx.xxx.xxx.xxx -j DROP
=============================================
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
