如何在 Linux 上通过命令行配置防火墙如何在 Linux 上通过命令行配置防火墙如何在 Linux 上通过命令行配置防火墙如何在 Linux 上通过命令行配置防火墙
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 Linux 上通过命令行配置防火墙

当您想要在 Linux 上配置防火墙时,iptables 可能是您首先想到的工具。当然,iptables 有 GUI 前端,可以简化 iptables 的学习曲线。但是,如果您不是 GUI 工具的忠实粉丝,则可以考虑 CSF(ConfigServer Security & Firewall)。 CSF 是一种使用 Perl 编写的易于使用且多功能的防火墙配置工具,在无头服务器环境中运行良好。使用 CSF,您可以通过其命令行界面或 CSF 配置文件轻松配置高级防火墙规则,例如状态数据包检查和入侵检测过滤器。

所有主要 Linux 发行版均支持 CSF,包括 CentOS、Redhat、Fedora、Ubuntu、Debian 和 openSUSE。截至撰写本文时,当前版本支持 CenOS/RHEL v7-v8、Debian v8-v10、Ubuntu v18-v20 和 Fedora v30。

您可以在流行的 Web 托管控制面板平台(例如 cPanel/DirectAdmin)上使用 CSF,因为 CSF 的默认安装中已经提供了此类平台的预配置。

以下指南介绍如何在 Linux 上使用 CSF 安装和配置防火墙规则

在 Linux 上安装 CSF

您可以从官方网站下载 CSF。 CSF 的 tar 存档附带一个安装脚本。


$ wget https://download.configserver.com/csf.tgz
$ tar xvfvz csf.tgz
$ cd csf
$ sudo ./install.sh

首次配置CSF

测试CSF所需的所有iptables模块是否可用。


$ sudo /etc/csf/csftest.pl

Testing ip_tables/iptable_filter...OK
Testing ipt_LOG...OK
Testing ipt_multiport/xt_multiport...OK
Testing ipt_REJECT...OK
Testing ipt_state/xt_state...OK
Testing ipt_limit/xt_limit...OK
Testing ipt_recent...OK
Testing xt_connlimit...OK
Testing ipt_owner/xt_owner...OK
Testing iptable_nat/ipt_REDIRECT...OK
Testing iptable_nat/ipt_DNAT...OK

RESULT: csf should function on this server

为了使 CSF 正常工作,您需要确保没有使用任何其他 iptables 配置工具(例如 APF 和 BFD),因为 CSF 可能会与它们发生冲突。因此,请运行以下脚本来删除 APF/BFD 作为保护措施。如果脚本抛出“apf:命令未找到”错误,请不要担心。


$ sudo /etc/csf/remove_apf_bfd.sh

现在您已准备好在系统上配置 CSF。所有 CSF 相关脚本和配置都安装在 /etc/csf 中。

默认情况下,CSF 以测试模式启动,这意味着防火墙规则未完全生效。要禁用此测试模式并自定义防火墙规则,请修改 CSF 配置。


$ sudo vi /etc/csf/csf.conf

# Change to 0 to disable TESTING mode
TESTING = "0"
. . .
# Allow incoming TCP ports
TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995"

# Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,53,80,110,113,443"

# Allow incoming UDP ports
UDP_IN = "20,21,53"

# Allow outgoing UDP ports
UDP_OUT = "20,21,53,113,123"
. . .

修改 /etc/csf/csf.conf 后,请确保按如下方式重新启动 CSF。 iptables 规则中的任何必要更改都将根据修改后的 CSF 配置自动进行。


$ sudo csf -r

除了CSF配置文件之外,您还可以使用csf命令通过命令行界面配置防火墙。 csf 命令提供了用于配置防火墙规则的综合选项,如下所示。


Usage: /usr/sbin/csf [option] [value]

Option              Meaning
-h, --help          Show this message
-l, --status        List/Show iptables configuration
-l6, --status6      List/Show ip6tables configuration
-s, --start         Start firewall rules
-f, --stop          Flush/Stop firewall rules (Note: lfd may restart csf)
-r, --restart       Restart firewall rules
-q, --startq        Quick restart (csf restarted by lfd)
-sf, --startf       Force CLI restart regardless of LFDSTART setting
-a, --add ip        Allow an IP and add to /etc/csf.allow
-ar, --addrm ip     Remove an IP from /etc/csf.allow and delete rule
-d, --deny ip       Deny an IP and add to /etc/csf.deny
-dr, --denyrm ip    Unblock an IP and remove from /etc/csf.deny
-df, --denyf        Remove and unblock all entries in /etc/csf.deny
-g, --grep ip       Search the iptables rules for an IP match (incl. CIDR)
-t, --temp          Displays the current list of temp IP entries and their TTL
-tr, --temprm ip    Remove an IPs from the temp IP ban and allow list
-td, --tempdeny ip ttl [-p port] [-d direction]
                    Add an IP to the temp IP ban list. ttl is how long to
                    blocks for (default:seconds, can use one suffix of h/m/d).
                    Optional port. Optional direction of block can be one of:
                    in, out or inout (default:in)
-ta, --tempallow ip ttl [-p port] [-d direction]
                    Add an IP to the temp IP allow list (default:inout)
-tf, --tempf        Flush all IPs from the temp IP entries
-cp, --cping        PING all members in an lfd Cluster
-cd, --cdeny ip     Deny an IP in a Cluster and add to /etc/csf.deny
-ca, --callow ip    Allow an IP in a Cluster and add to /etc/csf.allow
-cr, --crm ip       Unblock an IP in a Cluster and remove from /etc/csf.deny
-cc, --cconfig [name] [value]
                    Change configuration option [name] to [value] in a Cluster
-cf, --cfile [file] Send [file] in a Cluster to /etc/csf/
-crs, --crestart    Cluster restart csf and lfd
-w, --watch ip      Log SYN packets for an IP across iptables chains
-m, --mail [addr]   Display Server Check in HTML or email to [addr] if present
-lr, --logrun       Initiate Log Scanner report via lfd
-c, --check         Check for updates to csf but do not upgrade
-u, --update        Check for updates to csf and upgrade if available
-uf                 Force an update of csf
-x, --disable       Disable csf and lfd
-e, --enable        Enable csf and lfd if previously disabled
-v, --version       Show csf version

如果您想随时卸载 CSF,只需运行以下命令即可。


$ sudo /etc/csf/uninstall.sh
©2015-2025 艾丽卡 support@alaica.com