Linux防火墙-netfilter
关闭selinux的方法:
[root@localhost ~]# setenforce 0 # 临时关闭
setenforce: SELinux is disabled
[root@localhost ~]# getenforce  # 查看selinux
Disabled
[root@localhost ~]# vim /etc/selinux.conf
# 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=disabled 
# 永久关闭selinux
# 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=targetediptables
iptables只是Linux防火墙的管理工具而已,位于/sbin/iptables。真正实现防火墙功能的是 netfilter,它是Linux内核中实现包过滤的内部结构。
iptables 传输包的过程:
① 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去。
② 如果数据包就是进入本机的,它就会沿着图向下移动,到达INPUT链。数据包到了INPUT链后,任何进程都会收到它。本机上运行的程序可以发送数据包,这些数据包会经过OUTPUT链,然后到达POSTROUTING链输出。
③ 如果数据包是要转发出去的,且内核允许转发,数据包就会如图所示向右移动,经过FORWARD链,然后到达POSTROUTING链输出。

iptables的规则表和链:
表(tables)提供特定的功能,iptables内置了5个表
- filter表用于过滤包,最常用的表,有INPUT、FORWARD、OUTPUT三个链 
- nat表用于网络地址转换,有PREROUTING、OUTPUT、POSTROUTING三个链 
- managle表用于给数据包做标记 
- raw表可以实现不追踪某些数据包 
- security表在centos6中并没有,用于强制访问控制(MAC)的 
规则链:
- INPUT——进来的数据包应用此规则链中的策略 
- OUTPUT——外出的数据包应用此规则链中的策略 
- FORWARD——转发数据包时应用此规则链中的策略 
- PREROUTING——对数据包作路由选择前应用此链中的规则(记住!所有的数据包进来的时侯都先由这个链处理) 
- POSTROUTING——对数据包作路由选择后应用此链中的规则(所有的数据包出来的时侯都先由这个链处理) 
iptables 的使用
centos7之前使用netfilter防火墙 ,centos7开始使用firewalld防火墙
关闭firewalld开启netfilter方法 systemctl stop firewalld systemctl disable firewalled yum install -y iptables-services systemctl enable iptables systemctl start iptables
[root@localhost ~]# systemctl stop firewalld    # 关闭firewalld开启netfilter方法
[root@localhost ~]# systemctl disable firewalld # 开机关闭
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.再CentOS7中要使用iptables,需要
[root@localhost ~]# yum install -y iptables-services
[root@localhost ~]# systemctl enable iptables
[root@localhost ~]# systemctl start iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.iptables 命令:
介绍:设置linux的netfilter
格式:iptables [option] ...
用法:
- -A 在指定链的末尾添加(append)一条新的规则 
- -D 删除(delete)指定链中的某一条规则,可以按规则序号和内容删除 
- -I 在指定链中插入(insert)一条新的规则,默认在第一行添加 
- -R 修改、替换(replace)指定链中的某一条规则,可以按规则序号和内容替换 
- -L 列出(list)指定链中所有的规则进行查看 
- -E 重命名用户定义的链,不改变链本身 
- -F 清空(flush) 
- -N 新建(new-chain)一条用户自己定义的规则链 
- -X 删除指定表中用户自定义的规则链(delete-chain) 
- -P 设置指定链的默认策略(policy) 
- -Z 将所有表的所有链的字节和数据包计数器清零 
- -n 使用数字形式(numeric)显示输出结果 
- -v 查看规则表详细信息(verbose)的信息 
- -V 查看版本(version) 
- -h 获取帮助(help) 
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 845 packets, 61230 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         
...       
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -Z防火墙处理数据包的四种方式
- ACCEPT 允许数据包通过 
- DROP 直接丢弃数据包,不给任何回应信息 
- REJECT 拒绝数据包通过,必要时会给数据发送端一个响应的信息。 
- LOG在/var/log/messages文件中记录日志信息,然后将数据包传递给下一条规则 
iptables 实例
# 添加一条拒绝从192.168.188.1:1234 -> 192.168.188.128:80 tcp方式进来的规则
[root@localhost ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 47 packets, 3138 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.128      tcp spt:1234 dpt:80
[root@localhost ~]# iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@localhost ~]# iptables -P INPUT DROP # 将INPUT链默认改为DROP
[root@localhost ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP # 禁止ping本机- -s :来源ip 
- -p :传输协议 
- --sport:来源端口 
- -d :目标ip 
- --dport:目标端口 
- -i:指定网卡 
- -j:处理方式 
[root@localhost ~]# vim /usr/local/sbin/iptables.sh //加入如下内容
#! /bin/bash
ipt="/usr/sbin/iptables"
$ipt -F
$ipt -P INPUT DROP
$ipt -P OUTPUT ACCEPT
$ipt -P FORWARD ACCEPT
$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT    
$ipt -A INPUT -s 192.168.133.0/24 -p tcp --dport 22 -j ACCEPT
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT
$ipt -A INPUT -p tcp --dport 21 -j ACCEPTnat表应用
A机器两块网卡ens33(192.168.133.130)、ens37(192.168.100.1),ens33可以上外网,ens37仅仅是内部网络,B机器只有ens37(192.168.100.100),和A机器ens37可以通信互联。
需求1:可以让B机器连接外网
A机器上打开路由转发 echo "1">/proc/sys/net/ipv4/ip_forward
A上执行 iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
B上设置网关为192.168.100.1需求2:C机器只能和A通信,让C机器可以直接连通B机器的22端口
A上打开路由转发 echo "1">/ proc/sys/net/ipv4/ip_forward
A上执行 iptables -t nat -A PREROUTING -d 192.168.133.130 -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.133.130
B上设置网关为192.168.100.1iptables的备份和恢复:
[root@localhost ~]# service iptables save # 保存规则
iptables: Saving firewall rules to /etc/sysconfig/iptables: [  确定  ]
[root@localhost ~]# iptables-save > iptables.ipt
[root@localhost ~]# ls
anaconda-ks.cfg  CentOS7-Base-163.repo  iptables  python36-3.6.3-7.el7.x86_64.rpm  python36-libs-3.6.3-7.el7.x86_64.rpm
[root@localhost ~]# iptables-restore < iptables.ipt  # 恢复规则
[root@localhost ~]#firewalld
当CentOS7之后默认就没有iptables,取而代之的是firewalld。
[root@localhost ~]# systemctl enable firewalld # 开机启动
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@localhost ~]# 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@localhost ~]# systemctl start firewalld   # 启动firewalld
[root@localhost ~]# systemctl stop firewalld    # 关闭firewalld
[root@localhost ~]# systemctl restart firewalld # 重启firewalldfirewalld不再是iptables的表,而是zones
[root@localhost ~]# firewall-cmd --get-zones  # 查看所有zones
block dmz drop external home internal public trusted work
[root@localhost ~]# firewall-cmd --get-default-zone  # 默认zone
public
[root@localhost ~]#
firewall-cmd --set-default-zone=work //设定默认
zone
 firewall-cmd --get-zone-of-interface=ens33 //查指定网卡
firewall-cmd --zone=public --add-interface=lo //给指定网卡设置zone 
firewall-cmd --zone=dmz --change-interface=lo //针对网卡更改zone
firewall-cmd --zone=dmz  --remove-interface=lo  //针对网卡删除zone 
firewall-cmd --get-active-zones  //查看系统所有网卡所在的zone
firewall-cmd --get-services  查看所有的servies
firewall-cmd --list-services  //查看当前zone下有哪些
service
 firewall-cmd --zone=public --add-service=http //把http增加到public zone下面
firewall-cmd --zone=public --remove-service=http
ls /usr/lib/firewalld/zones/ //zone的配置文件模板
firewall-cmd --zone=public --add-service=http --permanent //更改配置文件需求:ftp服务自定义端口1121,需要在work zone下面放行ftp
cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
vim /etc/firewalld/services/ftp.xml //把21改为1121
cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
vim /etc/firewalld/zones/work.xml //增加一行
 <service name="ftp"/>
firewall-cmd --reload //重新加载
firewall-cmd --zone=work --list-services最后更新于
这有帮助吗?