【图文教程】防火墙

本文介绍了Linux防火墙的基础知识,包括firewalld和netfilter,详细讲解了iptables的5表5链以及常用命令。通过案例演示了如何使用iptables进行规则配置,实现过滤和端口映射。此外,还探讨了firewalld的9个zone,以及如何进行zone管理和service配置。最后,介绍了iptables规则的备份与恢复方法。
摘要由CSDN通过智能技术生成

防火墙

1. firewalld和netfilter

SELINUX介绍

安全增强型 Linux (SecurityEnhanced Linux) 简称 SELinux , 它是—个 Linux 内核模块, 也是Linux 的—个安全子系统。SELinux 主要由美国国家安全局开发。2.6及以上版本的 Linux 内核都已经集成了 SELinux 模块。 SELinux 的结构及配置非常复杂, 而且大量概念性的东西, 要学精难度较大。很多 Linux 系统管理员嫌麻烦都把 SELinux关闭 了。

1. 临时关闭:selinux:
[root@sc ~]# setenforce 0
2. 查看关闭状态:getenforce
[root@sc ~]# getenforce 0
Permissive  #开启状态,但不生效。
[root@sc ~]#
3. 永久关闭 selinux:编辑 /etc/selinux/config 文件,把 SELINUX 行改为 disabled ,然后重启操作系统:

三个状态:
enforcing:开启
permissive:开启状态, 但是不生效, 只记录日志。临时关闭, 会调整到这个状态。
disabled:关闭
更改完配置后, 需要重启系统, 方可生效。

SELINUX 一般情况下都是关闭的,因为开启的话会有很多服务受限于它,从而增大运维的成本,而且关闭了 SELINUX 也不会对安全有什么影响。

[root@sc ~]# vi /etc/selinux/config

在这里插入图片描述

4. 在 CentOS7 上也可以使用 netfilter 机制的防火墙

netfilter 防火墙在 CentOS6 之前的版本是这个名字,在 CentOS7 则变成了 firewalld ,而且机制也变得不太一样,但是内部的工具: iptables 的用法是一样的。 iptables 可以去添加一些规则,比如关闭或开放端口。

5. 首先关闭 CentOS7 上的 firewalld 服务
[root@sc ~]# systemctl disable  firewalld  #禁止 firewalld 开机自启动
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@sc ~]# systemctl stop  firewalld  #关闭 firewalld 服务
[root@sc ~]#
6. 如果想启动 netfilter 服务,我们需要安装 iptables-services 包,安装这个包等于开启了 netfilter 服务
[root@sc ~]# yum install -y iptables-services  #安装 iptables 服务
[root@sc ~]# systemctl enable iptables  #启用服务
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@sc ~]# systemctl start iptables  #开始服务
[root@sc ~]#

10.13 netfilter5表5链介绍

netfilter 5表5链详解:http://www.cnblogs.com/metoy/p/4320813.html

在这里插入图片描述

netfilter 5表:

filte表
三个链: INPUT、 FORWARD、 OUTPUT作用:过滤数据包
内核模块:iptables _filter. 

Nat表
三个链: PRE ROUTING、POSTROUTING、OUTPUT
作用:用于网络地址转换(IP、 端口)内核模块:iptable_nat 

Mangle表
五个链:PRE ROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD
作用:修改数据包的服务类型、TTL、并且可以配置路由实现QOS
内核模块:iptable _ mangle(别看这个表这么麻烦,咱们设置策略时几乎都不会用到它)

Raw表
两个链:OUTPUT、PREROUTING
作用:决定数据包是否被状态跟踪机制处理
内核模块:iptable_raw 

Security表
三个链:INPUT、OUTPUT和FORWARD
作用:Security表在centos6中并没有,用千强制访问控制(MAC)的网络规则
内核模块:iptable_security

在这里插入图片描述

netfilter 5链:

PREROUTING: 数据包进入路由表之前
INPUT: 通过路由表后目的地为本机
FORWARD: 通过路由表后,目的地不为本机
OUTPUT: 由本机产生,向外发出
POSTROUTING: 发送到网卡接口之前
1. Nat表映射示意图:

在这里插入图片描述

2. netfilter 5链示意图:

在这里插入图片描述

10.14 iptables语法

在这里插入图片描述

iptables介绍:

iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作

参数 参数含义
-t<表> 指定要操纵的表
-A 向规则链中添加条目
-D 从规则链中删除条目
-i 向规则链中插入条目
-R 替换规则链中的条目
-L 显示规则链中已有的条目
-F 清除规则链中已有的条目
-Z 清空规则链中的数据包计算器和字节计数器
-N 创建新的用户自定义规则链
-P 定义规则链中的默认目标
-h 显示帮助信息
-p 指定要匹配的数据包协议类型
-s 指定要匹配的数据包源ip地址
-j<目标> 指定要跳转的目标
-i<网络接口> 指定数据包进入本机的网络接口
-o<网络接口> 指定数据包要离开本机所使用的网络接口
表名包括:

filter:包过滤,用于防火墙规则。

net:地址转换,用于网关路由器。
规则链名包括:

INPUT链:处理输入数据包。

OUTPUT链:处理输出数据包。

PORWARD链:处理转发数据包。

PREROUTING链:用于目标地址转换(DNAT)。

POSTOUTING链:用于源地址转换(SNAT)。
动作包括:

ACCEPT:接收数据包。

DROP:丢弃数据包。

REDIRECT:重定向、映射、透明代理。

SNAT:源地址转换。

DNAT:目标地址转换。

MASQUERADE:IP伪装(NAT),用于ADSL。

LOG:日志记录。
iptables参数示例:
1. 使用 iptables -nvL 命令查看 iptables 服务自带的一些规则:
[root@sc ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   34  2448 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 23 packets, 3076 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@sc ~]#
[root@sc ~]# service iptables restart  #重启 iptables 这个服务的命令
2. iptables 的默认规则在 /etc/sysconfig/iptables 文件里保存着:
[root@sc ~]# 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@sc ~]#
3. 清除规则使用 iptables -F 命令,这样虽然清空了规则,但是默认规则还会保存在 /etc/sysconfig/iptables 文件里:
[root@sc ~]# iptables -F  #清除规则命令
[root@sc ~]# iptables -nvL  #查看指令
Chain INPUT (policy ACCEPT 16 packets, 1168 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 9 packets, 1212 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@sc ~]# 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@sc ~]#
4. 重启 iptables 规则:
[root@sc ~]# service iptables restart
5. 想要把当前规则保存到 /etc/sysconfig/iptables 文件里就要使用 service iptables save 命令:

在这里插入图片描述

如果没有保存到 /etc/sysconfig/iptables 文件里的话,重启服务后就会重新加载 /etc/sysconfig/iptables 文件里的规则:

[root@sc ~]# service iptables restart  #重启 iptables 服务
Redirecting to /bin/systemctl restart iptables.service
[root@sc ~]# iptables -nvL  #查看指令
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    6   428 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, 496 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@sc ~]#
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值