Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
gentoo:wireguard [2025/02/05 12:38] – willy | gentoo:wireguard [2025/03/13 13:26] (current) – [WireGuard] willy | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== WireGuard ====== | + | ====== |
- | [[https:// | + | [[https:// |
+ | |||
+ | WireGuard will create an encrypted and protected tunnel between hosts, where each host act as a peer. You need to have at least one host reachable from all the others, of course, but then WireGuard will create a common subnetwork on which all the hosts will see each other. | ||
===== Concepts ===== | ===== Concepts ===== | ||
- | network | + | You should be fmailiar with basic networking concepts like routing, subnets, addresses. |
+ | |||
+ | A **subnet** is a portion of a network | ||
+ | |||
+ | All VPNs work by creating an encrypted **tunnel** between it's peers. This tunnel needs to be initiated from one host to the other (or viceversa) and whatever traffic flows inside the tunnes is not intellegible to anyone else because it's, guess what, encrypted. | ||
- | tunnel | + | To perform the encryption at both ends, some encryption **keys** needs to be shared. The approach used by WireGuard is to use the private/ |
- | keys | ||
===== Installation ===== | ===== Installation ===== | ||
- | You will also need **nftables** if you plan to do port-forwarding | + | These steps need to be followed on every host that participate in the WireGuard tunnel. |
+ | |||
+ | Installing WireGuard on Gentoo is pretty easy since the latest release is always in portage, but you will also need [[gentoo: | ||
<code bash> | <code bash> | ||
emerge -v net-vpn/ | emerge -v net-vpn/ | ||
</ | </ | ||
+ | |||
Create local host private and public keys: | Create local host private and public keys: | ||
Line 22: | Line 30: | ||
wg genkey > / | wg genkey > / | ||
wg pubkey < / | wg pubkey < / | ||
+ | chmod 500 / | ||
</ | </ | ||
+ | |||
+ | You will need these two keys for the configuration below. | ||
===== Configuration ===== | ===== Configuration ===== | ||
- | Each WireGuard tunnel | + | A WireGuard tunnel |
- | So, create | + | So, create |
<file - wg0.conf> | <file - wg0.conf> | ||
[Interface] | [Interface] | ||
- | PrivateKey = << local private key >> | + | PrivateKey = << local host private key >> |
Address = 10.100.0.1/ | Address = 10.100.0.1/ | ||
- | ListenPort = << | + | ListenPort = << |
[Peer] | [Peer] | ||
- | PublicKey = << remote | + | PublicKey = << remote |
- | Endpoint = << | + | Endpoint = << |
- | AllowedIPs = 10.100.0.2/ | + | AllowedIPs = 10.100.0.2/ |
PersistentKeepAlive = 25 | PersistentKeepAlive = 25 | ||
</ | </ | ||
Where: | Where: | ||
- | * You can have as many peers as you need to connect to the local host | + | * You can have as many peers as you need to connect to the local host, just create one [Peer] block for each one. |
* The // | * The // | ||
- | * The //Address// is the __local host__ address on the tunnel subnetwork | + | * The //Address// is the __local host__ address on the __tunnel subnetwork__ (usually, a new subnet you are not using already) |
- | * The // | + | * The // |
* The // | * The // | ||
- | * The // | + | * The // |
- | * The //peer port// is the // | + | * The // |
- | * The // | + | |
* The // | * The // | ||
- | Each peer (host) connecting to the WireGuard tunnel will need one of these files. If you have two hosts (tipycal setup), assume that you need two // | + | Each host connecting to the WireGuard tunnel will need one of these files. These configuration files should usually be symmetrical to each other. |
Link the startup scripts and set it to start on boot: | Link the startup scripts and set it to start on boot: | ||
Line 61: | Line 71: | ||
</ | </ | ||
- | ===== Port Forwarding | + | ===== Remote access |
- | + | ||
- | nft list tables | + | There are tons of WireGuard tutorials online on how to use WireGuard |
- | nft add table ip wg | + | |
- | nft -- add chain ip wg prerouting { type nat hook prerouting priority -100 \; } | + | |
- | nft add chain ip wg postrouting { type nat hook postrouting priority 100 \; } | + | |
- | nft add rule ip wg prerouting tcp dport 2022 dnat to 10.70.0.1 dport 22 | + | |
- | nft add rule ip wg prerouting tcp dport 22 dnat to 10.70.0.1 | + | |
- | nft add rule ip wg postrouting ip daddr 10.70.0.1 masquerade | + | |
- | + | ||
- | nft add ip wg prerouting ' | + | |