Hướng dẫn cài đặt OpenVPN trên Window Server 2019

Tải và cài đặt OpenVPN

Link: https://community.openvpn.net/Downloads

Windows 64-bit MSI installer: https://swupdate.openvpn.org/community/releases/OpenVPN-2.7_rc3-I010-amd64.msi

Tiến hành cài đặt:

  • Chạy file cài => Chọn Custom
  • Chọn thêm option OpenSSL Utilities >> EasyRSA 3 Certificate Management Scripts (nhớ cài luôn EasyRSA (bộ tạo chứng chỉ))
  • Chọn Install Now

Cấu hình

  1. Tạo CA, server certificate và client certificate
  • Mở Command Prompt với quyền admin.
  • Điều hướng đến thư mục EasyRSA, ví dụ:
    C:\Program Files\OpenVPN\easy-rsa
  • Khởi tạo PKI:

EasyRSA-Start.bat
./easyrsa init-pki
./easyrsa build-ca
./easyrsa build-server-full server nopass
./easyrsa build-client-full client1 nopass
./easyrsa gen-dh

Cấu hình OpenVPN Server

  • File cấu hình mẫu:
    C:\Program Files\OpenVPN\sample-config\server.ovpn
  • Copy thành:
    C:\Program Files\OpenVPN\config\server.ovpn
  • Sửa file server.ovpn

Thêm các cấu hình sau (nếu chưa có, mục đích để client sẽ routing qua server để đi ra internet)

\#Cho client đẩy toàn bộ traffic vào VPN

push "redirect-gateway def1 bypass-dhcp"

\Đẩy DNS về server (khuyến nghị)

push "dhcp-option DNS 1.1.1.1"
push "dhcp-option DNS 8.8.8.8"

Copy certificates đến đúng folder

  • Copy the following files (from the C:\Program Files\OpenVPN\easy-rsa\pkiC:\Program Files\OpenVPN\easy-rsa\pki\issued and C:\Program Files\OpenVPN\easy-rsa\pki\private folders):
    • ca.crt
    • dh.pem
    • server.crt
    • server.key
  • To the C:\Program Files\OpenVPN\config-auto and C:\Program Files\OpenVPN\config folders.

Mở firewall

Mở port 1194 (hoặc port bạn dùng):

netsh advfirewall firewall add rule name="OpenVPN" dir=in action=allow protocol=UDP localport=1194

Chạy OpenVPN Service

  • Mở Services.msc
  • Tìm OpenVPNService
  • Start service

Enable Routing

  1. Bật IP Forwarding trên Windows Server

Windows mặc định KHÔNG cho phép route/NAT, nên phải bật forwarding.

Powershell (Run as Administrator):

Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters -Name IPEnableRouter -Value 1

Rồi restart mạng hoặc reboot server.

  1. Tạo NAT để client dùng IP public của server

CÁCH A – Dùng RRAS (ổn định nhất)

  1. Mở Server Manager → Add Roles → Remote Access
  2. Chọn Routing
  3. Next > Next > Close
  4. Mở Tool Routing and Remote Access
  5. Chuột phải vào tên server > Configure and Enable Routing and Remote Access
  6. Chọn Custom configuration > Tích chọn NAT và LAN routing
  7. Start service
  8. Tiếp tục chọn: IPv4 > NAT > Click phải chuột chọn New Interface > Chọn đúng card mạng đang ra đc internet > Chọn Public interface connected to the Internet > Tick chọn Enable NAT on this interface > OK

CÁCH B – Dùng lệnh NAT (nhanh hơn)

Nếu bạn không muốn cài RRAS, dùng lệnh:

1) Lấy tên card mạng Internet

netsh interface ipv4 show interfaces

Ví dụ tên là: Ethernet0

2) Lấy tên card TAP-OpenVPN

netsh interface ipv4 show interfaces

Thường là: Ethernet 2 hoặc Local Area Connection 3.

3) Tạo NAT

netsh routing ip nat install
netsh routing ip nat add interface "Ethernet0" full
netsh routing ip nat add interface "Ethernet 2" private

→ NAT được bật.

Cấu hình trên máy client

Lấy file client.ovpn trong C:\Program Files\OpenVPN\sample-config

Sửa lại IP server:

remote my-server-1 1194

Tìm đến đoạn:

ca ca.crt
cert client.crt
key client.key

Xóa 3 dòng đó đi, thay bằng

<ca>
Nội dung file ca.crt
</ca>

<cert>
Nội dung file client.crt
</cert>

<key>
Nội dung file client.key
</key>

Sau đó gửi file này cho client và kết nối bằng Openvpn Client

Link