在Debian系统中,可以使用 UFW(Uncomplicated Firewall)或 iptables 来限制访问。以下是使用这两种方法的详细步骤:
使用UFW限制访问
-
安装UFW(如果尚未安装):
sudo apt update sudo apt install ufw
-
启用UFW:
sudo ufw enable
-
允许特定IP地址访问:
sudo ufw allow from 192.168.1.0/24 to any port 23
这条命令允许来自192.168.1.0/24网络的IP地址访问Telnet服务(端口23)。
-
拒绝其他IP地址访问:
sudo ufw deny 23
这条命令拒绝所有其他IP地址访问Telnet服务。
-
查看UFW状态:
sudo ufw status
使用iptables限制访问
-
安装iptables(如果尚未安装):
sudo apt update sudo apt install iptables
-
设置默认策略:
sudo iptables -P INPUT DROP sudo iptables -P FORWARD DROP sudo iptables -P OUTPUT ACCEPT
-
允许特定IP地址访问:
sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT
这条命令允许IP地址为192.168.1.100的设备访问服务器。
-
允许本地回环接口连接:
sudo iptables -A INPUT -i lo -j ACCEPT
-
允许已有连接和相关连接:
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
-
保存iptables规则:
sudo netfilter-persistent save