linux+怎么更改防火墙设置吗
linux+怎么更改防火墙设置吗详细介绍
在 Linux 里,常见的防火墙管理工具是 iptables 和 firewalld ,以下是更改这两种防火墙设置的方法。
使用
firewalld(适用于 CentOS 7、Fedora 等系统)
firewalld 是一种动态防火墙管理工具,能在不中断现有连接的情况下更改规则。
- 查看防火墙状态:
bash
systemctl status firewalld
若防火墙未启动,可使用以下命令启动:
bash
systemctl start firewalld
若要让防火墙在系统启动时自动启动,可执行:
bash
systemctl firewalld
- 开放端口:
若要开放 8080 端口,可执行以下命令:
bash
firewall-cmd public --add-port/tcp
--zone=public 表示对公共区域应用此规则;--add-port=8080/tcp 表示开放 TCP 协议的 8080 端口;--permanent 表示此规则在重启后依然有效。
3. 重新加载防火墙规则:
bash
firewall-cmd
- 移除端口:
若要移除之前开放的 8080 端口,可执行:
bash
firewall-cmd public --remove-port/tcpfirewall-cmd
- 查看开放的端口:
bash
firewall-cmd public --list-ports
使用
iptables(适用于 Ubuntu、Debian 等系统)
iptables 是一个基础的防火墙工具,直接操作内核中的防火墙规则表。
- 查看当前规则:
bash
iptables
- 开放端口:
若要开放 8080 端口,可执行以下命令:
bash
iptables INPUT tcp ACCEPT
-A INPUT 表示在输入链(INPUT)中添加规则;-p tcp 表示针对 TCP 协议;--dport 8080 表示目标端口为 8080;-j ACCEPT 表示允许此连接。
3. 保存规则:
在 Ubuntu 系统中,可使用 iptables-persistent 工具来保存规则:
bash
iptables-persistentnetfilter-persistent save
- 移除规则:
若要移除之前添加的规则,需要先找出规则编号,再使用-D选项删除:
bash
iptables --line-numbersiptables INPUT 规则编号
9123 iptables iptables INPUT <规则编号>
上述命令需要你拥有管理员权限,所以很多命令前面使用了 sudo。在更改防火墙设置时,要谨慎操作,防止因规则配置不当而影响系统的安全性和网络连接。