在CentOS上配置FTP服务器的防火墙规则,通常需要使用firewalld
或iptables
。以下是使用这两种工具配置防火墙规则的步骤:
firewalld
启动并启用 firewalld
服务:
sudo systemctl start firewalld
sudo systemctl enable firewalld
开放FTP端口: FTP默认使用两个端口:21(控制连接)和20(数据连接)。你可以选择开放这两个端口,或者只开放你需要的端口。
开放21端口:
sudo firewall-cmd --permanent --add-port=21/tcp
开放20端口:
sudo firewall-cmd --permanent --add-port=20/tcp
如果你需要被动模式(PASV),还需要开放一个范围端口(例如50000-50050):
sudo firewall-cmd --permanent --add-port=50000-50050/tcp
重新加载防火墙规则:
sudo firewall-cmd --reload
验证规则是否生效:
sudo firewall-cmd --list-all
iptables
启动并启用 iptables
服务:
sudo systemctl start iptables
sudo systemctl enable iptables
开放FTP端口:
开放21端口:
sudo iptables -A INPUT -p tcp --dport 21 -j ACCEPT
开放20端口:
sudo iptables -A INPUT -p tcp --dport 20 -j ACCEPT
如果你需要被动模式(PASV),还需要开放一个范围端口(例如50000-50050):
sudo iptables -A INPUT -m multiport --dports 50000:50050 -j ACCEPT
保存 iptables
规则:
CentOS 7及以上版本使用firewalld
,但如果你使用的是CentOS 6或其他版本,可以使用以下命令保存规则:
sudo service iptables save
验证规则是否生效:
sudo iptables -L -n
通过以上步骤,你应该能够在CentOS上成功配置FTP服务器的防火墙规则。