使用nftables為指定Linux用戶強制使用代理
出自md5.pw
更多語言
更多操作
1. 準備
確保 nftables 已經安裝, 且本地有透明代理埠 (比如Xray的dokodemo-door), 這裡就不注重如何開設透明代理
nft --version若沒有安裝, 以Debian13為例
apt update
apt install nftables
systemctl enable nftables拷貝原有的nftables配置, 到一個好找的目錄里, 以 /main/nftables 為例, 再對原路徑進行連結
cp /etc/nftables.conf /main/nftables/
ln -f /main/nftables/nftables.conf /etc/nftables.conf並準備好編輯 /main/nftables/nftables.conf
2. 配置nftables
查看用戶id
cat /etc/passwd這裡假設我在10809埠開設了Xray的dokodemo-door, 需要將1000與1001的用戶id所建立的tcp連接使用代理
請在合適位置添加或與其它的table進行合併
table inet proxy {
chain output {
type nat hook output priority 10;
policy accept;
meta skuid { 1000, 1001 } ip protocol tcp dnat to 127.0.0.1:10809;
}
}保存後並重啟
systemctl restart nftables.service3. 配置sysctl
sysctl 中需要配置IP轉發, 新建一個forward.conf並進行編輯
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1保存好後連結過去, 並讓其生效
ln -f /main/nftables/forward.conf /etc/sysctl.d/
sysctl --system4. 測試
su 1000或1001用户名
curl https://ipinfo.io/ip