I used following commands to prevent -almost- any outgoing internet connection from my Linux computer when VPN connection is not active. It is simple and it works.
Reset ufw rules to default # ufw --force reset Drop all incoming traffic # ufw default deny incoming Drop all outgoing traffic # ufw default deny outgoing Let firewall allow outbound VPN traffic # ufw allow out on tun0 Let firewall allow VPN connection to be established Change IP, port and protocol values as per your VPN server config Example: ufw allow out to 255.255.255.255 port 9999 proto udp # ufw allow out to $serverIP port $port proto $proto Run firewall and make it start automatically during startup # ufw enable
From defending clients to defending code. Developing software professionally in the U.S., offering legal advice on the side, and gaming like it’s still the ’90s. LLM Exeter, PhD(c) in Private Int’l Law. Defender of Kaer Morhen.