0

I have recently set up a headless Rasperry Pi, using Raspberry Pi OS Lite.

I set up Wireguard and some UFW rules.

The outcome that I would like to have:

  • all network traffic is blocked except for traffic that goes through the wireguard connection
  • basically: the vpn would be on all the time. if the vpn is not on, then the pi should not communicate to the outside world

so these are the UFW settings that I set up:

# Reset existing rules to start fresh
sudo ufw reset

# Set default deny policies
sudo ufw default deny incoming
sudo ufw default deny outgoing

# Allow all incoming and outgoing traffic on wg0
sudo ufw allow in on wg0 from any to any
sudo ufw allow out on wg0 from any to any

# Specific allowances (local network and torrent client port)
sudo ufw allow from 192.168.178.0/24
sudo ufw allow to 192.168.178.0/24
sudo ufw allow in 51413/tcp

# Enable UFW
sudo ufw enable

for wireguard I saved the config as /etc/wireguard/wg0.conf and using it looks promising.

  • running wireguard itself works (I checked with sudo wg show -> it shows the interface for wg0)
  • setting the UFW rules works (I checked with sudo ufw status)
sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
Anywhere on wg0            ALLOW       Anywhere                  
Anywhere                   ALLOW       192.168.178.0/24          
192.168.178.0/24           ALLOW       Anywhere                  
51413/tcp                  ALLOW       Anywhere                  
Anywhere (v6) on wg0       ALLOW       Anywhere (v6)             
51413/tcp (v6)             ALLOW       Anywhere (v6)             

Anywhere                   ALLOW OUT   Anywhere on wg0           
Anywhere (v6)              ALLOW OUT   Anywhere (v6) on wg0

However, I am observing that the behaviour is not exactly what I hoped to get. basically the pi can't connect to the outside... what did I miss?

1 Answer 1

0

I managed to fix it. UFW wasn't the issue, as the configuration is actually correct. The problem was the VPN.

What I observed was no connectivity after a certain amount of time. When resetting everything and reconnecting the VPN, it would work, but then after some time it would stop working.

I needed to add a "stay alive" parameter in the wireguard configuration:

[Peer]
...
PersistentKeepalive=25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .