10.12 firewalld和netfilter
10.13 netfilter5表5链介绍
10.14 iptables语法
10.15 iptables filter表案例
10.16/10.17/10.18 iptables nat表应用
10.12 firewalld和netfilter
如何关闭SELinux
1、临时关闭SELINUX的方式:
setenforce 0 #临时关闭SELINUX,重启后SELINUX还会自动启动。
getenforce #getenforce可以获取SELINUX的运行状态。
[root@long01 ~]# setenforce 0
[root@long01 ~]# getenforce
Permissive
关于SELINUX的运行状态
-
enforcing #开启状态,会强制执行SELINUX的安全策略
-
permissive #提示状态,会打印触发SELINUX安全策略的警告,但是不会执行相应的策略。
-
disabled #关闭SELINUX,重启后也不会再启动SELINUX。
2、永久关闭SELINUX:
永久关闭SELINUX需要修改SELINUX的配置文件/etc/selinux/config
修改方法1:
vim /etc/selinux/config #使用vim编辑器编辑/etc/selinux/config文件。
将文件中的SELINUX=enforce修改为SELINUX=disabled后,保存退出。
修改方法2:执行下面这条命令:
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
检查修改结果:
[root@long01 ~]# grep -i "selinux=disabled" /etc/selinux/config
SELINUX=disabled
#grep能过滤出结果,表示修改正确。然后重启系统就能完全关闭SELINUX了。
[root@long01 ~]# getenforce
Disabled
开启netfilter步骤
1、关闭firewalld并禁止开机自启
2、下载iptables服务
3、开启iptables服务;enable iptables、start iptables
[root@xinlinux-02 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@xinlinux-02 ~]# systemctl stop firewalld
[root@xinlinux-02 ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@xinlinux-02 ~]# systemctl start iptables
关闭firewalld开启netfilter方法
systemctl disable firewalled #禁止开机自启firewalld
systemctl stop firewalld #停止firewalld服务
yum install -y iptables-services #下载iptables服务
开启iptables服务
systemctl enable iptables
systemctl start iptables
10.13 netfilter5表5链介绍
man iptables #有五表的说明
1)netfilter5表
1、filter——三个链:INPUT、FORWARD、OUTPUT
作用:过滤数据包
内核模块:iptables_filter.
2、Nat表—— 三个链:PREROUTING、POSTROUTING、OUTPUT
作用:用于网络地址转换(IP、端口)
内核模块:iptable_nat ( ###Nat表用的最多)
3、Mangle表——五个链:PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD
作用:修改数据包的服务类型、TTL、并且可以配置路由实现QOS
内核模块:iptable_mangle(设置策略时几乎都不会用到它)
4、Raw表——两个链:OUTPUT、PREROUTING
作用:决定数据包是否被状态跟踪机制处理
内核模块:iptable_raw
5、Security表——三个链:INPUT、OUTPUT和FORWARD
作用:Security表在centos6中并没有,用于强制访问控制(MAC)的网络规则
内核模块:iptable_security
2)netfilter的5个链
PREROUTING:数据包进入路由表之前
INPUT:通过路由表后目的地为本机
FORWARD:通过路由表后,目的地不为本机
OUTPUT:由本机产生,向外发出
POSTROUTING:发送到网卡接口之前
数据包流向:
##数据包首先流入PREROUTING,然后路由决策判断是否为本机;1、若是本机,就通过INPUT进入到本机内核,内核处理后发送出去通过OUTPUT,然后经过POSTROUTING;
2、若不是本机,直接FORWARD转发出去,然后POSTROUTING,数据包流出
总结:
1、如果数据包是进入本机的,会通过PREROUTING、INPUT、OUTPUT、POSTROUTING;
2、如果数据包不是进入本机的,会通过PREROUTING、FORWARD、POSTROUTING;
参考文章 http://www.cnblogs.com/metoy/p/4320813.html
10.14 iptables语法
注意:iptables规则是从上往下执行的
iptables一般语法格式:
iptables [-t table] 命令 [chain] [rules] [-j target]
格式说明:
table————指定表名,iptables内置包括filter表、nat表、mangle、raw表和security表。
命令—————对链的操作命令
chain————链名
rules————匹配规则
target————动作如何进行
操作命令选项说明:
-P或–policy <链名>:定义默认策略
-L或–list <链名>:查看iptables规则列表
-A或—append <链名>:在规则列表的最后增加1条规则
-I或–insert <链名>:在指定的位置插入1条规则
-D或–delete <链名>:从规则列表中删除1条规则
-R或–replace <链名>:替换规则列表中的某条规则
-F或–flush <链名>:删除表中所有规则 (临时删除,重启iptables后复原,)
-Z或–zero <链名>:将表中数据包计数器和流量计数器归零
规则选项说明:
-i或–in-interface <网络接口名>:指定数据包从哪个网络接口进入,如ppp0、eth0和eth1等
-o或–out-interface <网络接口名>:指定数据包从哪块网络接口输出,如ppp0、eth0和eth1等
-p或—proto协议类型 < 协议类型>:指定数据包匹配的协议,如TCP、UDP和ICMP等
-s或–source <源地址或子网>:指定数据包匹配的源地址
–sport <源端口号>:指定数据包匹配的源端口号,可以使用“起始端口号:结束端口号”的格式指定一个范围的端口
-d或–destination <目标地址或子网>:指定数据包匹配的目标地址 –dport <目标端口号>:指定数据包匹配的目标端口号,可以使用“起始端口号:结束端口号”的格式指定一个范围的端口
动作选项说明:
ACCEPT:接受数据包
DROP:丢弃数据包
REDIRECT:与DROP基本一样,区别在于它除了阻塞包之外, 还向发送者返回错误信息。
SNAT:源地址转换,即改变数据包的源地址
DNAT:目标地址转换,即改变数据包的目的地址
MASQUERADE: IP伪装,即是常说的NAT技术, MASQUERADE只能用于ADSL等拨号上网的IP伪装,也就是主机的IP是由ISP分配动态的; 如果主机的IP地址是静态固定的,就要使用SNAT
1、查看iptables规则
iptables -nvL
[root@xinlinux-02 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
105 6956 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 58 packets, 6200 bytes)
pkts bytes target prot opt in out source destination
第一列pkts(包数量)、bytes(数据量字节数)
2、清空规则(临时清空,重启iptables还原,除非保存到规则文件)
iptables -F
[root@xinlinux-02 ~]# iptables -F
[root@xinlinux-02 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 6 packets, 428 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 4 packets, 448 bytes)
pkts bytes target prot opt in out source destination
[root@xinlinux-02 ~]# systemctl restart iptables
[root@xinlinux-02 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
12 872 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
3、保存当前规则
#保存文件在/etc/sysconfig/iptables,重启时会调用该文件里面的规则
[root@xinlinux-02 ~]# 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
service iptables save
#保存当前设置的规则到文件/etc/sysconfig/iptables
systemctl restart iptables #重启iptables
4、查看指定表规则 -t指定表
#不指定表时,默认是filter表
iptables -nvL -t 表名
[root@xinlinux-02 ~]# 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 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
5、可以把计数器清零(表规则内的数据清空pkts、bytes)
iptables -Z
#抓包后发现一些ip来源有异常,用iptables将它DROP掉,将ip封掉;在iptables -nvL统计pkts、bytes时,可以设置一个规则----半小时后数据包量没有增加或者超过一个范围,那就可以将该ip解封掉
;(通过pkts判断的) 过完半小时后iptables -Z重新开始计算
6、增加规则-A(放在规则最后面)
iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
#-s指定来源ip,-p指定协议,--sport来源端口,-d目标ip,--dport目标端口,-i操作选项
ps:如果不-s指定ip源的话,系统会默认所有网卡ip
ps:如果增加规则不生效,有可能是被其他优先级较高的规则覆盖了;将规则iptables -F临时删除,然后重新添加就应该生效了
DROP/REJECT
共同点:效果一样,都是让数据包过不来
区别:
DROP:数据包过来看都不看直接丢掉
REJECT:数据包过来先看一眼,然后再拒绝
#使用 --dprot时要结合-p 协议使用
7、(插入)增加规则-I(大写i)(插入到最前面)
iptables -I INPUT -p tcp --dport 80 -j DROP
[root@xinlinux-02 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
[root@xinlinux-02 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
8、删除规则
第一种方法:-D(放在规则最前面)(需要知道添加这一条规则的完整命令,通过-D删除)
iptables -D INPUT -p tcp --dport 80 -j DROP
第二种方法:通过规则序列号删除规则
iptables -nvL --line-numbers #查看规则序列号
[root@xinlinux-02 ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8
iptables -D INPUT [规则序列号] #通过指定规定序列号删除
[root@xinlinux-02 ~]# iptables -D INPUT 1
[root@xinlinux-02 ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 54 4024 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
9、默认规则 (默认规则优先级最低)
#OUTPUT链的数据包由默认策略决定
iptables -P [链名] [动作选项]
iptables -P OUTPUT DROP #将OUTPUT链的数据包全部丢掉
#远程登录操控的话先不执行,因为一旦执行,远程连接的22端口也会被禁用掉;如果要执行该命令前先将22端口放行
10.15 iptables filter表案例
案例1:放行端口
只放行80、21、22三个端口的数据包,且22端口只有指定的IP段才能通过
这个案例我们可以编写一个shell脚本来实现。
vim /usr/local/sbin/iptables.sh #编辑脚本文件,加入以下内容
#! /bin/bash #定义执行脚本的shell
IPT="/usr/sbin/iptables" #定义变量,iptables命令的绝对路径。
$IPT -F #清空iptables规则
$IPT -P INPUT DROP #默认规则,丢弃所有数据包。
$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.233.0/24 -p tcp --dport 22 -j ACCEPT #仅允许192.168.233.0/24网段链接22端口
$IPT -A INPUT -p tcp --dport 80 -j ACCEPT #允许通过所有80端口的数据包
$IPT -A INPUT -p tcp --dport 21 -j ACCEPT #允许通过所有21端口的数据包
执行这个脚本后查看添加的规则。
bash /usr/local/sbin/iptables.sh
iptables -nvL
[root@xinlinux-02 ~]# vim /usr/local/sbin/iptables.sh
[root@xinlinux-02 ~]# bash /usr/local/sbin/iptables.sh
[root@xinlinux-02 ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
9 616 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT tcp -- * * 192.168.233.0/24 0.0.0.0/0 tcp dpt:22
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
案例2:禁ping
让本机可以ping通别的机器,但是别的机器ping不通本机
iptables -F ; iptables -P INPUT ACCEPT
#首先清空之前的规则,并且允许所有数据包通过INPUT链,必须一条命令执行,否则远程连接会断开
#丢弃icmp包类型为8的数据包。icmp类型为8的数据包是icmp请求,类型为0的是应答包。
关于icmp包类型参考说明:https://blog.csdn.net/noooooorth/article/details/51636482
iptables -A INPUT -p icmp --icmp-type 8 -j DROP
##添加这条规则后,可以本机可以ping通其他ip,但是其他机器ping不通本机了。
10.16/10.17/10.18 iptables nat表应用
A机器两块网卡ens33(192.168.133.130)、ens37(192.168.100.1),ens33可以上外网,ens37仅仅是内部网络,B机器只有ens37(192.168.100.100),和A机器ens37可以通信互联。
需求1:可以让B机器连接外网(就是A机器当做路由器,B连接)
1、 A机器上打开路由转发 echo "1">/proc/sys/net/ipv4/ip_forward
#/proc/sys/net/ipv4/ip_forward为0表示没有开启内核转发
2、 A上执行 iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
3、 B上设置网关为192.168.100.1
route add default gw 192.168.100.1 #设置默认网关
手动添加网卡ip ifconfig ens37 192.168.100.1/24 (重启消失)
端口映射
需求2:C机器只能和A通信,让C机器可以直接连通B机器的22端口
##将B机器的ip端口映射出来,通过A机器指定的端口可以连接B22端口
1、A上打开路由转发echo "1">/ proc/sys/net/ipv4/ip_forward
2、执行前先iptables -F清空规则,以免影响后面操作,
A上执行iptables -t nat -A PREROUTING -d 192.168.133.130 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
#DNAT为目标地址转换
3、 A上执行iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.133.130
#SDAT为源地址转换
4、 B上设置网关为192.168.100.1
route add default gw 192.168.100.1 #设置默认网关