---
title: "【OS Unix-like】防火墙,服务器,端口,进程"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/unix-firewall-server-port-process/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "10-计算机、信息技术与工程/03-软件工程与质量保障/CI-CD与质量保障/CI-CD工具链/【OS Unix-like】防火墙,服务器,端口,进程.md"
content_hash: 1662144f40d3c3213ebb3b04440465f0853318ce52a2f871cbc431073f9e4bb0
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# 【OS Unix-like】防火墙,服务器,端口,进程

> **warning · 历史命令边界**
> 文中的 CentOS 7 与部分 `iptables` 服务命令只用于遗留环境。CentOS Linux 7 已于 2024-06-30 停止维护；新系统应先确认发行版、网络管理器、`firewalld`/`nftables`/UFW 的当前实现，再执行变更。迁移步骤见 [受支持Linux发行版的部署迁移检查清单](https://www.pystone.net/notes/linux-distro-migration-checklist/)。

﻿# 【OS Unix-like】防火墙,服务器,端口,进程

> 创建时间：2021/4/27 16:43

* 进程相关操作
  * 服务器与端口
    * 端口分类
    * 常见操作
      * 查看某个端口是否被占用
  * 防火墙相关
    * 介绍
    * 防火墙工具的关系
    * CentOS 7 - firewalld
      * 简介与基本操作
      * 其他操作
    * Ubuntu - UFW
    * iptables
      * 基本操作 - as service
      * not As Service
      * 设置规则
      * save & restore
      * iptables-persistent
      * 规则查看与删除
    * MacOS
  * Ref

## 进程相关操作

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

## 服务器与端口

### 端口分类

**从端口的性质来分** ：
公认端口（WellKnownPorts）：这类端口也常称之为"常用端口"。这类端口的端口号从0到1024，它们紧密绑定于一些特定的服务。通常这些端口的通信明确表明了某种服务的协议，这种端口是不可再重新定义它的作用对象。

注册端口（RegisteredPorts）：端口号从1025到49151。它们松散地绑定于一些服务。程序可根据实际需要自己定义.

动态和/或私有端口（Dynamic and/or PrivatePorts）：端口号从49152到65535。理论上，不应把常用服务分配在这些端口上。实际上，有些较为特殊的程序，特别是一些木马程序就非常喜欢用这些端口，因为这些端口常常不被引起注意，容易隐蔽。

> 常用端口号：
>  21 - FTP
>  25 - 邮件
>  443 - HTTPS
>  80/8080/3128/8081/9080 - HTTP

* * *

> 根据所提供的服务方式
>  TCP协议端口
>  UDP协议端口

### 常见操作

#### 查看某个端口是否被占用

Windows:

netstat -ano

-a: Displays all active TCP connections and the TCP and UDP ports on which the computer is listening.

-n: Displays active TCP connections, however, addresses and port numbers are expressed numerically and no attempt is made to determine names.

-o: Displays active TCP connections and includes the process ID (PID) for each connection. You can find the application based on the PID on the Processes tab in Windows Task Manager. This parameter can be combined with -a, -n, and -p.

Linux:

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

CentOS:

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

## 防火墙相关

### 介绍

防火墙是一组规则。当数据包进出受保护的网络区域时，进出内容（特别是关于其来源、目标和使用的协议等信息）会根据防火墙规则进行检测，以确定是否允许其通过。

![assets/0016 - 【OS Unix-like】防火墙,服务器,端口,进程__resource-001-62cb04773f39.png](/media/b15cc8b279e9ed261aa8.png)

防火墙可以根据协议或基于目标的规则过滤请求。

注意：Linux系统中，防火墙是默认开启的，如需开启，就要主动去开启某个规则。

### 防火墙工具的关系

  * Netfilter - 它在 Linux 内核模块级别控制访问网络栈。
  * iptables - iptables 规则集是管理 Netfilter 钩子的主要命令行工具。
  * nftables - nft 命令行工具，iptables 的替代品。
  * ufw 和 firewalld - 更高级别的 Netfilter 解释器。

### CentOS 7 - firewalld

#### 简介与基本操作

firewalld 是 systemd 家族的一部分。
默认安装在 RedHat 和 CentOS 上，可以安装在 Debian/Ubuntu 机器上。

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

默认情况下，firewalld 处于运行状态，并拒绝所有传入流量，但有几个例外，如 SSH。这意味着你的网站不会有太多的访问者。
对 web 服务器，一般会希望打开 HTTP 和 HTTPS 端口，按照惯例，这两个端口分别被指定为 80 和 443。

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```
```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```
```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

#### 其他操作

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```
```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

### Ubuntu - UFW

sudo ufw version防火墙版本；
sudo ufw status查看防火墙状态；
sudo ufw disable关闭防火墙；
Ubuntu 系统默认已安装了ufw，安装命令：sudo apt-get install ufw
启用：
sudo ufw enable
sudo ufw default deny
以上三条命令已经足够安全了，如果你需要启用其它服务，可使用sudo ufw allow开启。
sudo ufw allow smtp 允许所有的外部IP访问本机的25/tcp (smtp)端口
sudo ufw allow 22/tcp 允许所有的外部IP访问本机的22/tcp (ssh)端口
sudo ufw allow 53 允许外部访问53端口(tcp/udp)
sudo ufw allow from 192.168.1.100 允许此IP访问所有的本机端口
sudo ufw allow proto udp 192.168.0.1 port 53 to 192.168.0.2 port 53
sudo ufw deny smtp 禁止外部访问smtp服务
sudo ufw delete allow smtp 删除上面建立的某条规则

### iptables

https://docs.anaconda.com/anaconda-repository/admin-guide/install/config/adjust-iptables-port80/

Ubuntu Document: https://help.ubuntu.com/community/IptablesHowTo

#### 基本操作 - as service

有些linux上，iptables是service

service iptables status
service iptables stop
service iptables start
service iptables restart

永久关闭或者开启：
chkconfig iptables off
chkconfig iptables on

#### not As Service

In Linux generally, "iptables" isn't a service - it's a command to manipulate the netfilter kernel firewall. You can "disable" (or stop) the firewall by setting the default policies on all standard chains to "ACCEPT", and flushing the rules.

开放指定端口：

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

彻底关闭防火墙：

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

Ref: https://serverfault.com/questions/129086/how-to-start-stop-iptables-on-ubuntu

#### 设置规则

-A : 告诉 iptables 我们要添加规则
OUTPUT : 这条规则应该成为输出链的一部分
-p : 该规则仅使用 TCP 协议的数据包
-d : 目的地址
-j : 当数据包符合规则时要采取的操作是 ACCEPT

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

规则顺序是很重要的。因为 iptables 会对一个请求遍历每个规则，直到遇到匹配的规则。
当遇到匹配时，iptables 不再继续往下检查了。

只需重启服务器，iptables 规则就会全部丢失。如何在每次启动时都加载规则，参见：https://zhuanlan.zhihu.com/p/45920510

#### save & restore

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

#### iptables-persistent

https://www.cnblogs.com/zoro-zero/p/12843198.html
https://www.thomas-krenn.com/en/wiki/Saving_Iptables_Firewall_Rules_Permanently

#### 规则查看与删除

罗列规则

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

删除规则

```csharp
string str1 = String.Empty;
string str2 = String.Empty;

str2 = String.Intern(sb.ToString());

if((object)str1==(object)str2)
    Console.WriteLine(&quot;The strings are equal.&quot;);
else
    Console.WriteLine(&quot;The strings are not equal.&quot;);

```

### MacOS

## Ref

https://zhuanlan.zhihu.com/p/45920510
https://blog.csdn.net/bbwangj/article/details/74502967
https://zhuanlan.zhihu.com/p/29949791

https://serverfault.com/questions/129086/how-to-start-stop-iptables-on-ubuntu
