homepage/content/post/wireguard-vps-android.md

76 lines
3.6 KiB
Markdown
Raw Permalink Normal View History

2019-07-07 00:12:17 +00:00
+++
2019-07-07 01:36:48 +00:00
title = "Wireguard Gateway & Android"
2019-07-07 12:41:56 +00:00
date = 2019-07-07T14:41:00+02:00
2019-07-07 00:12:17 +00:00
author = "MH"
cover = ""
tags = ["VPN", "Wireguard", "Android", "Setup"]
2019-07-07 01:36:48 +00:00
description = "Setup a Linux Wireguard Gateway and connet with Android"
2019-07-07 00:12:17 +00:00
showFullContent = false
2019-07-07 12:41:56 +00:00
draft = false
2019-07-07 00:12:17 +00:00
+++
2019-07-07 00:15:06 +00:00
2019-07-07 01:36:48 +00:00
# Setup your Linux Server
2019-07-07 00:15:06 +00:00
2019-07-07 12:41:56 +00:00
Your Linux server should be accessible via a public IP by UDP.
If the IP address changes, use DynDNS.
2019-07-07 00:15:06 +00:00
2019-07-07 01:36:48 +00:00
First **install WireGuard** on your Linux server. On the official website you will find
the right way for your Linux distro:
[WireGuard.com/install](https://www.wireguard.com/install/)
2019-07-07 00:15:06 +00:00
2019-07-07 01:36:48 +00:00
Then **configure** the Gateway.
2019-07-07 12:41:56 +00:00
We will use the range 100.64.0.0/10 (RFC 6598) because it doesn't colide with private IPv4 adresses (RFC 1918).
2019-07-07 00:15:06 +00:00
2019-07-07 01:40:44 +00:00
echo '[Interface]' > /etc/wireguard/wg0.conf
echo "PrivateKey = $(wg genkey)" >> /etc/wireguard/wg0.conf
echo 'ListenPort = 50002' >> /etc/wireguard/wg0.conf
echo "Address = 100.64.0.1/10" >> /etc/wireguard/wg0.conf
2019-07-07 00:15:06 +00:00
2019-07-07 01:36:48 +00:00
iptables -t nat -A POSTROUTING -s 100.64.0.0/10 -o eth0 -j MASQUERADE
2019-07-07 01:40:44 +00:00
systemctl enable --now wg-quick@wg0
2019-07-07 00:15:06 +00:00
Don't forget to save the iptables rules for the next start. The easiest way is to include this config in wg0.conf:
PostUp = iptables -I FORWARD -i eth0 -j ACCEPT; iptables -I FORWARD -o eth0 -j ACCEPT; iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i eth0 -j ACCEPT; iptables -D FORWARD -o eth0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
2019-07-11 13:11:29 +00:00
2019-07-07 12:41:56 +00:00
To get the public key (you need it later on):
2019-07-07 00:15:06 +00:00
2019-07-11 13:05:22 +00:00
wg pubkey <<<$(grep PrivateKey /etc/wireguard/wg0.conf | cut -d ' ' -f3)
2019-07-07 00:15:06 +00:00
2019-07-07 12:41:56 +00:00
Now the gateway is configured and running. To get some information, type in wg and use systemd:
2019-07-07 01:40:44 +00:00
systemctl status wg-quick@wg0
wg
Enable IP forwarding in the Linux kernel by uncommenting or adding (uncommenting) `net.ipv4.ip_forward = 1` in /etc/sysctl.conf to persist the setting between system restarts. Use sysctl -w net.ipv4.ip_forward=1 to enable IP forwarding immediately without having to reboot.
2019-07-07 01:40:44 +00:00
2019-07-07 01:36:48 +00:00
# Setup your Android
2019-07-07 00:15:06 +00:00
2019-07-07 01:36:48 +00:00
Download the App from [F-Droid](https://f-droid.org/en/packages/com.wireguard.android/) or [Google Play](https://play.google.com/store/apps/details?id=com.wireguard.android).
2019-07-07 00:15:06 +00:00
2024-04-26 23:04:42 +00:00
In WireGuard, you must manually set an IP address for each client. The ``100.64.0.0/10`` area has hosts from ```100.64.0.1``` to ```100.127.255.254```, the first one is already occupied by the gateway, so we use ```100.64.0.0.2```.
2019-07-07 00:15:06 +00:00
2019-07-07 12:41:56 +00:00
![]({{<siteurl>}}img/wireguard-android/A.jpg) | ![]({{<siteurl>}}img/wireguard-android/B.jpg) | ![]({{<siteurl>}}img/wireguard-android/C.jpg)
-------|----------|--------
Open the app and "Create from scratch" | Generate a key pair. Make a note of the public key, you will need it later. Name the configuration and add the client IP address with 32 mask. | Add a peer. Now enter the gateway information. Allowed IPs is ```0.0.0.0/0```.
2019-07-07 00:15:06 +00:00
2019-07-07 01:36:48 +00:00
# Add Android Client to Server
2019-07-07 00:15:06 +00:00
2019-07-07 12:41:56 +00:00
Now add the client information to the gateway and restart the interface.
2019-07-07 01:36:48 +00:00
[Peer] >> /etc/wireguard/wg0.conf
PublicKey = <Client-Pub-Key> >> /etc/wireguard/wg0.conf
2019-07-07 12:41:56 +00:00
AllowedIPs = 100.64.0.2/32 >> /etc/wireguard/wg0.conf
2019-07-07 00:15:06 +00:00
2019-07-11 13:05:22 +00:00
systemctl restart wg-quick@wg0 && systemctl status wg-quick@wg0
2019-07-07 00:15:06 +00:00
2019-07-07 01:36:48 +00:00
# Sources
2019-07-07 12:41:56 +00:00
[Wireguard Quickstart](https://www.wireguard.com/quickstart/)
2019-07-07 00:29:27 +00:00
2019-07-07 12:41:56 +00:00
Heise [Besser tunneln](https://www.heise.de/select/ct/2019/5/1551091519824850)
2019-07-07 00:29:27 +00:00
2019-07-07 12:41:56 +00:00
Witepaper [WireGuard: Next Generation Kernel Network Tunnel](https://www.wireguard.com/papers/wireguard.pdf)
2019-07-07 01:36:48 +00:00
2019-07-07 12:41:56 +00:00
Demo Video [Test WireGuard VPN net roaming](https://video.obermui.de/videos/watch/5009724a-a670-4130-bc99-4ab820773da6)