> 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="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FQW407nHINc7kKLAedwgJ%2FScreenshot%202567-02-16%20at%2012.02.03.png?alt=media&amp;token=6f8f1ac1-4207-4b4a-a67a-85078ca2eaa2" alt=""><figcaption></figcaption></figure>

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

```bash
pritunl default-password
```

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FduCVYR8tfhJkIzCnGEUf%2FScreenshot%202567-02-16%20at%2012.03.00.png?alt=media&amp;token=0fc5fbe9-c63d-408a-80da-2cb014d9386a" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FHPRl05LHQrwj3pGAnD8Q%2FScreenshot%202567-02-16%20at%2012.04.39.png?alt=media&amp;token=8c14f842-9342-4310-b322-ad2efb1ab4b4" alt=""><figcaption></figcaption></figure>

### 3. Users and Organizations

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

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FgfDPzvUxKx8jxc08Xo2Y%2FScreenshot%202567-02-16%20at%2012.07.49.png?alt=media&amp;token=e248be0b-a27b-453e-a4ed-35519bfe8e2f" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2Fsi6SxlaJHKYyweGN6osJ%2FScreenshot%202567-02-16%20at%2016.04.47.png?alt=media&amp;token=efd772cd-284a-4663-a6c7-94033ef760f2" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FjxCoIAFKpRmCcRLKyscc%2FScreenshot%202567-02-16%20at%2016.05.04.png?alt=media&amp;token=92c9fbc8-5265-440a-9f96-5be46e30184d" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FArZmuaI6rWymaopQPUDf%2FScreenshot%202567-02-16%20at%2017.09.18.png?alt=media&amp;token=b12de9d9-b432-48db-8735-d04eecd849ec" 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="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FS0Zy4LWywUQq7HFHSilR%2FScreenshot%202567-02-16%20at%2012.11.34.png?alt=media&amp;token=4775a45a-65cf-48e7-bdc1-a485fbf65efd" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FWs3GoRWksjhDcTG1LAMa%2FScreenshot%202567-02-16%20at%2016.37.01.png?alt=media&amp;token=a3c7feca-233b-4c2f-a503-525ad9008428" 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="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FUK4mlEBsEYH18MOnlrUV%2FScreenshot%202567-02-16%20at%2015.55.19.png?alt=media&amp;token=6cc07100-ec81-4322-9633-321527b91ffb" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2F7tNPBV067pVQhtmTkW76%2FScreenshot%202567-02-19%20at%2010.19.16.png?alt=media&amp;token=4d0fbd49-80d9-4d1f-881b-756dc29e0dcc" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FLxcIallJNKyzDpNJJSl7%2FScreenshot%202567-02-16%20at%2016.27.16.png?alt=media&amp;token=9a08b409-07ae-4fe0-8a0c-b59b2c455e42" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FmZepAX6eYlhIjQv3KYrC%2FScreenshot%202567-02-16%20at%2015.56.21.png?alt=media&amp;token=e2e377c8-09ad-4165-966e-8efc69dc59c8" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FZe2RnwpZTsk84Pc49KHQ%2FScreenshot%202567-02-16%20at%2015.57.06.png?alt=media&amp;token=d325eabf-e23a-435f-99b7-5bf7fc3959ca" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FdlMCqDm8TR5yAPW0On8I%2FScreenshot%202567-02-16%20at%2016.57.59.png?alt=media&amp;token=e5c1483a-baf2-444e-82f2-e6c3aad8898b" alt=""><figcaption></figcaption></figure>

### 5. Users

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

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FhbDZRAMB3IsoXkKamqTt%2FScreenshot%202567-02-16%20at%2017.18.56.png?alt=media&amp;token=3cb19b54-691c-4bc7-9413-b7d5f48fa201" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2F00L8ld7now976Z8KTuS5%2FScreenshot%202567-02-19%20at%2010.06.22.png?alt=media&amp;token=87af407c-40fd-48e9-9a86-aab3b7b769e8" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1352697161-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq2BYmWTMiaonrEL8QBP2%2Fuploads%2FEMlp4ONSo2VM0VvnV7gD%2FScreenshot%202567-02-19%20at%2010.10.38.png?alt=media&amp;token=a05beb48-26d1-4847-b1bc-25df5d42a17c" alt=""><figcaption></figcaption></figure>
