在CentOS系统上设置SFTP防火墙规则通常涉及使用 firewalld
或 iptables
。以下是使用这两种方法的详细步骤:
启动并启用firewalld服务:
sudo systemctl start firewalld
sudo systemctl enable firewalld
允许SFTP流量:
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
验证规则是否已添加:
sudo firewall-cmd --list-all
你应该能在输出中看到 ssh
服务被列出,并且状态为 active
。
启动并启用iptables服务:
sudo systemctl start iptables
sudo systemctl enable iptables
允许SFTP流量:
sudo iptables -A INPUT -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
保存iptables规则:
CentOS 7及以上版本使用 firewalld
作为默认防火墙管理工具,但如果你使用的是CentOS 6或其他版本,可以使用以下命令保存规则:
sudo service iptables save
重启iptables服务:
sudo service iptables restart
验证规则是否添加成功:
sudo iptables -L -n
你应该能看到允许TCP端口22的规则。
--source
选项来限制来源IP。通过以上步骤,你应该能够在CentOS系统上成功配置SFTP防火墙规则。如果有任何问题,请检查防火墙规则和SELinux配置。