> For the complete documentation index, see [llms.txt](https://docs-epc.gitbook.io/ncs-documents/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs-epc.gitbook.io/ncs-documents/tutorial/pritunl-for-vpn-server.md).

# Pritunl for VPN server

ผู้ใช้งานสามารถสร้าง VPN Server เองได้เพื่อการจำกัดการเข้าถึงให้เข้าถึงได้เฉพาะผู้ใช้งานที่ได้รับอนุญาต โดยสามารถใช้งานร่วมกับ ( [NAT Gateway](/ncs-documents/networking/nat-gateway-with-ubuntu-vm-host-route.md) )

## Prerequisite

* VPC Network ที่ต้องการให้สามารถเข้าถึงผ่าน VPN ได้ ( [Managing VPC Network](/ncs-documents/networking/vpc-network.md) )
* Instance ที่ใช้สำหรับติดตั้ง Pritunl-Server โดยในส่วนของ Instance Network ให้เลือก VPC ที่ต้องการใช้งาน VPN ( [Launching Instance](/ncs-documents/compute/compute-instance/launch-compute-instance.md) )

## Instruction

### 1. Install

#### ทำการ ssh เข้าไปยัง Instance ที่ต้องการทำการติดตั้ง Pritunl Server แล้วทำการ Run command ดังนี้

* Ubuntu22.04

```bash
sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list << EOF
deb https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse
EOF

sudo tee /etc/apt/sources.list.d/pritunl.list << EOF
deb https://repo.pritunl.com/stable/apt jammy main
EOF

sudo apt --assume-yes install gnupg
wget -qO- https://www.mongodb.org/static/pgp/server-6.0.asc | sudo tee /etc/apt/trusted.gpg.d/mongodb-org-6.0.asc
gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 7568D9BB55FF9E5287D586017AE645C0CF8E292A
gpg --armor --export 7568D9BB55FF9E5287D586017AE645C0CF8E292A | sudo tee /etc/apt/trusted.gpg.d/pritunl.asc
sudo apt update
sudo apt --assume-yes install pritunl mongodb-org
sudo systemctl start pritunl mongod
sudo systemctl enable pritunl mongod
```

> ข้อมูลเพิ่มเติมสำหรับการติดตั้ง Pritunl Server <https://pritunl.com>

### 2. Configuration

เมื่อทำการติดตั้งเสร็จแล้วให้ทำการเข้าใช้งานผ่าน Browser ด้วย External IP

1. เมื่อเข้าสู่ระบบครั้งแรกจะต้องทำการใส่ setup-key โดยจะสามารถหา setup-key ได้ด้วย command

```bash
pritunl setup-key
```

<figure><img src="/files/4nTMO4IGJOjFmmAxZVhc" alt=""><figcaption></figcaption></figure>

2. ต่อไปจะต้องทำการ login โดยใช้ user pritunl และ password สามารถหาได้ด้วยคำสั่ง

```bash
pritunl default-password
```

<figure><img src="/files/H07khRNJKKBFvHvuv8y2" alt=""><figcaption></figcaption></figure>

3. เมื่อทำการ login ครั้งแรกแล้วจะเข้าสู่หน้า Initial Setup ซึ่งจะสามารถแก้ไขข้อมูลของ root user ได้

<figure><img src="/files/KNRJAZiamPEEQnjwYDyM" alt=""><figcaption></figcaption></figure>

### 3. Users and Organizations

1. ทำการ Add Organization เพื่อใช้สำหรับการจัดการกลุ่มผู้ใช้งาน

<figure><img src="/files/lnrdjSZtclXCW06hfGzV" alt=""><figcaption></figcaption></figure>

2. ทำการ Add User ไปยัง Organizations โดยจะสามารถกำหนด pin ได้เพื่อเป็นรหัสการเข้าใช้งาน VPN

<figure><img src="/files/uj2q9c9brf1Q41HOQJqr" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/qHDoqed3Y9OZinfQRFqe" alt=""><figcaption></figcaption></figure>

3. เมื่อทำการสร้าง user เรียบร้อยแล้ว จะต้องทำการ Download profile เพื่อเตรียมนำไปส่งต่อให้ user สำหรับการใช้งาน

<figure><img src="/files/xSpaV95AwZXkdOhqyPPe" alt=""><figcaption></figcaption></figure>

### 4. Servers

ในส่วนของการตั้งค่าการเชื่อมต่อ และ routing ต่างๆ สามารถตั้งค่าได้ในส่วนของ Servers

1. กดไปที่ Add Server และสามรถทำการตั้งค่าได้ดังตัวอย่าง โดยจะมีข้อมูลที่จำเป็นดังนี้

* Port -> Port สำหรับการเชื่อมต่อ VPN โดย [Security group](/ncs-documents/networking/security-group/create-default-security-groups.md) จะต้องทำการเปิด Port นี้ด้วย
* Virtual Network -> CIDR ที่จะถูกใช้โดย Clients ที่มาเชื่อมต่อ VPN โดย Subnet ควรจะมีเพียงพอต่อการใช้งานของจำนวนผู้ใช้งาน
* Bind address -> address ที่จะถูกใช้งานสำหรับ VPN Server โดยเว้นว่างไว้จะเป็นการอนุญาตให้ใช้งานได้ทุก Interface
* Enable DNS Routing -> เปิดใช้งานสำหรับ Routing ที่ผ่านเข้ามายัง Server จะมีการใช้งาน DNS ที่ถูกกำหนดไว้
* Enable WireGuard -> เปิดใช้งานสำหรับการเชื่อมต่อผ่าน OpenVPN
* Ping Interval -> ระยะห่างของช่วงเวลาที่ Server จะทำการตรวจสอบว่าผู้ใช้งานยังทำการเชื่อมต่ออยู่หรือไม่
* Max Clients -> จำนวนผู้ใช้งานเชื่อมต่อพร้อมกันสูงสุด
* Allow Multiple Devices -> อนุญาตให้ผู้ใช้งานสามารถเข้าใช้งาน VPN พร้อมกันได้หลายอุปกรณ์
* Inter-Client Routing -> อนุญาตให้ผู้ใช้งานสามารถสื่อสารถึงกันได้ผ่าน Virtual Network ที่ผู้ใช้งานได้รับ

{% hint style="info" %}
ข้อมูลเพิ่มเติม <https://docs.pritunl.com/docs/configuration-3>
{% endhint %}

<figure><img src="/files/Uh5axAQJ4PP2hlFPZU6Y" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/GPUlojphN1IckbWjNr0y" alt=""><figcaption></figcaption></figure>

2. เมื่อทำการ Add Server เรียบร้อยแล้ว จะมีการ Add route 0.0.0.0/0 มาให้เป็น Default ทำให้ Network traffic ทั้งหมดของผู้ใช้งานจะผ่านมาที่ VPN Server ซึ่งสามารถลบได้ด้วยปุ่ม Remove Route และทำการ Add Route เฉพาะที่ต้องการใช้งานได้

<figure><img src="/files/6CfonyEsrbm1UX2wKqrc" alt=""><figcaption></figcaption></figure>

3. ทำการ Add Route ที่ใช้สำหรับการเข้าใช้งาน Private VPC Network โดยจะทำการยกตัวอย่างหากใช้งาน CIDR 10.10.10.0/24

<figure><img src="/files/eFcUQWDvXZoRtvex6o5S" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/1thtN8y63XAPXWVcZ3Im" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/F1985Q1NkqiSrhp2086r" alt=""><figcaption></figcaption></figure>

4. ทำการ Attach Organization เพื่อทำการกำหนดกลุ่ม User ที่จะใช้งาน Server

<figure><img src="/files/cuC3520ew6eWBIy1MKl7" alt=""><figcaption></figcaption></figure>

5. เมื่อเรียบร้อยแล้วทำการ Start server เพื่อใช้งาน

<figure><img src="/files/t9bUNG2EirCJWQWmYFBT" alt=""><figcaption></figcaption></figure>

### 5. Users

ผู้ใช้งานสามารถเข้าถึง VPN ผ่านทาง OpenVPN หรือ Pritunl client โดยใช้งานไฟล์ user profile ที่ได้ทำการ download มา

<figure><img src="/files/DHrhXhoegT8dovrd5q9y" alt=""><figcaption></figcaption></figure>

### ตัวอย่างการเข้าใช้งานเครื่อง server-1 ซึ่งได้ถูกติดตั้ง Nginx ไว้ โดยทำการเชื่อมต่อ VPN แล้วเข้าใช้งานผ่าน ip 10.10.10.6

<figure><img src="/files/VcS0XpgMmsw4y7fauMYR" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/uHUm7yg9Nmm8pPTR3AUX" alt=""><figcaption></figcaption></figure>
