当前位置: 首页 > 编程日记 > 正文

CentOS 7 下用 firewall-cmd / iptables 实现 NAT 转发供内网服务器联网

自从用 HAProxy 对服务器做了负载均衡以后,感觉后端服务器真的没必要再配置并占用公网IP资源。
而且由于托管服务器的公网 IP 资源是固定的,想上 Keepalived 的话,需要挤出来 3 个公网 IP 使用,所以更加坚定了让负载均衡后端服务器释放公网 IP 的想法。
可是,后端服务器也不是简单释放公网 IP 就能正常工作的,正在运行的系统很多模块依然需要具有连接外网获取数据的能力。

所以就想到了用 CentOS 做一个软路由(内网 NAT 转发),如果能实现的话,就满足了我的需求。
搜索并试验了一番,目前发现用 iptables 是可行的,而且已经被我验证有效的方案。

由于用到了 iptables,需要停止并禁用内置的 firewalld 防火墙服务。

☼ 停止内置的 firewalld

systemctl stop firewalld
systemctl disable firewalld

☼ 打开系统的 IP 转发功能

echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p

☼ 安装 iptables 服务

yum -y install iptables-services
# 移除 iptables 服务
#yum -y remove iptables-services

☼ 查看 iptables 规则

iptables -L

☼ 清空默认的 filter 表

iptables -F

☼ 清空默认的 nat 表

iptables -t nat -F

☼ 默认规则,禁止所有入站,允许所有出站

iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT

☼ 默认规则,允许所有本地环回通信,出入站

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

☼ 重点,开启 NAT 功能

iptables -t nat -A POSTROUTING -j MASQUERADE

☼ 完整的命令

可以在命令行下粘贴批量执行

systemctl stop firewalld
systemctl disable firewalldyum -y install iptables-servicesiptables -F
iptables -t nat -F
iptables -P INPUT  DROP
iptables -P OUTPUT ACCEPT
iptables -t nat -A POSTROUTING -j MASQUERADEiptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.66.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -s 你的可信任远程管理IP -j ACCEPTiptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPTiptables-save > /etc/sysconfig/iptables
systemctl restart iptables

☼ 其他

# 允许已建立的传入连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT# 允许DHCP传入连接
iptables -A INPUT -i eth1 -p udp --dport 67:68 -j ACCEPT# 默认禁止路由转发
iptables -P FORWARD DROP# 允许内网路由转发
iptables -A FORWARD -s 192.168.66.0/24 -j ACCEPT

☼ 后记,补充 2017-12-13 20:28

捣鼓了一下午,NAT 转发当路由器供内网服务器上网终于搞定了,结果CentOS重启后,发现 iptables 的配置丢失,竟然没有永久保存?
太扯淡!

网上说这个问题的还很多,有人说可以制作自启动脚本,在启动时自动将 iptables 的规则重新注册一次,
也算是一个解决办法。

不过,思来想起,既然 CentOS 已经抛弃了 iptables ,那肯定是有一定道理的,firewalld 一定也有办法实现同样的功能吧!


firewall-cmd --permanent --zone=public --add-masquerade# 调整防火墙策略,开放 vrrp 协议,给 Keepalived 使用
# 否则可能导致【脑裂】问题,争抢VIP,或者master挂掉之后backup无法正常工作
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens33 --protocol vrrp -j ACCEPT
firewall-cmd --reload
#

搞定了。

当然其他功能端口的开放,这里就不啰嗦了 (0^0)。

转载于:https://www.cnblogs.com/hope250/p/8033818.html

相关文章:

八皇后的一个回溯递归解法

解法来自严蔚敏的数据结构与算法。 代码如下&#xff1a; #include <iostream> using namespace std; const int N 8;//皇后数 int count 0;//解法统计 int a[N][N];//储存值的数组const char *YES "■"; const char *NO "□"; //const char *Y…

即时编译和提前编译_即时编译说明

即时编译和提前编译Just-in-time compilation is a method for improving the performance of interpreted programs. During execution the program may be compiled into native code to improve its performance. It is also known as dynamic compilation.即时编译是一种提…

bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submit][Status][Discuss]Description 给定一棵N个节点的树&#xff0c;每个点有一个权值&#xff0c;对于M个询问(u,v,k)&#xff0c;你需要回答u xor lastans和v这两个节点…

.Net SqlDbHelper

using System.Configuration; using System.Data.SqlClient; using System.Data;namespace ExamDAL {class SqHelper{#region 属性区// 连接字符串private static string strConn;public static string StrConn{get{return ConfigurationManager.ConnectionStrings["Exam&…

C语言的一个之前没有见过的特性

代码&#xff1a; #include <stdio.h>int test(){int a ({int aa 0;int bb 1;int cc 2;if(aa 0 && bb 1)printf("aa %d, bb %d\n", aa, bb);//return -2;cc;});return a; }int main(){typeof(4) a test();printf("a %d\n", a); } …

如何构建顶部导航条_如何构建导航栏

如何构建顶部导航条导航栏 (Navigation Bars) Navigation bars are a very important element to any website. They provide the main method of navigation by providing a main list of links to a user. There are many methods to creating a navigation bar. The easiest…

SPSS聚类分析:K均值聚类分析

SPSS聚类分析&#xff1a;K均值聚类分析 一、概念&#xff1a;&#xff08;分析-分类-K均值聚类&#xff09; 1、此过程使用可以处理大量个案的算法&#xff0c;根据选定的特征尝试对相对均一的个案组进行标识。不过&#xff0c;该算法要求您指定聚类的个数。如果知道&#xff…

[ 总结 ] nginx 负载均衡 及 缓存

操作系统&#xff1a;centos6.4 x64 前端使用nginx做反向代理&#xff0c;后端服务器为&#xff1a;apache php mysql 1. nginx负载均衡。 nginx编译安装&#xff08;编译安装前面的文章已经写过&#xff09;、apache php mysql 直接使用yum安装。 nginx端口&#xff1a;80…

中国剩余定理(孙子定理)的证明和c++求解

《孙子算经》里面的"物不知数"说的是这样的一个题目&#xff1a;一堆东西不知道具体数目&#xff0c;3个一数剩2个&#xff0c;5个一数剩3个&#xff0c;7个一数剩2个&#xff0c;问一共有多少个。 书里面给了计算过程及答案&#xff1a;70*2 21*3 15*2 -105*2 2…

osi七层网络层_OSI层速成课程

osi七层网络层介绍 (Introduction) Have you ever wondered how data is sent through the network from one machine to another? If yes, then the Open System Interconnected model is what you are looking for.您是否曾经想过如何通过网络将数据从一台机器发送到另一台机…

KMP算法求回溯数组的步骤

KMP算法到底是什么原理就不说了&#xff0c;各种资料上讲的明明白白&#xff0c;下面我就如何用代码来实现做一下说明和记录. KMP的核心思想就是&#xff0c;主串不回溯&#xff0c;只模式串回溯。而模式串匹配到第几位时失配&#xff0c;要回溯多少&#xff0c;由模式串本身来…

【.Net】vs2017 自带发布工具 ClickOnce发布包遇到的问题

一、遇到的问题 在安装了vs2017 社区版&#xff08;Community&#xff09;之后 想打包安装程序&#xff08;winform&#xff09; 还是想用之前的 installshield来打包 发现居然打不了&#xff0c;在官网查了 installshield不支持社区版&#xff08;Community&#xff09;&…

windows平台,开发环境变量配置

1.打开我的电脑--属性--高级--环境变量 JAVA配置环境变量变量名&#xff1a;JAVA_HOME 变量值&#xff1a;C:\Program Files\Java\jdk1.7.0 变量名&#xff1a;CLASSPATH 变量值&#xff1a;.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; 变量名&#xff1a;Path 变…

如何编写可测试的代码 哈利勒的方法论

Understanding how to write testable code is one of the biggest frustrations I had when I finished school and started working at my first real-world job. 当我完成学业并开始从事第一份现实世界的工作时&#xff0c;了解如何编写可测试的代码是我最大的挫败之一。 T…

【Java入门提高篇】Day6 Java内部类——成员内部类

内部类是什么&#xff0c;简单来说&#xff0c;就是定义在类内部的类&#xff08;一本正经的说着废话&#xff09;。 一个正经的内部类是长这样的&#xff1a; public class Outer {class Inner{} } 这是为了演示而写的类&#xff0c;没有什么luan用&#xff0c;可以看到Inner类…

POJ 1001(高精度乘法 java的2种解法)

方法1: import java.math.BigDecimal; import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner sc new Scanner(System.in);while(sc.hasNext()){String d sc.next();int z sc.nextInt();BigDecimal bd new BigDecimal(d);BigDeci…

Java编写的电梯模拟系统《结对作业》

作业代码&#xff1a;https://coding.net/u/liyi175/p/Dianti/git 伙伴成员&#xff1a;李伊 http://home.cnblogs.com/u/Yililove/ 对于这次作业&#xff0c;我刚开始一点思绪都没有&#xff0c;在老师安排了结对伙伴李伊之后&#xff0c;我的搭档问我&#xff0c;我们需要什么…

HTML属性说明

HTML elements can have attributes, which contain additional information about the element.HTML元素可以具有属性&#xff0c;其中包含有关该元素的其他信息。 HTML attributes generally come in name-value pairs, and always go in the opening tag of an element. Th…

css中的选择器

1.在html中引入css的方法&#xff1a;四种方式: a.行内式(也称内联式) 如: <h1 style"color:red;test</h1> b.内嵌式 <style type"text/css"> h1{ color:red; font-size: 10.5pt; font-family: Calibri, sans-serif; line-height: normal; widow…

javascript的call()方法与apply()方法的理解

先看一段代码 function cat() {} cat.prototype{food:fish,say:function () {console.log(I love this.food);} };var blackCat new cat(); blackCat.say(); 这时&#xff0c;控制台输出 I love fish若此时&#xff0c;有另一个对象 Dog{food:bones and shit}; dog对象没有say…

java排序算法(冒泡,插入,选择,快速,堆,归并,希尔,基数)

import java.util.Arrays; import java.util.LinkedList;/*** * * 各种排序: 冒泡&#xff0c;插入&#xff0c;选择&#xff0c;快速&#xff0c;堆&#xff0c;归并&#xff0c;希尔&#xff0c;基数*/ public class Sorts {//1. 冒泡&#xff1a;//时间复杂度:n(n-1)/2O(n^2…

边界填充算法讲解_边界填充算法

边界填充算法讲解Boundary fill is the algorithm used frequently in computer graphics to fill a desired color inside a closed polygon having the same boundary color for all of its sides.边界填充是在计算机图形学中经常使用的算法&#xff0c;用于在其所有边都具有…

使用Git管理源代码

git是个了不起但却复杂的源代码管理系统。它能支持复杂的任务&#xff0c;却因此经常被认为太过复杂而不适用于简单的日常工作。让我们诚实一记吧&#xff1a;Git是复杂的&#xff0c;我们不要装作它不是。但我仍然会试图教会你用&#xff08;我的&#xff09;基本的Git和远程代…

[.Net跨平台]部署DTCMS到Jexus遇到的问题及解决思路---Linux环境搭建

最近朋友托我帮忙研究如何把一个DTCMS部署到Linux下&#xff0c;经过1天的研究&#xff0c;部署基本成功&#xff0c;可能有些细节还未注意到&#xff0c;现在把心得分享一下。过程比预期的要简单 身为.Net程序员&#xff0c;这个问题的第一步可能就是如何搭建一个Linux环境来测…

Sequence point 中文

摘自维基百科&#xff1a; In C[4] and C,[5] sequence points occur in the following places. (In C, overloaded operators act like functions, and thus operators that have been overloaded introduce sequence points in the same way as function calls.) Between ev…

python中pop函数_Python中的Pop函数

python中pop函数什么是弹出功能&#xff1f; (What is the pop function?) The method pop() removes and returns the last element from a list. There is an optional parameter which is the index of the element to be removed from the list. If no index is specified…

第六周学习进度条

日期 任务 听课 编程 阅读 准备考试 日总计 周日 周一 120 300 0 0 420 100 周二 0 120 0 0 120 周三 0 0 0 0 0 周四 0 0 0 0 0 周五 0 0 0 0 0 周六 0 120 100 0 …

1071. 小赌怡情(15)

常言道“小赌怡情”。这是一个很简单的小游戏&#xff1a;首先由计算机给出第一个整数&#xff1b;然后玩家下注赌第二个整数将会比第一个数大还是小&#xff1b;玩家下注t个筹码后&#xff0c;计算机给出第二个数。若玩家猜对了&#xff0c;则系统奖励玩家t个筹码&#xff1b;…

关于年长程序员的5个误传

原文链接&#xff1a;http://kb.cnblogs.com/page/150932/ 英文原文&#xff1a;Five Pervasive Myths About Older Software Developers 最近我刚过完40岁生日&#xff0c;一个朋友向我开玩笑地说“嘿&#xff0c;你已经老了&#xff0c;不适合做程序员了&#xff01;”我虽然…

java中getter_Java中的Getter和Setters解释了

java中getterGetters and setters are used to protect your data, particularly when creating classes. Getter和Setter用于保护数据&#xff0c;尤其是在创建类时。 For each instance variable, a getter method returns its value while a setter method sets or updates…