ntftables is by now the preferred method. It also has the benefit that we do not need to hard-code an interface. In this case, nftables can identify the correct interface to forward the packets to (typically the Internet) solely by looking at the kernel's routing table

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2023-01-28 12:09:24 +01:00
parent 4c74cc9277
commit 590cb5f86f
2 changed files with 16 additions and 13 deletions
+15 -12
View File
@@ -30,8 +30,21 @@ A properly configured firewall is ***highly*** recommended for any Internet-faci
## Enable NAT on the server
### `nftables` (most distributions)
On your server, add the following to the `[INTERFACE]` section of your `/etc/wireguard/wg0.conf`:
```bash
PostUp = nft add table ip wireguard; nft add chain ip wireguard wireguard_chain {type nat hook postrouting priority srcnat\; policy accept\;}; nft add rule ip wireguard wireguard_chain counter packets 0 bytes 0 masquerade; nft add table ip6 wireguard; nft add chain ip6 wireguard wireguard_chain {type nat hook postrouting priority srcnat\; policy accept\;}; nft add rule ip6 wireguard wireguard_chain counter packets 0 bytes 0 masquerade
PostDown = nft delete table ip wireguard; nft delete table ip6 wireguard
```
You may need to install `nftables` using `sudo apt-get install nftables`.
### `iptables` (older distributions)
If you are using a different distribution, you may need to use `iptables` instead of `nftables`. In this case, add the following to the `[INTERFACE]` section of your `/etc/wireguard/wg0.conf`:
```bash
PostUp = iptables -w -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -w -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -w -t nat -D POSTROUTING -o eth0 -j MASQUERADE; ip6tables -w -t nat -D POSTROUTING -o eth0 -j MASQUERADE
@@ -41,19 +54,9 @@ PostDown = iptables -w -t nat -D POSTROUTING -o eth0 -j MASQUERADE; ip6tables -w
!!! warning "**Important:** Substitute interface"
**Without the correct interface name, this will not work!**
Substitute `eth0` in the preceding lines to match the Internet-facing interface. This may be `enp2s0` or similar on more recent Ubuntu versions (check, e.g., `ip a` for details about your local interfaces).
<!-- markdownlint-enable code-block-style -->
Substitute `eth0` in the preceding lines to match the Internet-facing interface. This may be `enp2s0` or similar on more recent Ubuntu versions (check, e.g., `ip a` for details about your local interfaces). If you are unsure, you can use `ip a` to find the correct interface name. The interface name is the one that is connected to the Internet.
<!-- markdownlint-disable code-block-style -->
!!! warning "**Important:** Debian Bullseye (Debian 11) and Raspian 11"
Debian Bullseye doesn't include iptables per default and uses nftables.
(you may need to install `nftables` using `sudo apt-get install nftables`)
We have to set following rules for PostUP and PostDown:
```bash
PostUp = nft add table ip wireguard; nft add chain ip wireguard wireguard_chain {type nat hook postrouting priority srcnat\; policy accept\;}; nft add rule ip wireguard wireguard_chain oifname "eth0" counter packets 0 bytes 0 masquerade; nft add table ip6 wireguard; nft add chain ip6 wireguard wireguard_chain {type nat hook postrouting priority srcnat\; policy accept\;}; nft add rule ip6 wireguard wireguard_chain oifname "eth0" counter packets 0 bytes 0 masquerade
PostDown = nft delete table ip wireguard; nft delete table ip6 wireguard
```
If you are using the `nftables` method, you do not need to specify the interface name in the `PostUp` and `PostDown` lines.
<!-- markdownlint-enable code-block-style -->
`PostUp` and `PostDown` defines steps to be run after the interface is turned on or off, respectively. In this case, iptables is used to set Linux IP masquerade rules to allow all the clients to share the servers IPv4 and IPv6 address.
+1 -1
View File
@@ -15,7 +15,7 @@ Installing everything we will need for a `wireguard` connections is as simple as
sudo apt-get install wireguard wireguard-tools
```
For Ubuntu 18.04 and lower, you need to do some extra steps:
For Ubuntu 18.04 and before, you need to do some extra steps:
```bash
sudo add-apt-repository ppa:wireguard/wireguard