返回主页 和纸静读模式

Tutorial: Homebrew your own virtual private network on NixOS

作者 gjz010 头像
gjz010
2024-03-29 5 min read
在 NixOS 上自建 OpenVPN 私有网络:服务端与客户端配置、基于 cfssl 的证书认证,以及 Linux 与 Windows 客户端的接入方式。

We will show how to homebrew your own virtual, private network using OpenVPN on NixOS.

Warning

This is NOT a tutorial about building tunnels across some company fireWALL.

Objectives

Suppose:

  • The server runs on server.example.com with default port 1194.
  • The internal LAN of which we expect to access directly on remote machines is 192.168.50.0/24.
  • We assign 192.168.100.0/24 to clients in virtual network.

Server

{pkgs, ...}:
let
  vpnSubnet = "192.168.100.0";
  vpnMask = "255.255.255.0";
  # See next chapter for details
  vpnKeys = ./vpn;
  vpnDevice = "tun0";
in
{
  # Configure firewall.
  networking.firewall.allowedUDPPorts = [ 1194 ];
  networking.firewall.allowedTCPPorts = [ 1194 ];
  networking.firewall.trustedInterfaces = [ vpnDevice ];

  environment.systemPackages = [pkgs.openvpn];

  services.openvpn.servers.remote.config = ''
    tls-server
    dev ${vpnDevice}
    server ${vpnSubnet} ${vpnMask}
    ifconfig-pool-persist /var/openvpn-remote.txt
    dh ${vpnKeys}/dh2048.pem
    ca ${vpnKeys}/ca.pem
    cert ${vpnKeys}/server.pem
    key ${vpnKeys}/server-key.pem
    cipher AES-256-CBC
    auth-nocache
    comp-lzo
    keepalive 10 60
    ping-timer-rem
    persist-tun
    persist-key
  '';
}

Certificate-based authentication

We use certificates for authentication since Pre-Shared keys are not cool.

ca.pem, server.pem and server-key.pem

Certificates can be generated with cfssl. See cfssl.

dh2048.pem

Taken from here:

openssl dhparam -out dh2048.pem 2048

Client

Two files are required for our setup:

  • OpenVPN client configuration: server.ovpn.
  • Client certificate: client.pem and client-key.pem. On Windows a PKCS#12 client-chain.p12 is required.

server.ovpn

We inline the ca.pem (cfssl) into our client configuration.

tls-client
dev tun
remote server.example.com 1194
cipher AES-256-CBC
auth-nocache
keepalive 10 60
resolv-entry infinite
nobind
persist-key
persist-tun
comp-lzo
link-mtu 1557
route 192.168.50.0 255.255.255.0 vpn_gateway
<ca>
[YOUR ca.pem HERE]
</ca>

client.pem and client-key.pem

You need to generate some client certificates according to cfssl.

Warning

One client certificate is required for EVERY client!

Linux + NetworkManager

On NixOS the NetworkManager plugin is required:

{pkgs, ...}:
{
  networking.networkmanager.plugins = [pkgs.networkmanager-openvpn];
}

Windows

OpenVPN on Windows requires the client certificates to be exported as a PKCS#12 certificate chain, say client-chain.p12.

See cfssl.

Additional notes

NixOS Wiki tutorial: https://nixos.wiki/wiki/OpenVPN

gjz010的笔记 (gjz010) · 感谢您的用心卒读

如果这篇文章带给您一丝平静...

请为本篇文章摇一摇折扇,或者留下一纸感言

如果这篇文章带给您一丝平静...

把这一页递给另一位读者,或替自己留下一段地址。

本页链接
https://www.gjz010.com/posts/tutorial/router-openvpn-server/

点按链接框,即可完整选中这一页的地址。

通过邮件分享
上一篇随感gjz010/notes-public
已至最后一卷深夜思绪

茶客沙龙 · 远方来笺

本篇暂未开启远方笺札。茶室主人尚未为此文绑定 Mastodon 讨论串。