netfilter 五个表五个链介绍,iptables案例
2018-06-12 21:47
309 查看
linux防火墙 netfilter
selinux 临时关闭 setenforce 0[root@bogon ~]# getenforce Enforcing 开启状态 [root@bogon ~]# setenforce 0 [root@bogon ~]# getenforce Permissive 暂停状态,重启后恢复
selinux 永久关闭 vi /etc/selinux/config(默认直接关闭selinux,不然会对后期的服务产生限制)
将SELINUX=enforcing改为SELINUX=disabled,保存后退出 (重启后才会生效)
# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
在centOS 7之前还有个防火墙是netfilter ,contos7以后改用 firewalld
关闭firewalld开机自启 : systemctl disable firewalld
关闭firewalld服务: systemctl stop firewalld
[root@aminglinux-01 network-scripts]# systemctl disable firewalld Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service. [root@aminglinux-01 network-scripts]#
开启netfilter
yum install -y iptables-services 下载安装netfilter
[root@aminglinux-01 network-scripts]# systemctl enable iptables 设置开机自启 Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. [root@aminglinux-01 network-scripts]# systemctl start iptables 启动服务 [root@aminglinux-01 network-scripts]# iptables -nvL 查看filter表 Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 8 576 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 6 468 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 8 packets, 928 bytes) pkts bytes target prot opt in out source destination [root@aminglinux-01 network-scripts]#
netfilter的5个表5个链接
五个表
filter 主要用于过滤包,是系统预设的表,该表内建3个链:INPUT,OUTPUT,FORWARD。INPUT链作用于进入本机的包,OUTPUT链作用于本机送出去的包,FORWARD链作用于那些跟本机无关的包。nat表 主要用于网络地址转换,它也有三个链。PREROUTING链的作用是在包刚刚到达防火墙时改变它的目的地址(如果需要的话),OUTPUT链的作用是改变本地产生的包的目的地址,POSTROUTING链的作用是在包即将离开防火墙时改变其源地址。
mangle表主要用于给数据包做标记,然后根据标记去操作相应的包。这个表几乎不怎么用,除非像称为一个高级网络工程师。
raw表 可以实现不追踪某些数据包,默认系统的数据包都会被追踪,但追踪势必消耗一定的资源,所以可以用raw表来指定某些端口的包不被追踪。
security表,在centos6中是没有的,他用于强制访问控制(MAC)的网络规则。
netfilter的5个链
PREROUTING: 数据包进入路由表之前。INPUT:通过路由表后目的地为本机。
FORWARDING: 通过路由表后,目的地不为本机。
OUTPUT: 由本机产生,向外转发。
POSTROUTONG: 发送到网卡接口之前。
iptables 语法
iptables规则的储存位置/etc/sysconfig/iptables
[root@aminglinux-01 network-scripts]# cat /etc/sysconfig/iptables # sample configuration for iptables service # you can edit this manually or use system-config-firewall # please do not ask us to add additional ports/services to this default configuration *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT [root@aminglinux-01 network-scripts]#
iptables -nvL 查看表,
默认查看的是filter表[root@bogon ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 5 356 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 4 packets, 400 bytes) pkts bytes target prot opt in out source destination
-t 指定查看的表比如查看nat表,iptables -nvL -t nat
[root@bogon ~]# iptables -nvL -t nat Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 1 packets, 76 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 1 packets, 76 bytes) pkts bytes target prot opt in out source destination
清空iptables所有规则iptables -F ,清空之后规则储存文件里面还是有配置的。
[root@aminglinux-01 ~]# iptables -F [root@aminglinux-01 ~]# ^C [root@aminglinux-01 ~]# iptables -nvL Chain INPUT (policy ACCEPT 13 packets, 948 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 12 packets, 1208 bytes) pkts bytes target prot opt in out source destination [root@aminglinux-01 ~]#
保存规则:service iptables save之后配置文件会有改变.
清除包及流量计数器置零:iptables -Z
流量计数器是指iptables -nvL 输出内容的最前面有pkts,bytes这两列. iptables -Z清零pkts bytes 有多少包 数据量
iptables -nvL --line-number 显示规则编号
清除包及流量计数器置零:iptables -Z 。
保存规则:service iptables save
-A/-D:表示增加/删除一条规则 (-A 加的规则默认写到前面已经有的规则的后面)
-I: 表示插入一条规则,其实跟-A一样也是增加 (不过-I加的规则会写到已有规则的前面,优先执行)
-p:表示指定协议,可以是tcp,udp,或者icmp
--dport: 跟-p 一起使用,表示指定目标端口。 (用之前前面必须-p 指定协议,不然会报错)
--sport: 跟-p 一起使用,表示指定来源的端口。 (用之前前面必须-p 指定协议,不然会报错)
-s:表示指定来源IP .(可以是一个IP段)。
-d: 指定目标IP.
-j:后面跟动作,其中ACCEPT表示允许包,DROP表示丢掉包,REJECT 表示拒绝包。
-i:指定网卡(不常用:但是偶尔能用到)。
DROP: 丢掉数据包. 拒绝访问.
[root@aminglinux-01 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP [root@aminglinux-01 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 440 36272 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 21 1472 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 2 104 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 71 7269 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited 0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 15 packets, 1288 bytes) pkts bytes target prot opt in out source destination [root@aminglinux-01 ~]#
按照编号删除规则,删除第六条测试
[root@aminglinux-01 ~]# iptables -nvL --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 531 42528 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 3 21 1472 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 4 2 104 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 73 7737 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited 6 0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 69 packets, 7276 bytes) num pkts bytes target prot opt in out source destination
[root@aminglinux-01 ~]# iptables -D INPUT 6
[root@aminglinux-01 ~]# iptables -nvL --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 585 46272 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 3 21 1472 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 4 2 104 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 73 7737 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 4 packets, 480 bytes) num pkts bytes target prot opt in out source destination [root@aminglinux-01 ~]#
iptables 小案例
需求。需要把80.22.21放行,22端口需要指定一个ip段,只有这个ip段的ip访问的时候才能访问到。
需要用到一个脚本首先vi /usr/local/sbin/iptables.sh
[root@aminglinux-01 ~]# vim /usr/local/sbin/iptables.sh #!/bin/bash ipt="/usr/sbin/iptables" #定义一个变量。要写绝对路径 $ipt -F #首先清空之前的规则 $ipt -P INPUT DROP #没有写-t 说明操作的是filter表。然后定义一下默认策略。 #定义INPUT链的默认策略为全部丢掉。 $ipt -P OUTPUT ACCEPT #定义了OUTPUT链的默认策略为全部放行 $ipt -P FORWARD ACCEPT #定义了FORWARD链的默认策略为全部放行 $ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #目的是让相关的数据包RELATED,ESTABLISHED这两个状态放行 $ipt -A INPUT -s 192.168.133.0/24 -p tcp --dport 22 -j ACCEPT #把这个网段的数据22端口放行 $ipt -A INPUT -p tcp --dport 80 -j ACCEPT #把80端口数据包放行 $ipt -A INPUT -p tcp --dport 21 -j ACCEPT #把21端口数据包放行 ~ ~ ~
nat表应用
A机器两块网卡ens33(192.168.245.128),ens37(192.168.100.1),ens33可以上外网,ens37仅仅是内部网络,B机器只有ens37(192.168.100.100),和A机器ens37可以通信互联。
首先在虚拟机A上添加一块网卡,在B上也添加一块儿网卡,新加的两块网卡都改成LAN区段模式。然后配好ip。需求1:可以让B机器连接外网。
A机器上打开路由转发 echo"1">/proc/sys/net/ipv4/ip_forward默认linux内核是没有开启转发的,/proc/sys/net/ipv4/ip_forward这个文件是0 [root@aminglinux-01 ~]# cat /proc/sys/net/ipv4/ip_forward 0 改成1,就开起了内核转发。
命令行设置ipifconfig ens37 192.168.100.1/24 (重启后失效,改配置文件才会永久生效)
A上执行iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
[root@aminglinux-01 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE [root@aminglinux-01 ~]# iptables -t nat -nvL Chain PREROUTING (policy ACCEPT 8 packets, 2525 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 MASQUERADE all -- * ens33 192.168.100.0/24 0.0.0.0/0 [root@aminglinux-01 ~]#
B上设置网关为192.168.100.1
route -n 查看网关
[root@aminglinux-01 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.245.2 0.0.0.0 UG 100 0 0 ens33 192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 ens37 192.168.245.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 [root@aminglinux-01 ~]#
添加网关 route add default gw 192.168.100.1
这时候ping A机器的1网卡,如果通信了说明B电脑已经可以访问公网了。
可以设置dns来进行验证。vi /etc/resolv.conf
需求2:C机器只能和A通信,让C机器可以直接连接通B机器的22端口。
A上打开路由转发 echo"1">/ proc/sys/net/ipv4/ip_forwardA上执行iptabls -t nat -A PREROUTING -d 192.168.245.128 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
A上执行iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.245.128
B上设置网关为192.168.100.1
扩展(selinux了解即可)
selinux教程 http://os.51cto.com/art/201209/355490.htmselinux pdf电子书 http://pan.baidu.com/s/1jGGdExK
iptables应用在一个网段 http://www.aminglinux.com/bbs/thread-177-1-1.html
sant,dnat,masquerade http://www.aminglinux.com/bbs/thread-7255-1-1.html
iptables限制syn速率 http://www.aminglinux.com/bbs/thread-985-1-1.html
http://jamyy.us.to/blog/2006/03/206.html
相关文章推荐
- firewalld、netfilter、 netfilter5表5链、iptables介绍
- IPTables工具及其与netfilter关系介绍
- Linux netfilter/iptables内核模块介绍
- 简单介绍iptables和netfilter
- 七周第三次课 2017.11.29 Linux网络相关、firewalld和netfilter、netfilter5表5链介绍、iptables语法
- 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法
- Linux网络相关、firewalld和netfilter 、netfilter5表5链介绍 、 iptables语法
- 介绍一个Rusty Russell关于iptables快速应用案例
- Linux网络相关 firewalld和netfilter netfilter5表5链介绍 iptables语法
- IPTables工具及其与netfilter关系介绍
- 11.29 Linux网络相关 firewalld和netfilter netfilter5表5链介绍 iptables语法
- IPTables工具及其与netfilter关系介绍
- firewalld、netfilter、 netfilter5表5链、iptables介绍
- netfilter/iptables详细介绍
- firewalld和netfilter、netfilter5表5链介绍、iptables语法
- iptables DNAT功能介绍及案例分析
- 七周三次课(11月29日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法
- Linux网络相关、firewalld和netfilter、netfilter5表5链介绍和iptables语法
- linux学习第二十九篇:linux网络相关,防火墙:firewalld和netfilter,netfilter5表5链介绍,iptables语法
- iptables介绍iptables和netfilter