pirate / wireguard-docs

📖 Unofficial WireGuard Documentation: Setup, Usage, Configuration, and full example setups for VPNs supporting both servers & roaming clients.
https://docs.sweeting.me/s/wireguard
MIT License
4.45k stars 309 forks source link

Wireguard connection keep alive #35

Closed gurabli closed 4 years ago

gurabli commented 4 years ago

Hi, I have configures WG between two remote points. Everything works perfectly, except that I need to restart wg0 interface once almost each day as somehow the link goes down. Server is on ddns, but the ip is not changing frequently. I have set keepalive in config. Still, I need to do down and up on client to bring back connection once in a day or two. The server ip doesn't change.

Any script or idea how to automate this so that it checks if tunnel is up and running, and if not, to resume connection? Or to really keep alive the connection?

Server running Ubuntu Serve 18.04, client Ubuntu Server 16.04.

Many thanks

pirate commented 4 years ago

Unfortunately, WireGuard only looks up the IP once in the beginning, so it will break whenever your DDNS IP rotates. It sounds like your problem is something else though, do you have KeepAlive set on the client side, the server side, or both? and is the client behind a NAT?

I think you can automate a reconnect after 12hr using PostUP = bash -c 'sleep 43200; wg-quick down /path/to/wg0.conf && wg-quick up /path/to/wg0.conf'.

gurabli commented 4 years ago

Thanks! My problem is that tunnel brakes for an unknown reason for me, not because of IP change. It is dynamic, but changes once in a two weeks, or even less.

I think I have keepalive set only on client side. Should I set both?

And yes, both client and server are behind NAT (local address behind router).

pirate commented 4 years ago

If client and server are both behind a NAT, then you need KeepAlive set on both, because outbound packets are needed on both sides to keep the forwarding entry alive in the NAT table.

If that doesn't work, let me know and I might have some other ideas.

gurabli commented 4 years ago

After adding keep alive to both client and server, the tunnel is not braking anymore, perfect. Thanks for your help!

amihos commented 4 years ago

After adding keep alive to both client and server, the tunnel is not braking anymore, perfect. Thanks for your help!

how you add it to the server? after adding it like this:

[Interface] PrivateKey = 4O3loXmP7HtWwj/5rAWNRG9TzdNU87DbMT/G3BuOnX4= Address = 10.6.0.1/24 ListenPort = 51820 PersistentKeepalive = 25

I got an error:

[#] wg setconf wg0 /dev/fd/63 Line unrecognized: `PersistentKeepalive=25' Configuration parsing error

pirate commented 4 years ago

Use wg-quick for the extended syntax instead of wg.

IITuxtmuxII commented 2 years ago

PersistentKeepalive = 25

You have to put it in peer not in interface