|
1 | 1 | --- |
2 | 2 | title: Wireguard VPS Tutorial |
3 | 3 | author_profile: false |
| 4 | +toc: true |
| 5 | +toc_label: "Table of Contents" |
| 6 | +toc_icon: "cog" |
4 | 7 | --- |
5 | | -<iframe width="560" height="315" src="https://www.youtube.com/embed/AQnf-f5QLHQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe> |
| 8 | +{% include video id="AQnf-f5QLHQ" provider="youtube" %} |
6 | 9 |
|
| 10 | +# VPS Wireguard Server |
7 | 11 |
|
8 | | -This is some text to describe what is happening on the page |
| 12 | +## Update and Install Wireguard |
| 13 | + |
| 14 | +First update and upgrade your system, then install wireugard |
| 15 | +```bash |
| 16 | +sudo apt update && sudo apt upgrade -y |
| 17 | +sudo apt install wireguard |
| 18 | +``` |
| 19 | + |
| 20 | +## Key Generation |
| 21 | +```bash |
| 22 | +umask 077 |
| 23 | +wg genkey | tee privatekey | wg pubkey > publickey |
| 24 | +``` |
| 25 | +This will generate base64-encoded public and private keys using the wg utility. |
| 26 | + |
| 27 | +## Enable Forwarding |
| 28 | +```bash |
| 29 | +sudo nano /etc/sysctl.conf |
| 30 | +``` |
| 31 | +Open and edit the sysctl.conf |
| 32 | + |
| 33 | +You'll want to uncomment the line `net.ipv4.ip_forward=1`. Then save your changes and exit. |
| 34 | + |
| 35 | +Type: |
| 36 | +```bash |
| 37 | +sudo sysctl -p |
| 38 | +``` |
| 39 | + |
| 40 | +`net.ipv4.ip_forward=1` -> Should print to the screen |
| 41 | + |
| 42 | +# Wireguard Configuration |
| 43 | +First move the public and private keys to the `/etc/wireguard` directory. |
| 44 | +You may need to become root. |
| 45 | +Then create a file named wg0.conf and move it to the `/etc/wireguard` directory. |
| 46 | +```bash |
| 47 | +sudo mv publickey /etc/wireguard |
| 48 | +sudo mv privatekey /etc/wireguard |
| 49 | +touch wg0.conf |
| 50 | +sudo mv wg0.conf /etc/wireguard |
| 51 | +``` |
| 52 | + |
| 53 | +## Private Key |
| 54 | +Copy the value printed to the screen after running this command. |
| 55 | +```bash |
| 56 | +sudo cat /etc/wireguard/privatekey |
| 57 | +``` |
| 58 | + |
| 59 | +Then open the wg0.conf file: |
| 60 | +```bash |
| 61 | +sudo nano /etc/wireguard/wg0.conf |
| 62 | +``` |
| 63 | + |
| 64 | +## wg0.conf |
| 65 | + |
| 66 | +```bash |
| 67 | +[Interface] |
| 68 | +PrivateKey = |
| 69 | +ListenPort = 51820 |
| 70 | +SaveConfig = true |
| 71 | + |
| 72 | +[Peer] |
| 73 | +PublicKey = |
| 74 | +AllowedIps = 10.0.0.2/32 |
| 75 | +``` |
| 76 | +- Paste the private key from the previous screen in the PrivateKey section. |
| 77 | + |
| 78 | + - In your phone application generate a public/private key pair and copy the public key to place here. |
| 79 | + - For each new Peer(phone) copy the config here. Change the PublicKey and update the AllowedIps to a different IP in the subnet range. Interface should not change |
| 80 | + |
| 81 | + ## NAT and UFW Configs for wg0.conf |
| 82 | + ```bash |
| 83 | +PostUp = ufw route allow in on wg0 out on eth0 |
| 84 | +PostUp = iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE |
| 85 | +PostUp = ip6tables -t nat -I POSTROUTING -o eth0 -j MASQUERADE |
| 86 | +PreDown = ufw route delete allow in on wg0 out on eth0 |
| 87 | +PreDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE |
| 88 | +PreDown = ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE |
| 89 | + ``` |
| 90 | + iI the wg0.conf you will need to add these rules just above the `[Peer]` section. This will allow traffic through wg0 out your eth0 port. NOTE* your main interface may be named something else. |
| 91 | + |
| 92 | +Run `ip route list default` to find out the name of your interface and replace `eth0` with that main interface name. |
| 93 | + |
| 94 | +Final `wg0.conf`: |
| 95 | + |
| 96 | +```bash |
| 97 | +[Interface] |
| 98 | +PrivateKey = somePrivateKeyValuehere= |
| 99 | +ListenPort = 51820 |
| 100 | +SaveConfig = true |
| 101 | + |
| 102 | + |
| 103 | +PostUp = ufw route allow in on wg0 out on eth0 |
| 104 | +PostUp = iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE |
| 105 | +PostUp = ip6tables -t nat -I POSTROUTING -o eth0 -j MASQUERADE |
| 106 | +PreDown = ufw route delete allow in on wg0 out on eth0 |
| 107 | +PreDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE |
| 108 | +PreDown = ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE |
| 109 | + |
| 110 | +[Peer] |
| 111 | +PublicKey = somePublicKeyValueHere= |
| 112 | +AllowedIps = 10.0.0.2/32 |
| 113 | +``` |
| 114 | + |
| 115 | + |
| 116 | +# Firewall |
| 117 | +There are a few firewall rules you will need to configure before you can connect to the server. |
| 118 | + |
| 119 | +```bash |
| 120 | +sudo ufw allow 51820/udp |
| 121 | +sudo ufw allow OpenSSH |
| 122 | +sudo ufw enable |
| 123 | +``` |
| 124 | +Using ufw we allow udp traffic in on port 51820 for Wireguard. |
| 125 | + |
| 126 | +Also we keep port 22 open for SSH connections. This is required to not lose connection to the server. |
| 127 | + |
| 128 | +Then we enable the firewall. |
| 129 | + |
| 130 | +# Enable Wireguard |
| 131 | +```bash |
| 132 | +sudo systemctl enable wg-quick@wg0.service |
| 133 | +``` |
| 134 | +This will enable to wireguard service to start at system boot |
| 135 | + |
| 136 | +```bash |
| 137 | +sudo systemctl start wg-quick@wg0.service |
| 138 | +``` |
| 139 | +This will start the wireguard service. If there are any issues you can track them down with the following command: |
| 140 | +```bash |
| 141 | +sudo systemctl status wg-quick@wg0.service |
| 142 | +``` |
| 143 | + |
| 144 | +# Phone Config |
| 145 | +In the wireguard app on Android create a new connection from scratch and populate the following values: |
| 146 | + |
| 147 | + |
| 148 | +1. Name Ubuntu |
| 149 | +2. Generate the private/public keys with the refresh icon. |
| 150 | +3. Addresses: Set to unique address within the subnet generated on the server. In this case its 10.0.0.2. |
| 151 | +4. Click Add Peer |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +5. The copy the Public Key from the server and paste it here where it says `Public Key`. |
| 157 | +6. Endpoint will be the `public IP` of the server. In this case the VM is on my local network with the IP of `192.168.0.46`. `:51820` is the port specified. |
| 158 | +7. Allowed IPS: `0.0.0.0/0, ::/0` Allow all network traffic |
| 159 | + |
| 160 | + |
| 161 | +# Test the connection |
| 162 | +After completing all the steps above you are ready to test the connection. Press the toggle button on the android application to start the connection to the wireguard server. Then check `rx` and `tx` to see if data is being sent and received from the server. Usually if the `rx` says 0 there are 2 common issues. |
| 163 | + |
| 164 | +1. The keys are incorrect |
| 165 | +2. There is a firewall rule blocking connections |
0 commit comments