当前位置: 代码网 > 服务器>服务器>Linux > Linux FTP服务器如何配置防火墙规则

Linux FTP服务器如何配置防火墙规则

2025年03月30日 Linux 我要评论
本文介绍如何在linux系统上为ftp服务器配置防火墙规则,主要讲解iptables和firewalld两种常用工具的使用方法。方法一:使用iptables查看现有规则: 运行以下命令查看当前的ip

linux ftp服务器如何配置防火墙规则

本文介绍如何在linux系统上为ftp服务器配置防火墙规则,主要讲解iptables和firewalld两种常用工具的使用方法。

方法一:使用iptables

  1. 查看现有规则: 运行以下命令查看当前的iptables规则:

    sudo iptables -l
    登录后复制
  2. 添加ftp端口规则: 允许ftp控制端口(21)和数据端口(20)以及数据传输端口范围(例如30000-31000)的访问:

    sudo iptables -a input -p tcp --dport 21 -j accept
    sudo iptables -a input -p tcp --dport 20 -j accept
    sudo iptables -a input -p tcp --dport 30000:31000 -j accept
    登录后复制
  3. 保存规则: 将iptables规则保存到配置文件:

    sudo iptables-save > /etc/iptables/rules.v4
    登录后复制
  4. 设置开机自动加载: 确保iptables规则在系统启动时自动加载:

    sudo sh -c "iptables-save > /etc/network/if-pre-up.d/iptables"
    sudo chmod +x /etc/network/if-pre-up.d/iptables
    登录后复制

方法二:使用firewalld

  1. 安装firewalld: 如果系统未安装firewalld,请先安装:

    • debian/ubuntu: sudo apt-get install firewalld
    • centos/rhel: sudo yum install firewalld
  2. 启动和启用firewalld: 启动firewalld服务并设置开机自启动:

    sudo systemctl start firewalld
    sudo systemctl enable firewalld
    登录后复制
  3. 添加ftp端口: 添加ftp端口(21, 20, 30000-31000)到firewalld的永久规则:

    sudo firewall-cmd --permanent --add-port=21/tcp
    sudo firewall-cmd --permanent --add-port=20/tcp
    sudo firewall-cmd --permanent --add-port=30000-31000/tcp
    登录后复制
  4. 重新加载配置: 重新加载firewalld配置使更改生效:

    sudo firewall-cmd --reload
    登录后复制
  5. 查看规则: 查看当前firewalld规则:

    sudo firewall-cmd --list-all
    登录后复制

重要提示: 以上命令需要root权限。请根据实际情况调整端口号。修改防火墙规则前,建议备份现有规则。 不正确的防火墙配置可能导致网络连接问题。 如有疑问,请参考相关文档。

以上就是linux ftp服务器如何配置防火墙规则的详细内容,更多请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com