linux firewalld 与 iptables

Linux下iptables 禁止端口和开放端口
1、关闭所有的 INPUT FORWARD OUTPUT 只对某些端口开放。
下面是命令实现:

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
再用命令

 iptables -L -n
查看 是否设置好, 好看到全部 DROP 了

这样的设置好了,我们只是临时的, 重启服务器还是会恢复原来没有设置的状态
还要使用 service iptables save 进行保存

service iptables save
看到信息 firewall rules 防火墙的规则 其实就是保存在 /etc/sysconfig/iptables

可以打开文件查看 vi /etc/sysconfig/iptables

2、下面我只打开22端口,看我是如何操作的,就是下面2个语句

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
再查看下 iptables -L -n 是否添加上去, 看到添加了

复制代码
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:22
Chain FORWARD (policy DROP)
target     prot opt source               destination
Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp spt:22
复制代码
现在Linux服务器只打开了22端口,用putty.exe测试一下是否可以链接上去。
可以链接上去了,说明没有问题。

最后别忘记了保存 对防火墙的设置
通过命令:service iptables save 进行保存

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

针对这2条命令进行一些讲解吧
-A 参数就看成是添加一条 INPUT 的规则
-p 指定是什么协议 我们常用的tcp 协议,当然也有udp 例如53端口的DNS
到时我们要配置DNS用到53端口 大家就会发现使用udp协议的
而 --dport 就是目标端口 当数据从外部进入服务器为目标端口
反之 数据从服务器出去 则为数据源端口 使用 --sport
-j 就是指定是 ACCEPT 接收 或者 DROP 不接收

3、禁止某个IP访问
1台Linux服务器,2台windows xp 操作系统进行访问
Linux服务器ip: 192.168.1.99
xp1 ip: 192.168.1.2
xp2 ip: 192.168.1.8

下面看看2台xp 都可以访问的

192.168.1.2 这是 xp1 可以访问的,
192.168.1.8 xp2 也是可以正常访问的。

那么现在我要禁止 192.168.1.2 xp1 访问, xp2 正常访问,
下面看看演示

通过命令

 iptables -A INPUT -p tcp -s 192.168.1.2 -j DROP
这里意思就是 -A 就是添加新的规则, 怎样的规则呢? 由于我们访问网站使用tcp的,

我们就用 -p tcp , 如果是 udp 就写udp,这里就用tcp了, -s就是 来源的意思,
ip来源于 192.168.1.2 ,-j 怎么做 我们拒绝它 这里应该是 DROP

好,看看效果。好添加成功。下面进行验证 一下是否生效

一直出现等待状态 最后 该页无法显示 ,这是 192.168.1.2 xp1 的访问被拒绝了。

再看看另外一台 xp 是否可以访问, 是可以正常访问的 192.168.1.8 是可以正常访问的


4、如何删除规则
首先我们要知道 这条规则的编号,每条规则都有一个编号

通过 iptables -L -n --line-number 可以显示规则和相对应的编号

1
2
3
4
5
6
iptables -L -n --line-number
 
num target     prot opt source               destination
1    DROP       tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:3306
2    DROP       tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:21
3    DROP       tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:80
多了 num 这一列, 这样我们就可以 看到刚才的规则对应的是 编号2

那么我们就可以进行删除了

iptables -D INPUT 2
删除INPUT链编号为2的规则。

再 iptables -L -n 查看一下 已经被清除了。


5、过滤无效的数据包
假设有人进入了服务器,或者有病毒木马程序,它可以通过22,80端口像服务器外传送数据。
它的这种方式就和我们正常访问22,80端口区别。它发向外发的数据不是我们通过访问网页请求
而回应的数据包。

下面我们要禁止这些没有通过请求回应的数据包,统统把它们堵住掉。

iptables 提供了一个参数 是检查状态的,下面我们来配置下 22 和 80 端口,防止无效的数据包。

iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
可以看到和我们以前使用的:

iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
多了一个状态判断。

同样80端口也一样, 现在删掉原来的2条规则,

iptables -L -n --line-number 
查看规则而且带上编号。我们看到编号就可以

删除对应的规则了。

iptables -D OUTPUT 1 
这里的1表示第一条规则。

当你删除了前面的规则, 编号也会随之改变。

好,我们删除了前面2个规则,22端口还可以正常使用,说明没问题了

下面进行保存,别忘记了,不然的话重启就会还原到原来的样子。

service iptables save
进行保存。

Saving firewall rules to /etc/sysconfig/iptables:          [ OK ]
其实就是把刚才设置的规则写入到 /etc/sysconfig/iptables 文件中。


6、DNS端口53设置
下面我们来看看如何设置iptables来打开DNS端口,DNS端口对应的是53

目前只开放22和80端口, 我现在看看能不能解析域名。

hostwww.google.com   
输入这个命令后,一直等待,说明DNS不通

出现下面提示 :
;; connection timed out; no servers could be reached

ping 一下域名也是不通

[root@localhost ~]#pingwww.google.com
ping: unknown hostwww.google.com
我这里的原因就是 iptables 限制了53端口。

有些服务器,特别是Web服务器减慢,DNS其实也有关系的,无法发送包到DNS服务器导致的。

下面演示下如何使用 iptables 来设置DNS 53这个端口,如果你不知道 域名服务端口号,你

可以用命令 : 

grep domain /etc/services
 

[root@localhost] ~ #grep domain /etc/services
domain          53/tcp                          # name-domain server
domain          53/udp
domaintime      9909/tcp                        # domaintime
domaintime      9909/udp                        # domaintime

看到了吧, 我们一般使用 udp 协议。

好了, 开始设置。。。

iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
这是我们 ping 一个域名,数据就是从本机出去,所以我们先设置 OUTPUT,

我们按照ping这个流程来设置。

然后 DNS 服务器收到我们发出去的包,就回应一个回来

iptables -A INPUT -p udp --sport 53 -j ACCEPT
同时还要设置

iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT
好了, 下面开始测试下, 可以用 iptables -L -n 查看设置情况,确定没有问题就可以测试了

[root@localhost ~iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:22
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:80
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp spt:53
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp dpt:53

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp spt:22 state ESTABLISHED
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp spt:80 state ESTABLISHED
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp dpt:53
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp spt:53

可以测试一下 是否 DNS 可以通过iptables 了。

[root@localhost ~]#hostwww.google.com
www.google.comis an alias forwww.l.google.com.
www.l.google.comis an alias for www-china.l.google.com.
www-china.l.google.com has address 64.233.189.104
www-china.l.google.com has address 64.233.189.147
www-china.l.google.com has address 64.233.189.99
正常可以解析 google 域名。

ping 方面可能还要设置些东西。

用 nslookup 看看吧

[root@localhost ~]#nslookup >www.google.com Server: 192.168.1.1 Address: 192.168.1.1#53 Non-authoritative answer: www.google.comcanonical name =www.l.google.com. www.l.google.com canonical name = www-china.l.google.com. Name: www-china.l.google.com Address: 64.233.189.147 Name: www-china.l.google.com Address: 64.233.189.99 Name: www-china.l.google.com Address: 64.233.189.104
说明本机DNS正常, iptables 允许53这个端口的访问。

7、iptables对ftp的设置
现在我开始对ftp端口的设置,按照我们以前的视频,添加需要开放的端口
ftp连接端口有2个 21 和 20 端口,我现在添加对应的规则。

[root@localhost root]#iptables -A INPUT -p tcp --dport 21 -j ACCEPT
[root@localhost root]#iptables -A INPUT -p tcp --dport 20 -j ACCEPT
[root@localhost root]#iptables -A OUTPUT -p tcp --sport 21 -j ACCEPT
[root@localhost root]#iptables -A OUTPUT -p tcp --sport 20 -j ACCEPT
 

好,这样就添加完了,我们用浏览器访问一下ftp,出现超时。
所以我刚才说 ftp 是比较特殊的端口,它还有一些端口是 数据传输端口,
例如目录列表, 上传 ,下载 文件都要用到这些端口。
而这些端口是 任意 端口。。。 这个 任意 真的比较特殊。
如果不指定什么一个端口范围, iptables 很难对任意端口开放的,
如果iptables允许任意端口访问, 那和不设置防火墙没什么区别,所以不现实的。
那么我们的解决办法就是 指定这个数据传输端口的一个范围。
下面我们修改一下ftp配置文件。
我这里使用vsftpd来修改演示,其他ftp我不知道哪里修改,大家可以找找资料。

[root@localhost root]#vi /etc/vsftpd.conf
 

在配置文件的最下面 加入

pasv_min_port=30001
pasv_max_port=31000
 

然后保存退出。

这两句话的意思告诉vsftpd, 要传输数据的端口范围就在30001到31000 这个范围内传送。

这样我们使用 iptables 就好办多了,我们就打开 30001到31000 这些端口。

[root@localhost root]#iptables -A INPUT -p tcp --dport 30001:31000 -j ACCEPT
[root@localhost root]#iptables -A OUTPUT -p tcp --sport 30001:31000 -j ACCEPT
[root@localhost root]#service iptables save
最后进行保存, 然后我们再用浏览器范围下 ftp。可以正常访问

用个账号登陆上去,也没有问题,上传一些文件上去看看。

上传和下载都正常。 再查看下 iptables 的设置

[root@localhost root]#iptables -L -n
 

Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpt:22
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpt:21
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpt:20
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpts:30001:31000

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spt:22
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spt:21
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spt:20
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spts:30001:31000

这是我为了演示ftp特殊端口做的简单规则,大家可以添加一些对数据包的验证
例如 -m state --state ESTABLISHED,RELATED 等等要求更加高的验证

[root@localhost ~]#servcie iptables stop           --临时关闭防火墙
 

[root@localhost ~]#chkconfig iptables off          --永久关闭防火墙

[root@localhost ~]#service iptables status          --查看防火墙状态


  iptables也叫netfilter,是Linux下自带的一款免费且优秀的基于包过滤的防火墙工具,他的功能十分强大,使用也非常灵活,可以对流入、流出、流经服务器的数据包进行精细的控制。但是iptables在CentOS7的版本上已经被阉割掉了,我们需要自行安装,以下是在CentOS7下安装iptables和使用方式。

一、安装iptables
1.1、查看是否安装成功
命令:systemctl status iptables

输出结果表示没有iptables的相关服务,我们需要安装

1.2、安装iptables
命令:yum install iptables-services

1.3、检查是否安装成功
命令:systemctl status iptables

输出结果表示已经安装了iptables但是还没有启动

1.4、启动iptables
命令:systemctl start iptables.service

再次查看状态,输出结果表示已经成功启动iptables

1.5、关闭SELINUX
这里需要关闭SELINUX,因为当SELINUX不关闭时,iptables不读取配置文件,关于如何关闭SELINUX建议大家浏览我的原创博文Linux:CentOS7下关闭SELINUX

二、命令
2.1、系统命令
systemctl start iptables #启动

systemctl status iptables #查看运行状态

systemctl restart iptables.service #重启

systemctl stop iptables.service #停止

systemctl enable iptables.service #设置开机启动

systemctl disable iptables.service #禁止开机启动

2.2、常用命令
iptables -h #查询帮助

iptables -L -n #列出(filter表)所有规则

iptables -L -n --line-number #列出(filter表)所有规则,带编号

iptables -L -n -t nat #列出(nat表)所有规则

iptables -F #清除(filter表)中所有规则

iptables -F -t nat #清除(nat表)中所有规则

service iptables save #保存配置(保存配置后必须重启iptables)

systemctl restart iptables.service #重启

三、语法
3.1、filter表解析
    filter表是iptables默认使用的表,负责对流入、流出本机的数据包进行过滤,该表中定义了3个链,分别是:INPUT、OUTPUT、FORWARD

INPUT:过滤进入主机的数据包

OUTPUT:处理从本机出去的数据包

FORWARD:负责转发流经本机但不进入本机的数据包,起到转发作用

3.2、iptables常用语法
-A:追加到规则的最后一条

-D:删除记录

-I:添加到规则的第一条

-p:(proto)规定通信协议,常见的协议有:tcp、udp、icmp、all

-j:(jump)指定要跳转的目标,常见的目标有:ACCEPT(接收数据包)、DROP(丢弃数据包)、REJECT(重定向)三种,但是一般不适用重定向,会带来安全隐患

四、常见案例
4.1、IP过滤
4.1.1、禁止192.168.1.3 IP地址的所有类型数据接入
iptables -A INPUT ! -s 192.168.1.3 -j DROP

4.2、开放端口
4.2.1、开放端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #开放80端口

4.2.2、开放端口范围
iptables -I INPUT -p tcp --dport 22:80 -j ACCEPT #开发22-80范围的端口

4.2.3、不允许80端口流出
iptables -I OUTPUT -p tcp --dport 80 -j DROP

在 CentOS 7 中,引入了一个新的服务,Firewalld,下面一张图,让大家明确的了解防火墙 Firewall 与 iptables 之间的关系与区别。

安装它,只需

yum install firewalld

如果需要图形界面的话,则再安装

yum install firewall-config

一、介绍

防火墙守护 firewalld 服务引入了一个信任级别的概念来管理与之相关联的连接与接口。它支持 ipv4 与 ipv6,并支持网桥,采用 firewall-cmd (command) 或 firewall-config (gui) 来动态的管理 kernel netfilter 的临时或永久的接口规则,并实时生效而无需重启服务。

zone

drop: 丢弃所有进入的包,而不给出任何响应
block: 拒绝所有外部发起的连接,允许内部发起的连接
public: 允许指定的进入连接
external: 同上,对伪装的进入连接,一般用于路由转发
dmz: 允许受限制的进入连接
work: 允许受信任的计算机被限制的进入连接,类似 workgroup
home: 同上,类似 homegroup
internal: 同上,范围针对所有互联网用户
trusted: 信任所有连接
过滤规则

source: 根据源地址过滤
interface: 根据网卡过滤
service: 根据服务名过滤
port: 根据端口过滤
icmp-block: icmp 报文过滤,按照 icmp 类型配置
masquerade: ip 地址伪装
forward-port: 端口转发
rule: 自定义规则
其中,过滤规则的优先级遵循如下顺序

1.source
2.interface
3.firewalld.conf

二、使用方法

# systemctl start firewalld        # 启动,
# systemctl enable firewalld        # 开机启动
# systemctl stop firewalld          # 关闭
# systemctl disable firewalld      # 取消开机启动

具体的规则管理,可以使用firewall-cmd ,具体的使用方法可以

$ firewall-cmd --help

--zone=NAME                        # 指定 zone
--permanent                        # 永久修改,--reload 后生效
--timeout=seconds                  # 持续效果,到期后自动移除,用于调试,不能与 --permanent 同时使用

1. 查看规则

查看运行状态

$ firewall-cmd --state

查看已被激活的 Zone 信息

$ firewall-cmd --get-active-zones
public
  interfaces: eth0 eth1

查看指定接口的 Zone 信息

$ firewall-cmd --get-zone-of-interface=eth0
public

查看指定级别的接口

$ firewall-cmd --zone=public --list-interfaces
eth0

查看指定级别的所有信息,譬如 public

$ firewall-cmd --zone=public --list-all
public (default, active)
  interfaces: eth0
  sources:
  services: dhcpv6-client http ssh
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

查看所有级别被允许的信息

$ firewall-cmd --get-service

查看重启后所有 Zones 级别中被允许的服务,即永久放行的服务

$ firewall-cmd --get-service --permanent

2. 管理规则

# firewall-cmd --panic-on          # 丢弃
# firewall-cmd --panic-off          # 取消丢弃
# firewall-cmd --query-panic        # 查看丢弃状态
# firewall-cmd --reload            # 更新规则,不重启服务
# firewall-cmd --complete-reload    # 更新规则,重启服务

添加某接口至某信任等级,譬如添加 eth0 至 public,永久修改

# firewall-cmd --zone=public --add-interface=eth0 --permanent

设置 public 为默认的信任级别

# firewall-cmd --set-default-zone=public

a. 管理端口

列出 dmz 级别的被允许的进入端口

# firewall-cmd --zone=dmz --list-ports

允许 tcp 端口 8080 至 dmz 级别

# firewall-cmd --zone=dmz --add-port=8080/tcp

允许某范围的 udp 端口至 public 级别,并永久生效

# firewall-cmd --zone=public --add-port=5060-5059/udp --permanent

b. 网卡接口

列出 public zone 所有网卡

# firewall-cmd --zone=public --list-interfaces

将 eth0 添加至 public zone,永久

# firewall-cmd --zone=public --permanent --add-interface=eth0

eth0 存在与 public zone,将该网卡添加至 work zone,并将之从 public zone 中删除

# firewall-cmd --zone=work --permanent --change-interface=eth0

删除 public zone 中的 eth0,永久

# firewall-cmd --zone=public --permanent --remove-interface=eth0

c. 管理服务

添加 smtp 服务至 work zone

# firewall-cmd --zone=work --add-service=smtp

移除 work zone 中的 smtp 服务

# firewall-cmd --zone=work --remove-service=smtp

d. 配置 external zone 中的 ip 地址伪装

查看

# firewall-cmd --zone=external --query-masquerade

打开伪装

# firewall-cmd --zone=external --add-masquerade

关闭伪装

# firewall-cmd --zone=external --remove-masquerade

e. 配置 public zone 的端口转发

要打开端口转发,则需要先

# firewall-cmd --zone=public --add-masquerade

然后转发 tcp 22 端口至 3753

# firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toport=3753

转发 22 端口数据至另一个 ip 的相同端口上

# firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toaddr=192.168.1.100

转发 22 端口数据至另一 ip 的 2055 端口上

# firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toport=2055:toaddr=192.168.1.100

f. 配置 public zone 的 icmp

查看所有支持的 icmp 类型

# firewall-cmd --get-icmptypes
destination-unreachable echo-reply echo-request parameter-problem redirect router-advertisement router-solicitation source-quench time-exceeded

列出

# firewall-cmd --zone=public --list-icmp-blocks

添加 echo-request 屏蔽

# firewall-cmd --zone=public --add-icmp-block=echo-request [--timeout=seconds]

移除 echo-reply 屏蔽

# firewall-cmd --zone=public --remove-icmp-block=echo-reply

g. IP 封禁

# firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='222.222.222.222' reject"


莫小安
CentOS7使用firewalld打开关闭防火墙与端口
1、firewalld的基本使用
启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld 
开机禁用  : systemctl disable firewalld
开机启用  : systemctl enable firewalld
 
 
2.systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。
启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed

3.配置firewalld-cmd

查看版本: firewall-cmd --version
查看帮助: firewall-cmd --help
显示状态: firewall-cmd --state
查看所有打开的端口: firewall-cmd --zone=public --list-ports
更新防火墙规则: firewall-cmd --reload
查看区域信息:  firewall-cmd --get-active-zones
查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0
拒绝所有包:firewall-cmd --panic-on
取消拒绝状态: firewall-cmd --panic-off
查看是否拒绝: firewall-cmd --query-panic
 
那怎么开启一个端口呢
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent
 

猜你喜欢

转载自blog.csdn.net/weixin_40018205/article/details/87717278
今日推荐