> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://jimcloud.crisp.help/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# Centos6  有关防火墙命令

在 CentOS 6 中，默认使用 `iptables` 作为防火墙管理工具。以下是一些常用的防火墙命令和操作：

## 1. 查看当前防火墙规则

```bash
sudo iptables -L -n -v
```

## 2. 启动和停止防火墙

- **启动防火墙**：

  
```bash
sudo service iptables start
```

- **停止防火墙**：

  
```bash
sudo service iptables stop
```

- **重启防火墙**：

  
```bash
sudo service iptables restart
```

- **设置防火墙开机自启**：

  
```bash
sudo chkconfig iptables on
```

- **禁止防火墙开机自启**：

  
```bash
sudo chkconfig iptables off
```

## 3. 添加和删除规则

### 允许某个服务（例如 HTTP）

```bash
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
```

### 允许某个端口（例如 8080）

```bash
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
```

### 拒绝某个服务（例如 FTP）

```bash
sudo iptables -A INPUT -p tcp --dport 21 -j DROP
```

### 删除规则

要删除规则，您需要知道规则的编号。首先查看规则列表：

```bash
sudo iptables -L --line-numbers
```

然后使用编号删除规则，例如删除第 2 条规则：

```bash
sudo iptables -D INPUT 2
```

## 4. 保存和恢复规则

- **保存当前规则**：

  
```bash
sudo service iptables save
```

- **恢复规则**：

  
```bash
sudo service iptables restart
```

## 5. 临时规则

如果您希望添加临时规则（重启后失效），可以直接使用 `iptables` 命令，但不保存：

```bash
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
```

## 6. 获取帮助

要获取 `iptables` 的更多帮助和选项，可以使用：

```bash
man iptables
```

通过以上命令，您可以有效地管理 CentOS 6 的防火墙设置。