Linux防火墙之三足鼎立,烽火连天不休

[TOC]

常见的linux系统防火墙有:UFW、firewall、iptables,其中,UFW是Debian系列的默认防火墙,firewall 是红帽系列7及以上的防火墙(如CentOS7.x),iptables是红帽系列6及以下(如CentOS6.x)的防火墙。

事实上,他们很可能同时安装在同一个系统上,并且相互作用影响!

首先,iptables是最底层、最古老的防火墙系统,所有系统都会存在此防火墙,但一般而言只需保证该防火墙处于完全开放状态即可,其他不用管他,更不需要复杂的配置。而ufwfirewall都是较新linux系统上的替代iptables的工具,当他们同时安装在服务器上时,两者之间就会存在冲突。

firewall和ufw可共同影响服务器,任一防火墙开启都会使端口无法连接。

1、端口开放排查思路

# 首先查看端口监听情况,看服务是否正常开启且允许所有ip访问。(输出的IP为0.0.0.0或::::,才是允许所有ip访问。)
netstat -anp |grep 端口号

# 排查firewall状态,若开启,则开放对应端口或暴力关闭防火墙
firewall-cmd --state
或者 systemctl status firewalld.service

# 排查ufw状态,若开启,则开放对应端口或暴力关闭防火墙
ufw status

在Docker中,每个容器都有一个虚拟的网络接口,它与主机的网络接口相连。Docker通过iptables规则来控制容器的网络访问。当Docker启动容器时,它会自动在主机上创建一些iptables规则,这些规则用于控制容器的网络访问,包括端口转发、网络地址转换等功能。管理员也可以使用iptables来控制Docker容器的网络访问,以满足特定的安全要求。

所以一般iptables是不关闭的,且在docker创建容器时会自动创建iptables的规则。

2、ufw防火墙配置

# 查看是否安装了ufw
ufw status

# 安装ufw
apt-get install ufw -y
 
 
# 访问控制
## 设置默认策略,即拒绝传入并允许传出连接
ufw default deny incoming
ufw default allow outgoing

## 允许SSH连接
ufw allow ssh

## 禁止ping
vi /etc/ufw/before.rules
-A ufw-before-input -p icmp-tyoe echo-request -j ACCEPT 中的ACCEPT改成DROP

## 如果将SSH守护程序配置为使用其他端口,则必须指定相应的端口
ufw allow ssh对应的端口
 
## 允许IP段访问本机的端口
ufw allow from 192.168.1.0/24 to any port 端口号
## 允许IP访问本机的端口
ufw allow from 192.168.2.5 to any port 端口号
 
## 允许TCP协议 端口
ufw allow 端口号/tcp
ufw allow 52013:52020/tcp
## 允许UDP协议 端口
ufw allow 端口号/udp
ufw allow 52013:52020/udp
 
## 允许特定的IP地址
ufw allow from 192.168.2.6
## 允许特定IP段
ufw allow from 192.168.1.0/24
 
## 如果是拒绝就把allow改成deny

 
#删除规则
## 根据编号删除
ufw status numbered
ufw delete 对应的编号
## 直接删除
ufw delete 规则
如:ufw delete allow 80
 

#重新加载防火墙规则
# 启用ufw
ufw enable
# 重启ufw
ufw reload
#检测状态
ufw status verbose

3、firewall 防火墙配置

# 查看 firewalld 服务状态
systemctl status firewalld

# 安装firewalld
apt-get install firewalld


# 访问控制
## 开放端口
firewall-cmd --add-port=端口号/协议 --permanent
如firewall-cmd --add-port=8001/tcp --permanent
## 关闭端口
firewall-cmd --remove-port=端口号/协议 --permanent
如firewall-cmd --remove-port=8001/tcp --permanent

## 允许IP访问某端口
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.2.5/32 port protocol=tcp port=端口号 accept'
## 允许IP段访问某端口
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.1.0/24 port protocol=tcp port=端口号 accept'

## 允许ping(允许icmp包通过)
firewall-cmd --permanent --add-rich-rule='rule protocol value="icmp" accept' 

 
#删除规则
## 加载防火墙配置
firewall-cmd --reload
## 根据显示的规则删除
firewall-cmd --permanent --remove-rich-rule="rule protocol value="规则名" accept"


# 凡是修改了规则,都必须重新加载防火墙规则
firewall-cmd --reload
 
#查看规则
firewall-cmd --list-all

4、iptables防火墙配置

# 查看服务状态,列出规则,如果报错或内容为空iptables未运行
iptables -nL

# 安装iptables
apt-get install iptables


# 清空规则
## 这个一定要先做,不然清空后可能会悲剧
iptables -P INPUT ACCEPT
## 清空默认所有规则
iptables -F
## 清空自定义的所有规则
iptables -X


# 访问控制
## 开放端口
iptables -I INPUT -p tcp --dport 端口号 -j ACCEPT
## 禁止端口
iptables -I INPUT -p tcp --dport 端口号 -j DROP

#允许IP访问端口
iptables -I INPUT -s 192.168.2.5 -p tcp --dport 端口号 -j ACCEPT 
#允许IP段访问端口
iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 端口号 -j ACCEPT 

## 允许来自于特定接口的数据包
iptables -A INPUT -i 接口名 -j ACCEPT

## 允许ping(允许icmp包通过)
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

## 过滤所有非以上规则的请求
iptables -P INPUT DROP


# 保存iptables
service iptables save
或者
iptables-save


完成上述命令我们就完成了开放指定的端口,但是如果此时服务器重启,上述规则就没有了,所以我们需要对规则进行一下持续化操作
# 安装iptables-persistent
apt-get install iptables-persistent
# 持久化规则
netfilter-persistent save
netfilter-persistent reload


# 重启防火墙
service iptables restart
无标签