温馨提示×

Debian防火墙如何限制访问

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

在Debian系统中,可以使用 UFW(Uncomplicated Firewall)或 iptables 来限制访问。以下是使用这两种方法的详细步骤:

使用UFW限制访问

  1. 安装UFW(如果尚未安装):

    sudo apt update
    sudo apt install ufw
    
  2. 启用UFW

    sudo ufw enable
    
  3. 允许特定IP地址访问

    sudo ufw allow from 192.168.1.0/24 to any port 23
    

    这条命令允许来自192.168.1.0/24网络的IP地址访问Telnet服务(端口23)。

  4. 拒绝其他IP地址访问

    sudo ufw deny 23
    

    这条命令拒绝所有其他IP地址访问Telnet服务。

  5. 查看UFW状态

    sudo ufw status
    

使用iptables限制访问

  1. 安装iptables(如果尚未安装):

    sudo apt update
    sudo apt install iptables
    
  2. 设置默认策略

    sudo iptables -P INPUT DROP
    sudo iptables -P FORWARD DROP
    sudo iptables -P OUTPUT ACCEPT
    
  3. 允许特定IP地址访问

    sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT
    

    这条命令允许IP地址为192.168.1.100的设备访问服务器。

  4. 允许本地回环接口连接

    sudo iptables -A INPUT -i lo -j ACCEPT
    
  5. 允许已有连接和相关连接

    sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
    
  6. 保存iptables规则

    sudo netfilter-persistent save
    

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

推荐阅读:Linux防火墙如何限制访问

0