0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ローカルDNSサーバーを作って、「この接続は保護されています」の通信してみた

0
Last updated at Posted at 2026-01-31

1.1. 動機

k8s を使用して遊んでいたときにこれを以下の状態でローカル内からアクセスできるようにしたいと思った。

  • IPアドレスアクセスではなく、ドメイン名でのアクセス
  • HTTPS通信
  • 「この接続は保護されています」の状態

image.png

1.2. 構成

1.2.1. ネットワーク構成

機器名 IPアドレス OS 役割
Router 192.168.8.1 OpenWrt ルーター
L2SW - - レイヤー2スイッチ
PC1 192.168.8.10 Windows 11 アクセス元
PC2 192.168.8.20 Ubuntu 22.04.5 LTS DNSサーバー、各種サーバー

※PC2には 192.168.8.20 のほかに、各サーバーに対応したIPアドレスを振っている前提とする。

構成イメージ:

1.2.2. 作成ドメイン

今回は以下のようなドメインを例にサーバーにアクセスするものとする。

ドメイン名 IPアドレス
auth.mysystem.com.lan 192.168.8.21

1.3. DNSサーバー構築

1.3.1. パッケージインストール

パッケージ名 用途
bind9 DNSサーバー本体
bind9utils 設定確認用ツール(named-checkconf等)
bind9-doc ドキュメント
sudo apt update
sudo apt install bind9 bind9utils bind9-doc

1.3.2. DNS動作設定

権威サーバーとして動作させるため。再帰クエリを無効化することで、自分が管理するドメインの情報のみを提供する。

sudo vi /etc/bind/named.conf.options

設定内容:

options {
        ...
        # --- 追記 ---
        recursion no;  # 権威DNSサーバーとして動作
        allow-query { localhost; 192.168.8.0/24; };  # ローカルネットワークのみ許可
        # ------------
        ...
};

1.3.3. ローカルゾーン作成

以下を実行し、設定内容を記載する。

sudo vi /etc/bind/named.conf.local

設定内容:

zone "mysystem.com.lan" {
       type master;
       file "/etc/bind/zones/mysystem.com.lan";
};

今回のアクセス対象のドメインはauth.mysystem.com.lanだが、以下のようなドメインにもアクセスすることを考えて上記の設定としている。

  • www.mysystem.com.lan など

1.3.4. ゾーンファイル作成

以下を実行し、設定内容を記載する。

sudo mkdir -p /etc/bind/zones
sudo vi /etc/bind/zones/mysystem.com.lan

設定内容:

$TTL    604800
@       IN      SOA     auth.mysystem.com.lan. admin.mysystem.com.lan. (
                              2024012501         ; Serial
                              604800             ; Refresh
                              86400              ; Retry
                              2419200            ; Expire
                              604800 )           ; Negative Cache TTL
;
@       IN      NS      auth.mysystem.com.lan.
auth    IN      A       192.168.8.21

Aレコードには対応するIPアドレスを記載する。

※Netplan設定などで、IPアドレスを振り忘れないように注意。

1.3.5. 設定確認とサービス起動

1.3.5.1. 設定ファイルの文法チェック

# 全体設定の確認
sudo named-checkconf

# ゾーンファイルの確認
sudo named-checkzone mysystem.com.lan /etc/bind/zones/mysystem.com.lan

1.3.5.2. サービス再起動

sudo systemctl restart bind9
sudo systemctl status bind9

1.3.6. 動作確認

# Aレコード確認
dig @127.0.0.1 auth.mysystem.com.lan

1.3.7. サーバーへアクセス

PC1のWindowsの設定から、優先DNSをPC2の192.168.8.20にし、代替DNSを192.168.8.1とする。

ポート443で動くauth.mysystem.com.lanに対応するサーバーを0.0.0.0などで起動すればPC1からアクセスできる。

ただ、保護されたアクセスにはなっていない。

※できなければファイヤーウォールなどの設定を見る。

1.4. 証明書作成

1.4.1. ルートCA証明書の作成

ディレクトリ作成

mkdir -p ~/ca/root-certs

CA秘密鍵の生成

openssl genrsa -out ~/ca/root-certs/rootCA.key 2048

CA証明書の生成(有効期限: 10年)

サブジェクト情報は以下のようにした。

フィールド 説明
C JP 国コード(Country)
ST Some-State デフォルト値
O Internet Widgits Pty Ltd デフォルト値
CN My Root CA コモンネーム(Common Name)
openssl req -x509 -new -nodes -key ~/ca/root-certs/rootCA.key \
    -sha256 -days 3650 \
    -out ~/ca/root-certs/rootCA.crt \
    -subj "/C=JP/ST=Some-State/O=Internet Widgits Pty Ltd/CN=My Root CA"

1.4.2. サーバー証明書の作成

ディレクトリ作成

mkdir -p ~/ca/mysystems/auth.mysystem.com.lan

サーバー秘密鍵の生成

openssl genrsa -out ~/ca/mysystems/auth.mysystem.com.lan/server.key 3072

CSRの生成

openssl req -new -key ~/ca/mysystems/auth.mysystem.com.lan/server.key \
    -out ~/ca/mysystems/auth.mysystem.com.lan/server.csr \
    -subj "/CN=auth.mysystem.com.lan"

SAN設定ファイルの作成

cat > ~/ca/mysystems/auth.mysystem.com.lan/server.ext << EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = auth.mysystem.com.lan
EOF

CA証明書でサーバー証明書に署名(有効期限: 1年)

openssl x509 -req -in ~/ca/mysystems/auth.mysystem.com.lan/server.csr \
    -CA ~/ca/root-certs/rootCA.crt \
    -CAkey ~/ca/root-certs/rootCA.key \
    -CAcreateserial \
    -out ~/ca/mysystems/auth.mysystem.com.lan/server.crt \
    -days 365 \
    -sha256 -extfile ~/ca/mysystems/auth.mysystem.com.lan/server.ext

証明書の検証

openssl verify -CAfile ~/ca/root-certs/rootCA.crt \
    ~/ca/mysystems/auth.mysystem.com.lan/server.crt

出来あがるディレクトリ構成は以下の通り。

~/ca/
├── root-certs/
│   ├── rootCA.key
│   └── rootCA.crt
└── mysystems/
    └── auth.mysystem.com.lan/
        ├── server.key
        ├── server.crt
        ├── server.csr
        └── server.ext

1.5. 証明書のインストール

以下記録するのを忘れていたためAIに書いてもらった。

  1. PC1 に ~/ca/root-certs/rootCA.crtを持ってきてをダブルクリックする。
  2. 「証明書のインストール」をクリック
  3. 保存場所: 「ローカルコンピューター」を選択
  4. 「次へ」をクリック
  5. 証明書ストア: 「証明書をすべて次のストアに配置する」を選択
  6. 「参照」をクリック → 「信頼されたルート証明機関」を選択
  7. 「次へ」→「完了」をクリック

1.6. 動作確認

PC1のブラウザ(Chrome)で https://auth.mysystem.com.lan にアクセス

「この接続は保護されています」と表示されれば成功

image.png

0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?