sudo apt update
sudo apt install iptables iproute2 iptables-persistent -y
iptables
sudo iptables -N TRAFFIC_TRACKING
sudo iptables -A INPUT -j TRAFFIC_TRACKING
sudo iptables -A OUTPUT -j TRAFFIC_TRACKING
sudo iptables -A TRAFFIC_TRACKING -m conntrack --ctstate ESTABLISHED,RELATED -j RETURN
sudo iptables -A TRAFFIC_TRACKING -p tcp --dport 22 -j RETURN
sudo iptables -A TRAFFIC_TRACKING -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A TRAFFIC_TRACKING -m conntrack --ctstate ESTABLISHED -j ACCEPT
sudo iptables -A TRAFFIC_TRACKING -m connbytes --connbytes 0:104857600000 --connbytes-dir both --connbytes-mode bytes -j RETURN
sudo iptables -A TRAFFIC_TRACKING -m connbytes --connbytes 104857600000: --connbytes-dir both --connbytes-mode bytes -j DROP
sudo mkdir -p /etc/iptables
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
sudo iptables -L -v -n -t filter
sudo iptables -L TRAFFIC_TRACKING -v -n
sudo crontab -e
0 0 1 * * /sbin/iptables -Z TRAFFIC_TRACKING
netfilter-persistent
sudo netfilter-persistent save
sudo systemctl restart netfilter-persistent
sudo systemctl start netfilter-persistent
sudo systemctl enable netfilter-persistent
sudo systemctl status netfilter-persistent
监控统计
sudo iptables -L TRAFFIC_TRACKING -v -n | awk '/0.0.0.0\/0/ {sum += $2} END {print "Used GB: ", sum / 1073741824}'
sudo iptables -L TRAFFIC_TRACKING -v -n | grep '0.0.0.0/0' | awk '{sum += $2} END {print "Used GB: " sum/1073741824}'
sudo iptables -L TRAFFIC_TRACKING -v -n | awk '/0.0.0.0\/0/ {sum += $2} END {print "Used MB: " sum/1048576}'