diff options
Diffstat (limited to 'services/nuldoc/public/blog/posts/2024-02-03/install-wireguard-on-personal-server/index.html')
| -rw-r--r-- | services/nuldoc/public/blog/posts/2024-02-03/install-wireguard-on-personal-server/index.html | 100 |
1 files changed, 45 insertions, 55 deletions
diff --git a/services/nuldoc/public/blog/posts/2024-02-03/install-wireguard-on-personal-server/index.html b/services/nuldoc/public/blog/posts/2024-02-03/install-wireguard-on-personal-server/index.html index 60a72a00..68b5a90a 100644 --- a/services/nuldoc/public/blog/posts/2024-02-03/install-wireguard-on-personal-server/index.html +++ b/services/nuldoc/public/blog/posts/2024-02-03/install-wireguard-on-personal-server/index.html @@ -15,7 +15,7 @@ <meta name="Hatena::Bookmark" content="nocomment"> <link rel="icon" type="image/svg+xml" href="/favicon.svg"> <title>【備忘録】 個人用サーバに WireGuard を導入する|REPL: Rest-Eat-Program Loop</title> - <link rel="stylesheet" href="/style.css?h=81dfc0b483beda175e3e17562faac7c9"> + <link rel="stylesheet" href="/style.css?h=c3724e2d900f13528c82005e79a8ec23"> </head> <body class="single"> <header class="header"> @@ -119,16 +119,14 @@ まずは個人用サービスをホストしている Ubuntu のサーバに WireGuard をインストールする。 </p> <div class="codeblock"> - <pre class="highlight" style="background-color:#f5f5f5"><code>$ sudo apt install wireguard -</code></pre> + <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line">$ sudo apt install wireguard</div></code></pre> </div> <p> 次に、WireGuard で使用する鍵を生成する。 </p> <div class="codeblock"> - <pre class="highlight" style="background-color:#f5f5f5"><code>$ wg genkey | sudo tee /etc/wireguard/server.key | wg pubkey | sudo tee /etc/wireguard/server.pub -$ sudo chmod 600 /etc/wireguard/server.{key,pub} -</code></pre> + <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line">$ wg genkey | sudo tee /etc/wireguard/server.key | wg pubkey | sudo tee /etc/wireguard/server.pub +</div><div class="codeblock-line">$ sudo chmod 600 /etc/wireguard/server.{key,pub}</div></code></pre> </div> </section> <section id="section--install-wireguard-client"> @@ -137,28 +135,26 @@ $ sudo chmod 600 /etc/wireguard/server.{key,pub} 公式サイトから各 OS 向けのクライアントソフトウェアを入手し、インストールする。次に、設定をおこなう。 </p> <div class="codeblock"> - <pre class="highlight" style="background-color:#f5f5f5"><code><span style="color: #6e7781"># クライアント 1 の場合 -</span><span style="color: #953800">[Interface]</span><span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #0550ae">Address</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">10.10.1.2/32</span> -<span style="color: #0550ae">PrivateKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><クライアント 1 の秘密鍵></span> -<span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #953800">[Peer]</span><span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #0550ae">PublicKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの公開鍵></span> -<span style="color: #0550ae">AllowedIPs</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの外部 IP アドレス>/32</span> -<span style="color: #0550ae">Endpoint</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの外部 IP アドレス>:51820</span> -</code></pre> + <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line"><span style="color: #6e7781"># クライアント 1 の場合</span> +</div><div class="codeblock-line"><span style="color: #953800">[Interface]</span> +</div><div class="codeblock-line"><span style="color: #0550ae">Address</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">10.10.1.2/32</span> +</div><div class="codeblock-line"><span style="color: #0550ae">PrivateKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><クライアント 1 の秘密鍵></span> +</div><div class="codeblock-line"> +</div><div class="codeblock-line"><span style="color: #953800">[Peer]</span> +</div><div class="codeblock-line"><span style="color: #0550ae">PublicKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの公開鍵></span> +</div><div class="codeblock-line"><span style="color: #0550ae">AllowedIPs</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの外部 IP アドレス>/32</span> +</div><div class="codeblock-line"><span style="color: #0550ae">Endpoint</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの外部 IP アドレス>:51820</span></div></code></pre> </div> <div class="codeblock"> - <pre class="highlight" style="background-color:#f5f5f5"><code><span style="color: #6e7781"># クライアント 2 の場合 -</span><span style="color: #953800">[Interface]</span><span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #0550ae">Address</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">10.10.1.3/32</span> -<span style="color: #0550ae">PrivateKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><クライアント 2 の秘密鍵></span> -<span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #953800">[Peer]</span><span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #0550ae">PublicKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの公開鍵></span> -<span style="color: #0550ae">AllowedIPs</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの外部 IP アドレス>/32</span> -<span style="color: #0550ae">Endpoint</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの外部 IP アドレス>:51820</span> -</code></pre> + <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line"><span style="color: #6e7781"># クライアント 2 の場合</span> +</div><div class="codeblock-line"><span style="color: #953800">[Interface]</span> +</div><div class="codeblock-line"><span style="color: #0550ae">Address</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">10.10.1.3/32</span> +</div><div class="codeblock-line"><span style="color: #0550ae">PrivateKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><クライアント 2 の秘密鍵></span> +</div><div class="codeblock-line"> +</div><div class="codeblock-line"><span style="color: #953800">[Peer]</span> +</div><div class="codeblock-line"><span style="color: #0550ae">PublicKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの公開鍵></span> +</div><div class="codeblock-line"><span style="color: #0550ae">AllowedIPs</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの外部 IP アドレス>/32</span> +</div><div class="codeblock-line"><span style="color: #0550ae">Endpoint</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの外部 IP アドレス>:51820</span></div></code></pre> </div> <p> <code>PrivateKey</code> や <code>PublicKey</code> は鍵ファイルのパスではなく中身を書くことに注意。 @@ -170,32 +166,29 @@ $ sudo chmod 600 /etc/wireguard/server.{key,pub} 一度サーバへ戻り、WireGuard の設定ファイルを書く。 </p> <div class="codeblock"> - <pre class="highlight" style="background-color:#f5f5f5"><code>$ sudo vim /etc/wireguard/wg0.conf -</code></pre> + <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line">$ sudo vim /etc/wireguard/wg0.conf</div></code></pre> </div> <div class="codeblock"> - <pre class="highlight" style="background-color:#f5f5f5"><code><span style="color: #953800">[Interface]</span><span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #0550ae">Address</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">10.10.1.1/32</span> -<span style="color: #0550ae">SaveConfig</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">true</span> -<span style="color: #0550ae">PrivateKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの秘密鍵></span> -<span style="color: #0550ae">ListenPort</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">51820</span> -<span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #953800">[Peer]</span><span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #0550ae">PublicKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><クライアント 1 の公開鍵></span> -<span style="color: #0550ae">AllowedIPs</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">10.10.1.2/32</span> -<span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #953800">[Peer]</span><span style="color: #24292f;background-color: #f6f8fa"> -</span><span style="color: #0550ae">PublicKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><クライアント 2 の公開鍵></span> -<span style="color: #0550ae">AllowedIPs</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">10.10.1.3/32</span> -</code></pre> + <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line"><span style="color: #953800">[Interface]</span> +</div><div class="codeblock-line"><span style="color: #0550ae">Address</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">10.10.1.1/32</span> +</div><div class="codeblock-line"><span style="color: #0550ae">SaveConfig</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">true</span> +</div><div class="codeblock-line"><span style="color: #0550ae">PrivateKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><サーバの秘密鍵></span> +</div><div class="codeblock-line"><span style="color: #0550ae">ListenPort</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">51820</span> +</div><div class="codeblock-line"> +</div><div class="codeblock-line"><span style="color: #953800">[Peer]</span> +</div><div class="codeblock-line"><span style="color: #0550ae">PublicKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><クライアント 1 の公開鍵></span> +</div><div class="codeblock-line"><span style="color: #0550ae">AllowedIPs</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">10.10.1.2/32</span> +</div><div class="codeblock-line"> +</div><div class="codeblock-line"><span style="color: #953800">[Peer]</span> +</div><div class="codeblock-line"><span style="color: #0550ae">PublicKey</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069"><クライアント 2 の公開鍵></span> +</div><div class="codeblock-line"><span style="color: #0550ae">AllowedIPs</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #24292f;background-color: #f6f8fa">=</span><span style="color: #24292f;background-color: #f6f8fa"> </span><span style="color: #0a3069">10.10.1.3/32</span></div></code></pre> </div> <p> 次に、WireGuard のサービスを起動する。 </p> <div class="codeblock"> - <pre class="highlight" style="background-color:#f5f5f5"><code>$ sudo systemctl enable wg-quick@wg0 -$ sudo systemctl start wg-quick@wg0 -</code></pre> + <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line">$ sudo systemctl enable wg-quick@wg0 +</div><div class="codeblock-line">$ sudo systemctl start wg-quick@wg0</div></code></pre> </div> </section> <section id="section--configure-firewall"> @@ -204,26 +197,23 @@ $ sudo systemctl start wg-quick@wg0 続けてファイアウォールを設定する。まずは WireGuard が使用する UDP のポートを開き、<code>wg0</code> を通る通信を許可する。 </p> <div class="codeblock"> - <pre class="highlight" style="background-color:#f5f5f5"><code>$ sudo ufw allow 51820/udp -$ sudo ufw allow in on wg0 -$ sudo ufw allow out on wg0 -</code></pre> + <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line">$ sudo ufw allow 51820/udp +</div><div class="codeblock-line">$ sudo ufw allow in on wg0 +</div><div class="codeblock-line">$ sudo ufw allow out on wg0</div></code></pre> </div> <p> 次に、80 や 443 などの必要なポートについて、<code>wg0</code> を経由してのアクセスのみ許可する。 </p> <div class="codeblock"> - <pre class="highlight" style="background-color:#f5f5f5"><code>$ sudo ufw allow in on wg0 to any port 80 proto tcp -$ sudo ufw allow in on wg0 to any port 443 proto tcp -</code></pre> + <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line">$ sudo ufw allow in on wg0 to any port 80 proto tcp +</div><div class="codeblock-line">$ sudo ufw allow in on wg0 to any port 443 proto tcp</div></code></pre> </div> <p> 最後に、<code>ufw</code> を有効にする。 </p> <div class="codeblock"> - <pre class="highlight" style="background-color:#f5f5f5"><code>$ sudo ufw status -$ sudo ufw enable -</code></pre> + <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line">$ sudo ufw status +</div><div class="codeblock-line">$ sudo ufw enable</div></code></pre> </div> </section> <section id="section--connect-each-other"> |
