NipGeihou's blog NipGeihou's blog
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档

NipGeihou

我见青山多妩媚,料青山见我应如是
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档
  • Linux

    • CentOS

      • 「CentOS」Firewall流量转发
        • 安装firewall
        • 准备工作
        • 设置转发
        • 转发本地
        • 转发远程
        • 重载配置文件
        • 手动修改配置文件
        • 参考资料
    • Ubuntu

    • 环境安装

    • 常用命令

    • 常用软件

    • 最佳实践

    • 通用

  • Docker

  • 云原生

  • Kubernetes

  • KubeSphere

  • K3S

  • 笔记

  • PVE

  • 维修

  • DevOps

  • 云服务

  • 路由器

  • Hyper-V

  • Windows

  • macOS

  • 运维
  • Linux
  • CentOS
NipGeihou
2022-01-06
目录

「CentOS」Firewall流量转发

# 安装 firewall

一般 centos 都自带有,如果没有就安装

yum install firewalld
systemctl enable firewalld # 开机自启
systemctl start firewalld

# 准备工作

开启路由转发

vim /etc/sysctl.conf
# 修改或添加 net.ipv4.ip_forward = 1

sysctl -p # 让内核参数立即生效

开启防火墙流量伪装功能

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

# 设置转发

开放入站端口

# 开放8080 tcp和udp端口
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --add-port=8080/udp --permanent

# 转发本地

常用于对外暴露高位端口号转发到实际端口,如外网暴露一个 12345 端口来访问服务器的 3306 的 MySQL 数据库端口

模板

firewall-cmd --add-forward-port=port=入站端口号:proto=tcp:toport=目标端口号 --permanent # tcp
firewall-cmd --add-forward-port=port=入站端口号:proto=udp:toport=目标端口号 --permanent # udp

例子

firewall-cmd --add-forward-port=port=12345:proto=tcp:toport=3306 --permanent
firewall-cmd --add-forward-port=port=12345:proto=udp:toport=3306 --permanent

# 转发远程

常用于流量中转

模板

firewall-cmd --add-forward-port=port=入站端口号:proto=tcp:toaddr=目标IP:toport=目标端口号 --permanent # tcp
firewall-cmd --add-forward-port=port=入站端口号:proto=udp:toaddr=目标IP:toport=目标端口号 --permanent # udp

例子

firewall-cmd --add-forward-port=port=8080:proto=tcp:toaddr=119.28.28.28:toport=53 --permanent
firewall-cmd --add-forward-port=port=8080:proto=udp:toaddr=119.28.28.28:toport=53 --permanent

# 重载配置文件

firewall-cmd --reload

# 手动修改配置文件

除了上述通过命令行的方式设置转发,也可通过配置文件的方式,用命令行添加的设置也会来到这里

vim /etc/firewalld/zones/public.xml
firewall-cmd --reload # 重载配置文件

# 参考资料

  • Centos 7 的 Firewall 流量转发 (opens new window)
#CentOS#端口转发
上次更新: 2022/01/06, 13:24:19
「Ubuntu」WSL Ubuntu安装SSH及免密登录

「Ubuntu」WSL Ubuntu安装SSH及免密登录→

最近更新
01
Docker Swarm
04-18
02
安全隧道 - gost
04-17
03
Solana最佳实践
04-16
更多文章>
Theme by Vdoing | Copyright © 2018-2025 NipGeihou | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式