温馨提示×

CentOS SFTP如何配置防火墙规则

小樊
47
2025-02-20 20:19:21
栏目: 智能运维
Centos服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在CentOS系统中,配置SFTP(SSH File Transfer Protocol)的防火墙规则通常涉及使用firewalldiptables。以下是使用这两种方法的详细步骤:

使用 firewalld

  1. 启动并启用 firewalld 服务

    sudo systemctl start firewalld
    sudo systemctl enable firewalld
    
  2. 允许SFTP流量: SFTP通常使用TCP端口22。你可以使用firewalld的富规则(rich rules)来允许SFTP流量。

    sudo firewall-cmd --permanent --add-service=sftp
    sudo firewall-cmd --reload
    
  3. 验证规则

    sudo firewall-cmd --list-all
    

    你应该能看到sftp服务被列出,并且状态为active

使用 iptables

  1. 启动并启用 iptables 服务

    sudo systemctl start iptables
    sudo systemctl enable iptables
    
  2. 允许SFTP流量: SFTP使用TCP端口22。你可以添加一条规则来允许这个端口的流量。

    sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
    sudo iptables -A OUTPUT -p tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT
    
  3. 保存规则iptables的规则在系统重启后会丢失,所以你需要保存规则。

    sudo service iptables save
    

    或者使用iptables-persistent包来持久化规则:

    sudo yum install iptables-services
    sudo systemctl enable iptables
    sudo systemctl start iptables
    
  4. 验证规则

    sudo iptables -L -n -v
    

    你应该能看到允许TCP端口22的规则。

注意事项

  • 安全性:确保你的防火墙规则只允许必要的流量。不要开放不必要的端口,以防止潜在的安全风险。
  • 测试:在生产环境中应用防火墙规则之前,先在测试环境中进行测试,确保规则不会影响其他服务。
  • 备份:在修改防火墙规则之前,建议备份当前的规则配置。

通过以上步骤,你应该能够在CentOS系统上成功配置SFTP的防火墙规则。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:CentOS如何配置SFTP防火墙规则

0