0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

自宅サーバーのWireGuard初期設定メモ

Last updated at Posted at 2024-12-24

備忘録ナリ。

環境

  • Ubuntu Server 24.04.01 LTS
  • 自作マシン

手順

参考サイト
上記サイトを参考に自身の環境に合わせたもの。

1.インストール

$ sudo apt install -y wireguard

2.サーバー側鍵生成

# 秘密鍵生成
$ wg genkey | sudo tee /etc/wireguard/server.key

# 秘密鍵から公開鍵生成
$ sudo cat /etc/wireguard/server.key | wg pubkey | sudo tee /etc/wireguard/server.pub

$ sudo chmod 400 /etc/wireguard/server.key /etc/wireguard/server.pub

2.クライアント側鍵生成

# 秘密鍵生成
$ wg genkey | sudo tee /etc/wireguard/client1.key

# 秘密鍵から公開鍵生成
$ sudo cat /etc/wireguard/client1.key | wg pubkey | sudo tee /etc/wireguard/client1.pub

$ sudo chmod 400 /etc/wireguard/client1.key /etc/wireguard/client1.pub

クライアントは今後増えることを想定してナンバリングしておく。

3.サーバー側設定

設定ファイル

$ sudo nano /etc/wireguard/wg0.conf
[Interface]
PrivateKey = サーバーの秘密鍵
Address = 10.0.0.1
ListenPort = 51820
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o enp3s0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o enp3s0 -j MASQUERADE

[Peer]
PublicKey = クライアントの公開鍵
AllowedIPs = 10.0.0.2/32

[Peer]
PublicKey = クライアントの公開鍵
AllowedIPs = 10.0.0.3/32
.
.
.

Ctrl+xyEnterで保存。

注意としては、

  • PostUpとPostDownのenp3s0の部分は各自のNICの名前を入力すること
  • クライアントは下に増やしていけば良い
  • IPアドレスは適当に連番とかで

どうでもいいけどNVMe SSD挿すとenp3s0の数字変わって鬱陶しい。

LAN内へのルーティング

$ sudo nano /etc/sysctl.conf

sysctl.confで以下の行を見つけてコメントアウト解除

- # net.ipv4.ip_forward=1
+ net.ipv4.ip_forward=1

反映させる。

$ sudo sysctl -p

起動と有効化

$ sudo systemctl enable wg-quick@wg0
$ sudo systemctl start wg-quick@wg0

ポート開放

$ sudo ufw allow 51820

4.クライアント側設定

client1.confとか適当に名前つける。スマホ想定。

[Interface]
PrivateKey = クライアントの秘密鍵
Address = 10.0.0.2
DNS = 1.0.0.1

[Peer]
PublicKey = サーバーの公開鍵
EndPoint = サーバーのIPアドレス:51820
AllowedIPs = 0.0.0.0/0

保存したらクライアント側で読み込む。

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?