# PiVPN起手式
###### tags: `Raspberry Pi`、`Linux`、`Debian`、`VPN`
## 安裝
### [Step1] 前置更新
首先你要有的是
* **一個固定IP**,假如有內網的話則要在Gateway裡設定連線到外網的IP與PORT。
* 一台可以上網LINUX設備,我這邊用樹莓派4B。
建議先更新樹莓派,這步驟各位都是知道的我就點到就好
```bash=
$ sudo apt update
$ sudo apt upgrade
```
### [Step2] CURL安裝
輸入下方指令開始下載,載入中途就會開始進行設定
```bash=
$ curl -L https://install.pivpn.io | bash
```
下載完會自動跳轉畫面,按下`<Ok>`

提醒接下來設定VPN Server是需要實體IP的。
接下來要設定IP與Gateway,按下`<Ok>`

先問你是不是現在的網路設定就好?
是就選`<Yes>`,繼續往下設定。
不是就選`<No>`,就會進入手動設定IP畫面。

#### 手動設定IP




#### 使用者設定


#### 安裝VPN模式
跟你說這邊有支援下面兩個VPN協定。
用 <kbd> </kbd> 空白鍵選完按下 <kbd>enter</kbd>

#### 以下以OpenVPN做示範
#### 設定連線模式
先告訴你預設大概是使用UDP等設定,問你要不要去手動更改設定。
可以接受就按`<No>`,要改動就按`<Yes>`

選擇使用的模型,我選UDP,用 <kbd> </kbd> 空白鍵選完按下 <kbd>enter</kbd>

#### 設定PORT

#### 設定DNS
這邊選擇要使用的DNS Server,若有自己的就選`Custom`,這邊選擇`Google`。

問你要使用自己的網域嗎?要就按下`<Yes>`並輸入自己的網域名稱。

選擇是要自己設定DNS還是使用固定IP自動連線

OpenVPN 2.4有提供較快速的安全機制,有使用到OpenVPN 2.4以上可以按下`<Yes>`

#### 生成憑證加密方式
選擇憑證加密的方式,這邊使用256位元進行加密。

#### 收尾階段


問是不是要啟動自動安全更新,要就選`<Yes>`。
接下來等他設定一下

到這邊看到這個就是安裝完成,也已經啟動好了。

完成後會提示你安裝完成,需要重開。

## 新增使用端
```bash=
$ pivpn add
```
enter完就會出下面幾個提問
```bash=
#設定名稱
Enter a Name for the Client: kagami
#檔案有效期限(以日計算)
How many days should the certificate last? 1080
Enter the password for the client: #輸入密碼
Enter the password again to verify: #再次輸入密碼
```
若順利的話,接下來應該看得到這一串處理流程。
```bash=
spawn ./easyrsa build-client-full kagami
Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
Generating an EC private key
writing new private key to '/etc/openvpn/easy-rsa/pki/easy-rsa-974.rTrkBP/tmp.SHSzYv'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-974.rTrkBP/tmp.rpIc3q
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'kagami'
Certificate is to be certified until Aug 11 08:04:08 2023 GMT (1080 days)
Write out database with 1 new entries
Data Base Updated
Client's cert found: kagami.crt
Client's Private Key found: kagami.key
CA public Key found: ca.crt
tls Private Key found: ta.key
========================================================
Done! kagami.ovpn successfully created!
kagami.ovpn was copied to:
/home/pi/ovpns
for easy transfer. Please use this profile only on one
device and create additional profiles for other devices.
========================================================
```
其中要注意的是,完成後會生成一份`你設定的名稱.ovpn`檔案提供給OpenVPN使用,位置是`/home/pi/ovpns`。
## 顯示所有ovpn清單
```bash=
$ pivpn -l
```

## 顯示目前連線清單
```bash=
$ pivpn -c
```

## 移除ViVPN
```bash=
$ pivpn -u
```
中間會出現一些問答
```bash=
::: Do you wish to completely remove PiVPN configuration and installed packages from your system? (You will be prompted for each package) [y/n]: y
::: Do you wish to remove dnsutils from your system? [Y/n]: y
::: Do you wish to remove iptables-persistent from your system? [Y/n]: y
::: Do you wish to remove openvpn from your system? [Y/n]: y
::: Do you wish to remove grepcidr from your system? [Y/n]: y
::: Do you wish to remove expect from your system? [Y/n]: y
::: Do you wish to remove unattended-upgrades from your system? [Y/n]: y
```
移除完會出現下方訊息,會提示需要重啟。
```bash=
::: Removing VPN configuration files...
:::
::: Removing pivpn system files...
:::
::: Finished removing PiVPN from your system.
::: Reinstall by simpling running
:::
::: curl -L https://install.pivpn.io | bash
:::
::: at any time!
:::
It is strongly recommended to reboot after un-installation.
Would you like to reboot now? [y/n]:
```