Debian 10+ 使用nftables 设置防火墙
一、安装部署
1. 安装并启用 nftables
|
1 2 3 4 5 6 7 |
apt update apt install nftables # 若启用了 ufw,关掉避免冲突 ufw disable 2>/dev/null || true # 关闭防火墙 ufw disable |
2. 备份原配置
|
1 |
cp /etc/nftables.conf /etc/nftables.conf.bak.$(date +%F) |
3. 写入配置
|
1 |
vim /etc/nftables.conf |
内容如下
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#!/usr/sbin/nft -f flush ruleset table inet filter { # ============================================================ # 出站:允许访问的外网 IP 一行一个(不限制端口) # ============================================================ set allowed_out_ips { type ipv4_addr flags interval elements = { 1.1.1.1, 2.2.2.2 } } # ============================================================ # 【可选】出站端口白名单 —— 默认不启用 # 需要限制端口时: # 1. 取消下面两个 set 的注释,按业务填写端口 # 2. 在 output 链里注释掉「不限制端口」那一行 # 3. 取消「限制端口」相关规则的注释 # ============================================================ # set allowed_out_tcp_ports { # type inet_service # elements = { # 80, # 443, # 3306 # } # } # set allowed_out_udp_ports { # type inet_service # elements = { # 53, # 123 # } # } # ============================================================ # 【可选】按 IP 单独限制端口(三个 IP 端口不一样时用) # 启用方式:注释 output 里「不限制端口」那行,再取消对应规则注释 # ============================================================ # 1.1.1.1 只允许这些 TCP 端口 # tcp dport { 80, 443 } ip daddr 1.1.1.1 accept # udp dport { 53 } ip daddr 1.1.1.1 accept # 2.2.2.2 只允许这些 TCP 端口 # tcp dport { 80, 443, 8080 } ip daddr 2.2.2.2 accept # ============================================================ # 入站:允许的 TCP 端口 # ============================================================ set allowed_in_tcp_ports { type inet_service elements = { 22, 80, 443, 631, 8090 } } chain input { type filter hook input priority filter; policy drop; iif "lo" accept ct state established,related accept ip protocol icmp accept ip6 nexthdr ipv6-icmp accept iifname { "tap0", "tap2" } accept tcp dport @allowed_in_tcp_ports accept } chain forward { type filter hook forward priority filter; policy drop; } chain output { type filter hook output priority filter; policy drop; oif "lo" accept ct state established,related accept # 内网(本机访问局域网设备;确定不用内网可整段删掉) ip daddr 10.0.0.0/8 accept ip daddr 172.16.0.0/12 accept ip daddr 192.168.0.0/16 accept ip daddr 127.0.0.0/8 accept # ---------------------------------------------------------- # 出站访问 3 个外网 IP —— 默认:不限制端口 # ---------------------------------------------------------- ip daddr @allowed_out_ips accept # 【方案 A】三个 IP 统一限制端口(取消下面注释,并注释掉上一行) # tcp dport @allowed_out_tcp_ports ip daddr @allowed_out_ips accept # udp dport @allowed_out_udp_ports ip daddr @allowed_out_ips accept # 【方案 B】按 IP 分别限制端口(取消上面对应 IP 的注释,并注释掉「不限制端口」那行) # 规则写在文件上方「按 IP 单独限制端口」区域,复制到此处亦可: # # tcp dport { 80, 443 } ip daddr 1.1.1.1 accept # tcp dport { 80, 443, 8080 } ip daddr 2.2.2.2 accept } } |
4. 先测试加载(语法错会报错,不会默默失败)
|
1 2 |
# 加载 或者 修改配置文件之后 都需要用这个命令来重载配置 nft -c -f /etc/nftables.conf && nft -f /etc/nftables.conf |
5. 查看规则
|
1 |
nft list ruleset |
6. 开机自启
|
1 2 3 4 5 6 7 8 |
# 查看是否开启自启 disabled 是不启动 enabled 是启动 systemctl is-enabled nftables # 设置开启自动 systemctl enable nftables # 重启防火墙 systemctl restart nftables |
二、验证
|
1 2 3 4 5 6 7 8 9 10 |
# 入站:本机监听应仍在 ss -tlnp | egrep ':(22|80|631|8090|20022)\s' # 出站:允许 IP 应能通(按你业务端口改,例如 443) curl -m 5 -I http://1.1.1.1 curl -m 5 -I http://2.2.2.2 # 出站:其它公网应失败 curl -m 5 -I https://www.baidu.com # 应超时/失败 ping -c 2 3.3.3.3 # 应失败(未放行 ICMP 出站) |