iptables基础

iptables----4张表----5个链---规则(过滤/控制作用)

-n 数字
-L 列表
-t 指定表 默认filter

加载如下模块到linux内核
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state

lsmod |egrep "filter|nat"

iptables - F 清除所有规则
iptables -X 删除自定义的链
iptables -Z 清除计数器

-A 在末尾行添加规则
-C 检查
-D 删除
-I 在第一条添加规则
-s 指定源地址处理
-i 指定进入接口 INPUT
-o(小写)指定 出去接口 OUTPUT

iptables -t filter -A INPUT -p tcp --dport 22 -j DROP #自杀
iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT

--dport 目的端口
--sport 源端口
-j 行为
行为包括(DROP丢弃)(ACCEPT接受)(REJECT拒绝)

iptables -nL --line-numbers 显示规则序列号

iptables -t filter -D INPUT 2 删除第二行

/etc/init.d/iptables restart(用iptables命令行配置的命令都是临时生效)。

iptables -t filter -A INPUT -i eth0 -p icmp -s 10.0.0.8 -j DROP #指定进入网卡为eth0 源地址为.8

取反
[root@oldboy ~]# iptables -t filter -A INPUT -i eth0 -p icmp ! -s 10.0.0.8 -j DROP
[root@oldboy ~]# iptables -t filter -I INPUT -i eth0 -p icmp ! -s 10.0.0.10 -j DROP

匹配主机源IP,目的IP
iptables -A INPUT -s 10.0.0.14
iptables -A INPUT -s ! 10.0.0.14
iptables -t nat -A PREROUTING -d 10.0.0.14
iptables -t nat -A PREROUTING -d ! 10.0.0.14

匹配网段
iptables -A INPUT -s 10.0.0.0/24
iptables -A INPUT -s ! 10.0.0.0/24

匹配单一端口
iptables -A INPUT -p tcp --sport 53
iptables -A INPUT -p udp --dport 53

匹配指定端口之外的端口
iptables -A INPUT -p tcp --dport ! 22
iptables -I INPUT -p tcp ! --dport 22 -s 10.0.0.123 -j DROP

匹配端口范围:
iptables -I INPUT -p tcp -m multiport --dport 21,22,23,24 -j ACCEPT <==次选
iptables -I INPUT -p tcp --dport 3306:8809 -j ACCEPT
iptables -I INPUT -p tcp --dport 18:80 -j DROP <==最佳
iptables -I INPUT -p tcp --dport 21,22,23,24 -j ACCEPT <==错误语法

●7.允许关联的状态包通过(web服务不要使用FTP服务)
#others RELATED ftp协议
#允许关联的状态包
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

限制指定时间包的允许通过数量及并发数
-m limit --limit n/{second/minute/hour}:
指定时间内的请求速率"n"为速率,后面为时间分别为:秒、分、时
--limit-burst [n]:
在同一时间内允许通过的请求"n"为数字,不指定默认为5
[root@nginx01 ~]# iptables -I INPUT -s 10.0.1.0/24 -p icmp --icmp-type 8 -m limit --limit 5/min --limit-burst 2 -j ACCEPT

=======================================
配置防火墙

iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/24 -j ACCEPT

[root@oldboy ~]# iptables -F
[root@oldboy ~]# iptables -X
[root@oldboy ~]# iptables -Z
[root@oldboy ~]# iptables -nL
[root@oldboy ~]# iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/24 -j ACCEPT
[root@oldboy ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@oldboy ~]# iptables -A INPUT -p tcp -s 192.168.1.0/24 -j ACCEPT
[root@oldboy ~]# iptables -A INPUT -p tcp -s 172.16.1.0/24 -j ACCEPT
[root@oldboy ~]# iptables -P INPUT DROP

iptables -P FORWARD DROP
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT

iptables -A INPUT -i lo -j ACCEPT #允许回环端口访问

iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p icmp -s 10.0.0.0/24 -m icmp --icmp-type any -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

nmap 10.0.0.19 -p 1-65535 ç时间很长
回执:
Starting Nmap 5.51 ( http://nmap.org ) at 2013-12-21 12:08 CST
Nmap scan report for C58-server-C (10.0.0.19)
Host is up (0.00028s latency).
Not shown: 65533 filtered ports
PORT STATE SERVICE
80/tcp open http

/etc/init.d/iptables save #保存
iptables-save >/etc/sysconfig/iptables #保存

=====================================
考试题:

局域网共享上网(nat表的POSTROUTING链(内网上外网)

db-01上:
ifdown eth0
route add default gw 172.16.1.5
route -n

lb-01上:
[root@lb01 ~]# vim /etc/sysctl.conf
[root@lb01 ~]# grep forward /etc/sysctl.conf

Controls IP packet forwarding

net.ipv4.ip_forward = 1
[root@lb01 ~]# sysctl -p
net.ipv4.ip_forward = 1
#地址转换
iptables -t nat -A POSTROUTING -o eth0 -s 172.16.1.0/24 -j SNAT --to-source 10.0.0.5
#查看
iptables -nL -t nat
vim /etc/resolv.conf

局域网共享的两条命令方法:
方法1:适合于有固定外网地址的:
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.8
(1)-s 172.16.1.0/24 办公室或IDC内网网段。
(2)-o eth0 为网关的外网卡接口。
(3)-j SNAT --to-source 10.0.0.8 是网关外网卡IP地址。
方法2:适合变化外网地址(ADSL):
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE ç伪装。

=====================================

#在10段主机可以通过访问lb01 10.0.0.7:9999,即可访问到172.16.1.51:22 SSH

#LB-01上:
ip addr add 10.0.0.7/24 dev eth0 label eth0:0
#添加规则
iptables -t nat -A PREROUTING -d 10.0.0.7 -p tcp --dport 9999 -j DNAT --to-destination 172.16.1.51:22

#web-01上:
ssh -p9999 10.0.0.7

==================================
跨不同网段IP服务器通信问题
(1)生产环境大于254台机器网段划分及路由解决方案详解01
http://v.youku.com/v_show/id_XNTAyMjAwMzI0.html
(2) linux route命令深入浅出与实战案例精讲
http://oldboy.blog.51cto.com/2561410/1119453
http://oldboy.blog.51cto.com/2561410/974194
必看3遍以上。
映射多个外网IP上网
iptables -t nat -A POSTROUTING -s 10.0.1.0/255.255.240.0 -o eth0 -j SNAT --to-source 124.42.60.11-124.42.60.16
iptables -t nat -A POSTROUTING -s 172.16.1.0/255.255.255.0 -o eth0 -j SNAT --to-source 124.42.60.103-124.42.60.106
#iptables -t nat -A POSTROUTING -s 172.16.1.0/22 -o eth0 -j SNAT --to-source 10.0.0.241-10.0.0.249
问题:
1、2000人被封
2、可用65535端口资源有限

===========================
iptables优化,了解即可
放到/etc/sysctl.conf下
sysctl -p 生效

dmesg里面显示 ip_conntrack: table full, dropping packet.的错误提示.如何解决。
#以下参数是对iptables防火墙的优化,防火墙不开会提示,可以忽略不理。
c58:
net.ipv4.ip_conntrack_max = 25000000
net.ipv4.netfilter.ip_conntrack_max=25000000
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=180
net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait=120
net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait=60
net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait=120
################################################################
C64:
net.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120

1.1.1 Iptables企业应用场景
1、主机防火墙(filter表的INPUT链)。
2、局域网共享上网(nat表的POSTROUTING链)。半个路由器,NAT功能。
3、端口及IP(一对一)映射(nat表的PREROUTING链),硬防的NAT功能。

转载于:https://blog.51cto.com/1362336072/2061412

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值