温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux下如何配置防火墙规则

发布时间:2025-11-19 20:10:51 来源:亿速云 阅读:110 作者:小樊 栏目:系统运维

在Linux下配置防火墙规则,通常使用的是iptables或firewalld这两个工具。下面分别介绍如何使用这两个工具配置防火墙规则。

使用iptables配置防火墙规则

  1. 查看当前iptables规则

    sudo iptables -L -n -v
    
  2. 允许特定IP访问

    sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT
    
  3. 拒绝特定IP访问

    sudo iptables -A INPUT -s 192.168.1.101 -j DROP
    
  4. 允许特定端口访问

    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    
  5. 拒绝特定端口访问

    sudo iptables -A INPUT -p tcp --dport 22 -j DROP
    
  6. 保存iptables规则

    sudo iptables-save > /etc/iptables/rules.v4
    
  7. 恢复iptables规则

    sudo iptables-restore < /etc/iptables/rules.v4
    

使用firewalld配置防火墙规则

  1. 查看当前firewalld状态

    sudo firewall-cmd --state
    
  2. 查看所有开放的端口

    sudo firewall-cmd --list-all
    
  3. 允许特定IP访问

    sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept'
    
  4. 拒绝特定IP访问

    sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.101" reject'
    
  5. 允许特定端口访问

    sudo firewall-cmd --permanent --add-port=80/tcp
    sudo firewall-cmd --permanent --add-port=443/tcp
    
  6. 拒绝特定端口访问

    sudo firewall-cmd --permanent --remove-port=22/tcp
    
  7. 重新加载firewalld配置

    sudo firewall-cmd --reload
    

注意事项

  • iptables:iptables是Linux内核自带的防火墙工具,配置较为底层,规则持久化需要手动保存和恢复。
  • firewalld:firewalld是CentOS 7及以上版本默认的防火墙管理工具,配置较为直观,支持动态更新和区域管理。

根据你的Linux发行版和需求选择合适的工具进行配置。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI