言成言成啊 | Kit Chen's Blog

Centos7防火墙基础

发布于2023-02-16 20:50:00,更新于2023-02-21 14:31:30,标签:devops  转载随意,文章会持续修订,请注明来源地址:https://meethigher.top/blog

一、防火墙管理

防火墙状态管理

1
2
3
4
5
6
# 打开
systemctl start firewalld
# 关闭
systemctl stop firewalld
# 重启
systemctl restart firewalld

防火墙自启动管理

1
2
3
4
# 打开开机自启动
systemctl enable firewalld
# 关闭开机自启动
systemctl disable firewalld

二、防火墙配置

2.1 查询规则

查看配置的所有的规则

1
2
3
# prots:所开放、暴漏的端口(包括ipv4、ipv6)
# rich rules:自定义防火墙规则,灵活运用,非常的实用(可限制ipv4、ipv6、源地址等)
firewall-cmd --list-all

其他简单查看命令

1
2
3
4
5
6
# 查看开放端口
firewall-cmd --list-ports
# 查看开放服务
firewall-cmd --list-services
# 查看开放协议
firewall-cmd --list-protocols

2.2 单一配置

2.2.1 协议配置

1
2
3
4
# 新增放行的tcp协议
firewall-cmd --zone=public --add-protocol=tcp --permanent && firewall-cmd --reload
# 移除放行的tcp协议
firewall-cmd --zone=public --remove-protocol=tcp --permanent && firewall-cmd --reload

2.2.2 端口配置

1
2
3
4
# 永久开放tcp协议的9999端口
firewall-cmd --zone=public --add-port=9999/tcp --permanent && firewall-cmd --reload
# 永久关闭tcp协议的9999端口
firewall-cmd --zone=public --remove-port=9999/tcp --permanent && firewall-cmd --reload

2.3 自定义配置

2.3.1 IP基础知识

以下IP是基于IPv4

IP地址格式如192.168.110.168/24,这也叫斜线记法。

以下从两个角度理解斜线记法。

1.) 从人的角度理解斜线记法

例如 192.168.1.0/24 表示32位的二进制地址中(任何IP的二进制地址都是32位),前24位为网络前缀,后8位代表主机号。

在换算中,192.168.1.0/24 对应的二进制为:

1
1100 0000,1010 1000,0000 0001,0000 0000

其中红色为主机号,总共有8位。当这8位全为0时,取最小地址192.168.1.0,当这8位全为1时,取最大地址192.168.1.255。但请注意,在实际中,主机号全为0或者全为1的地址一般不使用,作为预留地址另有作用。所以第一个地址为:

1
1100 0000,1010 1000,0000 0001,0000 0001

192.168.1.1 最后一个地址为:

1
1100 0000,1010 1000,0000 0001,1111 1110

192.168.1.254

因此,192.168.1.0/24 代表的IP段就是 192.168.1.1 ~ 192.168.1.254

2.) 从机器的角度理解斜线记法

斜线记法是给人看的,其实对机器来说,是没有斜线记法的。

因此,为了将斜线记法,变成让机器可理解的,就引入了子网掩码(地址掩码)的概念。

斜线后面的数字就是子网掩码中1的个数(从高位到低位),比如/20表示的子网掩码就是

1
1111 1111, 1111 1111, 1111 0000, 0000 0000

比如192.168.110.199/24用计算机能理解的表示

IP地址

1
1100 0000, 1010 1000, 0110 1110, 1100 0111

子网掩码地址

1
1111 1111, 1111 1111, 1111 1111, 0000 0000

将IP地址与子网掩码地址,进行AND运算,即可得到

1
1100 0000, 1010 1000, 0110 1110, 0000 0000

也就是IP地址192.168.110.199/24所在的网络地址是192.168.110.0/24

3.) 代码计算网络地址的IP范围

执行以下代码编译、运行

1
2
javac -encoding utf-8 IPCalcFreedom.java
java -Dfile.encoding=utf-8 IPCalcFreedom

源码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Scanner;

public class IPCalcFreedom {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.printf("输入IP地址:");
String ipAddress = scanner.nextLine();
resolveIP(ipAddress);
}

public static void resolveIP(String ipAddress) {
String[] split = ipAddress.split("[./]");
System.out.printf("%s表示前%s位为网络号, 后%s位为主机号\n", ipAddress, split[4], (32 - Integer.parseInt(split[4])));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < split.length - 1; i++) {
sb.append(complement(Integer.toBinaryString(Integer.parseInt(split[i]))));
}
int netIndex = Integer.parseInt(split[4]);
String ipBinary = sb.toString();
String netBinary = ipBinary.substring(0, netIndex);
String hostBinary = ipBinary.substring(netIndex);
System.out.printf("网络号二进制为%s\n", netBinary);
String maxHostBinary = getHostBinary(hostBinary, true);
String minHostBinary = getHostBinary(hostBinary, false);
System.out.printf("主机号二进制极值排除全为0或者全为1, 用于预留地址. 最小值为%s, 最大值为%s\n", minHostBinary, maxHostBinary);
String minIPBinary = netBinary + minHostBinary;
String maxIPBinary = netBinary + maxHostBinary;
String minIPDecimal = ipBinaryToDotDecimal(minIPBinary);
String maxIPDecimal = ipBinaryToDotDecimal(maxIPBinary);
System.out.printf("IP地址%s表示的范围是%s-%s\n", ipAddress, minIPDecimal, maxIPDecimal);
}

/**
* 将二进制ip地址转换为点分十进制
*/
public static String ipBinaryToDotDecimal(String binary) {
String[] strArray = new String[4];
for (int i = 0; i < 4; i++) {
int startIndex = i * 8;
int endIndex = startIndex + 8;
strArray[i] = String.valueOf(Integer.parseInt(binary.substring(startIndex, endIndex), 2));
}
return String.join(".", strArray);
}

/**
* 获取主机号二进制的最大值或者最小值
* 按照要求,除掉全为1或者全为0,用于预留地址
*/
public static String getHostBinary(String binary, boolean max) {
String target, replace;
if (max) {
target = "1";
replace = "0";
} else {
target = "0";
replace = "1";
}
String external = binary.replaceAll("[01]", target);
return external.substring(0, external.length() - 1) + replace;
}


/**
* 二进制补0操作
*/
public static String complement(String ipBinary) {
int length = ipBinary.length();
if (length == 8) {
return ipBinary;
} else {
return String.format("%8s", ipBinary).replace(' ', '0');
}
}
}

2.3.2 配置自由放行规则

1
2
3
4
5
# 对10.0.0.0/24开放tcp协议的9999端口
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/24" port protocol="tcp" port="9999" accept' && firewall-cmd --reload

# 移除
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/24" port protocol="tcp" port="9999" accept' && firewall-cmd --reload

2.3.3 配置自由禁止规则

1
2
3
4
5
# 对10.0.0.0/24禁止tcp协议的9999端口
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/24" port protocol="tcp" port="9999" drop' && firewall-cmd --reload

# 移除
firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="10.0.0.0/24" port protocol="tcp" port="9999" drop' && firewall-cmd --reload

三、端口转发

将本机80的请求转发到指定机器的9999端口

1
2
3
4
# 添加转发
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=9999:toaddr=10.0.0.10 --zone=public --permanent
# 移除转发
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=9999:toaddr=10.0.0.10 --zone=public --permanent

四、参考致谢

浅谈linux系统firewalld防火墙常用策略(ipv6、ipv4)_firewall-cmd ipv6_cs阿坤dn的博客-CSDN博客

Linux 服务器 Firewalld 防火墙配置端口转发_51CTO博客_linux防火墙设置firewalld

通俗易懂解释IP段192.168.1.0/24和192.168.0.0/16_运维_PHP面试网

firewall-cmd 命令,Linux firewall-cmd 命令详解:Linux上新用的防火墙软件,跟iptables差不多的工具 - Linux 命令搜索引擎

192.168.0.1/27 表示什么molaifeng的博客-CSDN博客/27

发布:2023-02-16 20:50:00
修改:2023-02-21 14:31:30
链接:https://meethigher.top/blog/2023/centos-firewall/
标签:devops 
付款码 打赏 分享
shift+ctrl+1可控制目录显示