博客
关于我
<9>linux中的火墙策略优化
阅读量:640 次
发布时间:2019-03-15

本文共 1918 字,大约阅读时间需要 6 分钟。

火墙配置与管理

一、iptables

1. 火墙介绍

iptables 是 Linux 系统中最常用的防火墙工具,主要用于管理网络流量。以下是 iptables 的一些基本概念:

  • netfilter:iptables 的核心是 netfilter,用于对数据包进行匹配和操作。
  • iptables:iptables 是 netfilter 的用户空间命令行工具,用于定义防火墙规则。
  • firewalld:RHEL 8+ 系统默认使用 firewalld,这是一个更用户友好的防火墙管理工具。

2. 火墙管理工具切换

在 RHEL 8 中,默认使用 firewalld。但有时可能需要切换回 iptables:

dnf install iptables-services -ysystemctl stop firewalldsystemctl disable firewalldsystemctl mask firewalldsystemctl enable --now iptables
dnf install firewalld -ysystemctl stop iptablessystemctl disable iptablessystemctl mask iptablessystemctl enable --now firewalld

3. iptables 的使用

  • 永久保存策略:将 iptables 策略保存到文件中:
# 保存到文件iptables-save > /etc/sysconfig/iptables
  • 保存并重启服务
service iptables save

4. 火墙默认策略

默认策略包含 5 条链:

链名 含义
input 输入
output 输出
forward 转发
postrouting 路由之后
prerouting 路由之前

默认的 3 张表:

表名 含义
filter 经过内核的数据
nat 不经过内核的数据
mangle 特殊情况

5. iptables 命令

  • 指定表:-t
  • 不解析:-n
  • 查看规则:-L
  • 添加规则:-A
  • 协议:-p
  • 端口:--dport
  • 来源:-s
  • 动作:-j (ACCEPT, DROP, REJECT)
  • SNAT/DNAT:指定地址转换
  • 新建链:-N
  • 链名称:-E
  • 删除链:-X
  • 删除规则:-D
  • 插入规则:-I
  • 更改规则:-R
  • 更改默认规则:-P
  • 刷新:-F

数据包状态:

状态 含义
RELATED 已建立连接
ESTABLISHED 正在连接
NEW 新连接

二、firewalld

1. firewalld 的开启

  • 停止 iptables 服务并启用 firewalld:
systemctl stop iptablessystemctl disable iptablessystemctl mask iptablessystemctl enable --now firewalld

2. 关于 firewalld 的域

firewalld 支持多个域:

域名 描述
trusted 接受所有网络连接
home 家庭网络
work 工作网络
public 公共网络
dmz 军级网络
block 拒绝所有
drop 丢弃所有
internal 内部网络
external 外部网络

3. firewalld 的设定原理及数据存储

  • 配置目录:/etc/firewalld
  • 模块目录:/lib/firewalld

4. firewalld 的管理命令

  • 查看状态:firewall-cmd --state
  • 查看默认域:firewall-cmd --get-default-zone
  • 查看所有规则:firewall-cmd --list-all
  • 查看指定域:firewall-cmd --list-all --zone=work

5. firewalld 的高级规则及 NAT

  • 添加规则:firewall-cmd --permanent --add-rule
  • 添加 SNAT/DNAT:firewall-cmd --permanent --add-forward-port

测试与验证

  • 测试网络连接
# 查看 listening 端口netstat -antupye | grep sshd
  • 测试防火墙规则
iptables -Lfirewall-cmd --list-all
  • 测试 ping/ssh 连接
ping 172.25.254.3ssh root@172.25.254.203

通过以上配置,能够实现对网络流量的有效管理,确保系统安全且功能正常。

转载地址:http://lwrlz.baihongyu.com/

你可能感兴趣的文章
清新的彩虹折线PS填充图案
查看>>
uFocus for Mac(mac文本编辑器)
查看>>
2017CS231n笔记5.CNN
查看>>
Linux系统安装Nodejs
查看>>
vue项目报错集合
查看>>
golang语法基础 编译、运行
查看>>
图片链接
查看>>
html-javascript网页编辑-绘图连线
查看>>
C-Makefile的简单规则与介绍
查看>>
LINUX-WIFI无线接入的一些东西
查看>>
word文档手写字母总会大写问题
查看>>
<9>linux中的火墙策略优化
查看>>
Redis中的key
查看>>
Andriod进阶之路 - DataBinding的简单使用
查看>>
juc-09-控制并发流程工具类
查看>>
第一节 docker安装
查看>>
Linux系统时间与硬件时间及时间同步
查看>>
Django静态文件的加载以及STATIC_URL、 STATIC_ROOT 、STATICFILES_DIRS的区别
查看>>
maven 必会常用命令
查看>>
Spring 和 DI 依赖注入
查看>>