ubuntu

ubuntu lamp防火墙如何设置

小樊
39
2025-08-19 10:11:15
栏目: 智能运维

Ubuntu LAMP防火墙常用ufw(默认工具)或iptables配置,以下是具体步骤:

一、使用ufw(推荐,简单易用)

  1. 安装与启用

    sudo apt update && sudo apt install ufw  # 安装(通常预装)
    sudo ufw enable                          # 启用防火墙
    sudo ufw default deny incoming           # 默认拒绝入站,允许出站
    
  2. 允许LAMP必要端口

    sudo ufw allow ssh                       # 允许SSH(默认22端口)
    sudo ufw allow http                      # 允许HTTP(80端口)
    sudo ufw allow https                     # 允许HTTPS(443端口)
    sudo ufw allow mysql                     # 允许MySQL(默认3306端口,需安装数据库)
    
  3. 高级规则(按需添加)

    • 限制IP访问:sudo ufw allow from 192.168.1.0/24 to any port 22(仅允许指定IP段访问SSH)
    • 拒绝特定IP:sudo ufw deny from 10.0.0.5
    • 查看规则:sudo ufw status numbered
    • 删除规则:sudo ufw delete [规则编号]

二、使用iptables(需手动配置)

  1. 安装与基础设置

    sudo apt install iptables              # 安装
    sudo iptables -P INPUT DROP            # 默认拒绝入站
    sudo iptables -P OUTPUT ACCEPT         # 允许出站
    sudo iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT  # 允许已建立的连接
    sudo iptables -A INPUT -i lo -j ACCEPT  # 允许本地回环
    
  2. 允许LAMP端口

    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT    # SSH
    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT    # HTTP
    sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT   # HTTPS
    sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT  # MySQL
    
  3. 保存规则

    sudo iptables-save > /etc/iptables/rules.v4  # 保存规则(重启后生效)
    

三、注意事项

参考来源:

0
看了该问题的人还看了