一:SNAT和DNAT原理 1.SNAT 和 DNAT #SNAT 配合POSTROUTING链使用,修改来源的私有地址为公网地址 #DNAT 配合PREROUTING链使用,修改数据报头目的地址为私有地址 2.vim /etc/sysctl.conf #开启iptables防火墙filter表里的FORWARD链 net.ipv4.ip_forward = 1 运行:sysctl -p命令使设置生效 二:实现要求 (1).内部服务器与:ftp(20,21)/http(80)/snmp(25)/ssh(22) (2).内部服务器能够解析域名和ping通域名或者IP地址 (3).能够访问NAT服务器上的ssh服务,其余服务都拒绝 三:脚本编写 #!/bin/bash # firewall.sh # <说明:本脚本没有启用内核的SYN Cookie模块,对于ping回应请求是通过防火墙的相应设置来阻挡># # NAT服务器(eth0 && eth1) # 1.外部网络使用eth0网卡,ip:10.10.54.151/24 # 2.内部网络使用eth1网卡,ip:172.31.31.254/24 # 内部服务器(eth0) # 网卡eth0 ip:172.31.31.51/24 gateway:172.31.31.254 ###########################第一部分:系统环境设置 # 相关变量设置 eth_pub="eth0" # public ip网卡 eth_pri="eth1" # private ip 网卡 ip_out="10.10.54.151" # NAT服务器对外共有ip innet="172.31.31.0/24" # 内部网络 ip_in="172.31.31.51" # 内部服务器私有ip export eth_pub eth_pri innet ip_out ip_in # 设置/etc/sysctl.conf 开启filter表的FORWARD链 sed -i "s/\(net.ipv4.ip_forward\) = 0/\1 = 1/g" /etc/sysctl.conf ##########################第二部分:针对NAT服务器本身的防火墙设置 # 1.清除规则,设置默认策略,开放lo接口 iptables -F iptables -X iptables -Z iptables -P INPUT DROP # NAT服务器INPUT链设置原则:关闭所有连接,开放特定服务 iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT iptables -A INPUT -i lo -j ACCEPT # loopback接口 # 2.启动额外的防火墙模块 if [ -f /usr/local/virus/iptables/iptables.deny ]; then sh /usr/local/virus/iptables/iptables.deny fi if [ -f /usr/local/virus/iptables/iptables.allow ]; then sh /usr/local/virus/iptables/iptables.allow fi if [ -f /usr/local/virus/httpd-err/iptables.http ]; then sh /usr/local/virus/httpd-err/iptables.http fi # 3.让NAT服务器通过主动向外发出请求而相应的数据包可以进入本机 iptables -t filter -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # 4.允许外部主机访问NAT服务器上的ssh服务,其余全部拒绝 iptables -t filter -A INPUT -p tcp --sport 1024:65534 --dport 22 -j ACCEPT # 5.仅允许内部服务器ping NAT服务器 iptables -A INPUT -i $eth_pri -p icmp -s $innet -j ACCEPT ##########################第三部分,与内部服务器有关的防火墙设置有关 # 1.清除nat表的规则,设置默认策略 iptables -F -t nat iptables -X -t nat iptables -Z -t nat iptables -t nat -P POSTROUTING ACCEPT iptables -t nat -P PREROUTING ACCEPT iptables -t nat -P OUTPUT ACCEPT # 2.内部服务器开放特定服务:ftp/http/snmp/ssh iptables -t nat -A PREROUTING -i $eth_pub -p tcp --sport 1024:65534 --dport 20 -j DNAT --to-destination ${ip_in}:20 # ftp/20 iptables -t nat -A PREROUTING -i $eth_pub -p tcp --sport 1024:65534 --dport 21 -j DNAT --to-destination ${ip_in}:21 # ftp/21 iptables -t nat -A PREROUTING -i $eth_pub -p tcp --sport 1024:65534 --dport 80 -j DNAT --to-destination ${ip_in}:80 # http/80 iptables -t nat -A PREROUTING -i $eth_pub -p tcp --sport 1024:65534 --dport 25 -j DNAT --to-destination ${ip_in}:25 # snmp/25 iptables -t nat -A PREROUTING -i $eth_pub -p tcp --sport 1024:65534 --dport 4000 -j DNAT --to-destination ${ip_in}:22 # ssh/22 # 3.内部服务器可以使用域名解析和ping iptables -t nat -A POSTROUTING -o $eth_pub -p udp --sport 1024:65534 --dport 53 -j SNAT --to-source ${ip_out} # 域名解析 iptables -t nat -A POSTROUTING -o $eth_pub -p icmp -j SNAT --to-source ${ip_out} # ping服 务 # 4.存储规则 /etc/init.d/iptables save
简单iptables规则设置
原创
©著作权归作者所有:来自51CTO博客作者kaxiou90的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:squid编译安装参数说明
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
详解网络知识:iptables规则
本文主要为大家详解介绍iptables规则。
IP iptables规则 云容器网络 -
iptables 规则策略设置
主机防火墙、网关防火墙的基础知识及应用
局域网 服务器 防火墙 基础知识 -
iptables原理及设置规则
iptables原理及设置规则图文汇总
IP 包过滤 HTTPS