温馨提示×

温馨提示×

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

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

Linux下如何设置防火墙规则

发布时间:2024-09-23 15:52:23 来源:亿速云 阅读:78 作者:小樊 栏目:建站服务器

在 Linux 下,通常使用 iptablesfirewalld 命令来设置和管理防火墙规则

1. 使用 iptables 设置防火墙规则:

iptables 是 Linux 系统中的一个命令行工具,用于配置内核网络过滤规则。以下是一些常用的 iptables 命令示例:

  • 允许来自特定 IP 地址的所有流量:

    sudo iptables -A INPUT -s <IP地址> -j ACCEPT
    
  • 阻止来自特定 IP 地址的所有流量:

    sudo iptables -A INPUT -s <IP地址> -j DROP
    
  • 允许特定端口的流量(例如,允许端口 80 的 HTTP 流量):

    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    
  • 阻止特定端口的流量(例如,阻止端口 443 的 HTTPS 流量):

    sudo iptables -A INPUT -p tcp --dport 443 -j DROP
    
  • 保存 iptables 规则(在某些 Linux 发行版中需要):

    sudo service iptables save
    

2. 使用 firewalld 设置防火墙规则:

firewalld 是另一个用于管理 Linux 发行版中的防火墙的工具。与 iptables 不同,firewalld 使用区域(zones)和服务(services)的概念来组织规则。以下是一些常用的 firewalld 命令示例:

  • 打开 HTTP(端口 80)流量:

    sudo firewall-cmd --permanent --zone=public --add-service=http
    sudo firewall-cmd --reload
    
  • 打开 HTTPS(端口 443)流量:

    sudo firewall-cmd --permanent --zone=public --add-service=https
    sudo firewall-cmd --reload
    
  • 阻止来自特定 IP 地址的所有流量:

    sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="<IP地址>" reject'
    sudo firewall-cmd --reload
    
  • 查看当前活动的防火墙规则:

    sudo firewall-cmd --list-all
    
  • 保存 firewalld 规则:

    sudo firewall-cmd --permanent
    sudo firewall-cmd --reload
    

请注意,这些示例仅适用于默认的防火墙配置。根据您的 Linux 发行版和安装的防火墙软件,您可能需要根据实际情况调整命令和参数。在修改防火墙规则之前,请确保您充分了解这些规则的含义,以免意外阻止重要流量。

向AI问一下细节

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

AI