咖喱的 Debian 配置备份(Debian 13 用)

备份使用 Debian 时的配置

内核精简

apt install -y linux-image-cloud-amd64

# 查看多余内核并卸载
dpkg -l | grep linux-image
dpkg -l | grep linux-headers

Package 安装

apt install sudo curl vim wget iperf3 rsync btop command-not-found unzip vim bash-completion bind9-dnsutils nftables git gpg kexec-tools -y

curl -sL nxtrace.org/nt |bash

# 自动更新
apt install unattended-upgrades apt-listchanges needrestart -y
dpkg-reconfigure -plow unattended-upgrades

# 时间同步
timedatectl set-ntp false
apt install chrony -y
systemctl enable chronyd --now

# DNS 服务器
apt install unbound openresolv unbound-anchor -y
systemctl mask unbound-resolvconf.service
# unbound-anchor -a /var/lib/unbound/root.key 配置密钥用于 TLS 查询(一般不用)

# 妙妙工具
## apt install plocate fd-find ripgrep axel etckeeper

# 防火墙配置
curl -s https://install.crowdsec.net | sudo sh
apt install crowdsec -y
apt install crowdsec-firewall-bouncer-nftables -y
cscli console enroll key
systemctl restart crowdsec

写入 nftables 基础规则

#!/usr/sbin/nft -f

flush ruleset

############################
# FILTER (IPv4+IPv6)
############################
table inet filter {

  chain input {
    type filter hook input priority filter; policy drop;

    # 1. 基础规则:优先放行 loopback 和已建立/相关的连接
    iif lo accept
    ct state established,related accept
    ct state invalid drop

    # 2. SSH 优化:基于源 IP 限制新建连接速率,防止爆破,避免全局拒绝服务
    # 限制每个独立 IPv4 地址每分钟最多 5 个新建连接
    tcp dport 22 ct state new meter ssh-meter-v4 { ip saddr limit rate 5/minute burst 5 packets } accept
    # 限制每个独立 IPv6 地址每分钟最多 5 个新建连接 (如果通过 IPv6 访问)
    tcp dport 22 ct state new meter ssh-meter-v6 { ip6 saddr limit rate 5/minute burst 5 packets } accept

    # HTTP/HTTPS (按需取消注释)
    tcp dport { 80, 443 } accept
    # meta l4proto { tcp, udp } th dport { 8443 } accept

    # 3. IPv4 ICMP 限速
    ip protocol icmp icmp type {
      echo-request, echo-reply,
      destination-unreachable, time-exceeded, parameter-problem
    } limit rate 50/second burst 100 packets accept

    # 4. IPv6 优化:无条件放行 NDP 及必要控制报文,保障 IPv6 基础连通性
    ip6 nexthdr icmpv6 icmpv6 type {
      destination-unreachable, packet-too-big, time-exceeded, parameter-problem,
      nd-router-solicit, nd-router-advert, nd-neighbor-solicit, nd-neighbor-advert
    } accept

    # IPv6 优化:仅对 Ping (echo-request) 进行限速
    ip6 nexthdr icmpv6 icmpv6 type echo-request limit rate 50/second burst 100 packets accept
    ip6 nexthdr icmpv6 icmpv6 type echo-reply accept

    # 5. DHCPv6 客户端
    udp dport 546 udp sport 547 accept

    # 6. 日志优化:静默丢弃常见的广播/多播等背景噪音,避免污染系统日志
    pkttype { broadcast, multicast } drop
    # (可选) 丢弃局域网常见的 NetBIOS/UPnP 等探测包
    udp dport { 137, 138, 1900, 5353 } drop

    # 7. 记录并丢弃其余输入
    limit rate 5/second burst 25 packets log prefix "NFT INPUT DROP: " flags all
    counter drop
  }

  chain forward {
    type filter hook forward priority filter; policy drop;

    ct state established,related accept
    ct state invalid drop

    # 放行 Docker 转发优化:收紧权限,保证容器网络隔离性
    # 仅允许 Docker 和自定义网桥主动访问外网 (假设外网物理网卡为 eth0,请根据实际情况修改)
    iifname { "docker0", "br-*" } oifname "eth0" accept

    # 注意:外部主动访问容器映射端口的流量,已由上方的  处理
    # 容器之间的互通交由 Docker 自带的隔离策略处理,无需在此处进行全局 accept

    # 记录并丢弃其余转发
    limit rate 5/second burst 25 packets log prefix "NFT FORWARD DROP: " flags all
    counter drop
  }

  chain output {
    type filter hook output priority filter; policy accept;
  }
}

############################
# NAT (IPv4)
############################
table ip nat {
  chain postrouting {
    type nat hook postrouting priority 100;

    # 允许容器网段通过 eth0 伪装上网 (请确保 eth0 是你的真实 WAN 口网卡名)
    ip saddr 172.16.0.0/12 oifname "eth0" masquerade
  }
}

防火墙重启生效

systemctl enable --now nftables
systemctl restart nftables

Unbound 配置

# Unbound configuration file for Debian.
#
# See the unbound.conf(5) man page.
#
# See /usr/share/doc/unbound/examples/unbound.conf for a commented
# reference config file.
#
# The following line includes additional configuration files from the
# /etc/unbound/unbound.conf.d directory.
include-toplevel: "/etc/unbound/unbound.conf.d/*.conf"

server:
    # ===========================================================================
    # 网络和核心设置
    # ===========================================================================

    # 监听本地 IPv4 和 IPv6 接口
    interface: 127.0.0.1
    interface: ::1
    port: 53

    # 允许来自本机的查询
    access-control: 127.0.0.1/32 allow
    access-control: ::1/128 allow

    # 启用 IPv4, IPv6, UDP 和 TCP 查询
    do-ip4: yes
    do-ip6: no
    do-udp: yes
    do-tcp: yes

    # 根据你的 CPU 核心数进行设置。例如,如果你的 CPU 是 4 核,就设置为 4。
    # 这能让 Unbound 并行处理更多请求。
    num-threads: 1
    pidfile: "/run/unbound.pid"
    chroot: ""

    # 建议移除 so-rcvbuf 和 so-sndbuf,让操作系统自动管理。
    # 如果你的服务器负载极高,可以取消注释并设置为 4m 或 8m。
    # so-rcvbuf: 4m
    # so-sndbuf: 4m


    # ===========================================================================
    # 性能和缓存优化
    # ===========================================================================

    # 消息缓存大小,存储 DNS 查询和应答。
    msg-cache-size: 64m
    # RRset 缓存大小,存储 DNS 记录。建议为 msg-cache-size 的两倍。
    rrset-cache-size: 128m

    # 将缓存条目分成多个板块,减少多线程锁竞争。值设为接近 num-threads 的2的幂。
    msg-cache-slabs: 1
    rrset-cache-slabs: 1
    infra-cache-slabs: 1
    key-cache-slabs: 1

    # 开启预取功能,在缓存过期前主动更新热门记录,保持缓存新鲜。
    prefetch: yes
    # 同样为 DNSSEC 密钥开启预取。
    prefetch-key: yes

    # 关键性能选项:开启后,即使记录过期也会立即从缓存返回,并在后台更新。
    # 极大地提升了用户感知的响应速度。
    serve-expired: yes
    # 设定过期记录的TTL(生存时间),防止客户端缓存过期的记录。
    serve-expired-reply-ttl: 30
    # 设定过期记录在缓存中可以被使用的最长时间。0 表示无限制。
    serve-expired-ttl: 86400


    # ===========================================================================
    # 安全和隐私增强 (这些对性能影响极小,但建议保留)
    # ===========================================================================

    # 强化 DNSSEC 数据,防止降级攻击。
    harden-glue: yes
    harden-dnssec-stripped: yes

    # 最小化查询名称,增强隐私保护。
    qname-minimisation: yes

    # 隐藏身份和版本信息。
    hide-identity: yes
    hide-version: yes

    # 使用随机的端口ID,增加对缓存投毒攻击的抵抗力。
    use-caps-for-id: no

    # 启用最小化响应,只返回必要信息,略微减少网络流量。
    minimal-responses: yes

    # 定义私有地址,防止 DNS Rebinding 攻击。
    private-address: 10.0.0.0/8
    private-address: 172.16.0.0/12
    private-address: 192.168.0.0/16

    # 不要查询 localhost 上的权威服务器。
    do-not-query-localhost: yes
    hide-trustanchor: yes

    # TLS 证书包路径,用于 DNSSEC 验证。
    tls-cert-bundle: "/etc/ssl/certs/ca-certificates.crt"
    module-config: "validator iterator"
    # 避免分片的推荐值
    edns-buffer-size: 1232
    # RFC 8198,减少无效查询
    aggressive-nsec: yes

forward-zone:
    name: "."
    forward-tls-upstream: no
    forward-addr: 1.1.1.1        # Cloudflare
    forward-addr: 1.0.0.1
    forward-addr: 8.8.8.8        # Google
    forward-addr: 8.8.4.4

和 kexec 搭配使用的脚本

自动切换新内核重启

以当前内核重启

性能优化

前置

net.ipv4.tcp_congestion_control = bbr
# 从 Debian 13 开始队列算法默认为 fq_codel 无须调整

分支1

// 不建议在低配机器上使用
apt install tuned bpftune
systemctl enable --now tuned
systemctl enable --now bpftune

分支2

bash <(curl -L -s https://cdn.skk.moe/sh/optimize.sh)

# 补全 CF 内核模块(懒人 Xanmod 法)
wget -qO - https://dl.xanmod.org/archive.key | sudo gpg --dearmor -vo /etc/apt/keyrings/xanmod-archive-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/xanmod-archive-keyring.gpg] http://deb.xanmod.org $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/xanmod-release.list
sudo apt update && sudo apt install linux-xanmod-x64v3

其他

Nginx(n.wtf)# Install extrepo
sudo apt update
sudo apt install extrepo -y

# Enable n.wtf repo
sudo extrepo enable n.wtf

# Install Latest Nginx
sudo apt update
sudo apt install nginx-extras -y