---
title: "【武漢肺炎】在家工作使用自建虛擬網絡連接辦公室"
url: https://sammy.hk/%e3%80%90%e6%ad%a6%e6%bc%a2%e8%82%ba%e7%82%8e%e3%80%91%e5%9c%a8%e5%ae%b6%e5%b7%a5%e4%bd%9c%e4%bd%bf%e7%94%a8%e8%87%aa%e5%bb%ba%e8%99%9b%e6%93%ac%e7%b6%b2%e7%b5%a1%e9%80%a3%e6%8e%a5%e8%be%a6%e5%85%ac/
author: Sammy Fung
published: 2020-02-13T17:46:00+08:00
modified: 2021-06-01T17:48:09+08:00
description: "在武漢肺炎疫情影響下，不少公司計劃安排員工在家工作（Work From Home），可是還需要解決員工在家存取辦公室的電腦文件檔案。在辦公室安裝 Linux 主機，建立虛擬網絡伺服器是一個可取而廉價的方案。 Ubuntu 是一個較方便快捷安裝的 Linux 系統，在安裝 Linux 之後，就可以安裝虛擬網絡相關的pac…"
categories: ["開放源碼"]
tags: [linux, vpn, workfromhome]
source: https://sammy.hk
---
[![](https://sammy.hk/wp-content/uploads/2021/06/workfromhome-800x445-1.jpg)](https://sammy.hk/wp-content/uploads/2021/06/workfromhome-800x445-1.jpg)在武漢肺炎疫情影響下，不少公司計劃安排員工在家工作（Work From Home），可是還需要解決員工在家存取辦公室的電腦文件檔案。在辦公室安裝 Linux 主機，建立虛擬網絡伺服器是一個可取而廉價的方案。

[Ubuntu](https://ubuntu.com/) 是一個較方便快捷安裝的 Linux 系統，在安裝 Linux 之後，就可以安裝虛擬網絡相關的package。為簡化 Windows 及 macOS 系統連接方法，所以本文介紹安裝 L2TP/IPSec 的 256 位元虛擬網絡的方法。

在安裝 Ubuntu 18.04 LTS Server Edition 後，用 APT 安裝 strongSwan IPSec VPN 和 Layer 2 Tunneling Protocol 套件。

```
$ apt-get install strongswan xl2tpd
```

編輯 /etc/ipsec.conf，加入以下一段，並把 left 設定為外置 IP 網址。

```
# ipsec.conf – strongSwan IPsec configuration file<br></br>conn L2TP-IPSEC<br></br>authby=secret<br></br>rekey=no<br></br>keyingtries=3<br></br>type=transport<br></br>esp=aes128-sha1<br></br>ike=aes128-sha-modp1024<br></br>ikelifetime=8h<br></br>keylife=1h<br></br>left=XXX.XXX.XXX.XXX # your system’s external IP<br></br>leftprotoport=17/1701<br></br>right=%any<br></br>rightprotoport=17/%any<br></br>rightsubnet=0.0.0.0/0<br></br>auto=add<br></br>dpddelay=30<br></br>dpdtimeout=120<br></br>dpdaction=clear<br></br>#force all to be nat’ed. because of iOS<br></br>forceencaps=yes
```

編輯 /etc/ipsec.secrets，加入以下一段，並把 xxx.xxx.xxx.xxx 設定為外置 IP 網址﹐及用 PSK 設定 private key。

```
# This file holds shared secrets or RSA private keys for authentication.

# RSA private key for this host, authenticating it to any other host
# which knows the public part.
%any xxx.xxx.xxx.xxx : PSK "Your_Pre-shared_Private_key_here"
```

編輯 /etc/ppp/options.xl2tpd，加入以下一段。

```
require-mschap-v2<br></br>refuse-mschap<br></br>ms-dns 8.8.8.8<br></br>ms-dns 8.8.4.4<br></br>asyncmap 0<br></br>auth<br></br>crtscts<br></br>idle 1800<br></br>mtu 1410<br></br>mru 1410<br></br>connect-delay 5000<br></br>lock<br></br>hide-password<br></br>local<br></br># debug<br></br>modem<br></br>name l2tpd<br></br>proxyarp<br></br>lcp-echo-interval 30<br></br>lcp-echo-failure 4
```

編輯 /etc/xl2tpd/xl2tpd.conf，加入以下一段。

```
[global]<br></br>ipsec saref = no<br></br>debug tunnel = no<br></br>debug avp = no<br></br>debug network = no<br></br>debug state = no<br></br>access control = no<br></br>rand source = dev<br></br>port = 1701<br></br>auth file = /etc/ppp/chap-secrets<br></br><br></br>[lns default]<br></br>ip range = 192.168.2.10-192.168.2.99<br></br>local ip = 192.168.2.1<br></br>require authentication = yes<br></br>name = l2tp<br></br>pass peer = yes<br></br>ppp debug = no<br></br>length bit = yes<br></br>refuse pap = yes<br></br>refuse chap = yes<br></br>pppoptfile = /etc/ppp/options.xl2tpd
```

編輯 /etc/ppp/chap-secrets，加入以下一段作為 VPN 登入用戶帳號和密碼。

```
# Secrets for authentication using CHAP<br></br># client server secret IP addresses<br></br>testuser * testpassword *
```

編輯 /etc/sysctl.conf，加入以下一段來啟動 IPv4 的 IP forwarding。

```
net.ipv4.ip_forward=1
```

然後重新載入 sysctl.conf 便可。

```
$ sysctl -p /etc/sysctl.conf
```

使用 iptables 來做 NAT masquerade 讓 VPN 用戶能連接外部網絡及互聯網。

```
$ iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth0 -j MASQUERADE
```

最後重啟 strongSwan IPSec VPN 和 Layer 2 Tunneling Protocol 套件便可。

```
$ /etc/init.d/ipsec restart<br></br>$ /etc/init.d/xl2tpd restart
```

如果外部網絡有防火牆或 Router，需要在防火牆或 Router 設定 Virtual IP 或 Port Forwarding，讓外部 UDP 500, 1701, 4500 的 request 能進入 Linux 伺服器。

[![](https://sammy.hk/wp-content/uploads/2021/06/osx-l2tp-ipsec-vpn-1-1024x857.png)](https://sammy.hk/wp-content/uploads/2021/06/osx-l2tp-ipsec-vpn-1.png)**VPN 用戶電腦設定和連接**

Click Wifi &gt; Open Network Preferences… 出現 Network 設定。

Click + 加入 Interface: VPN (L2TP Over IPSec)，Click Create。

Server Address: 輸入主機網址或名稱。
Account Name: 輸入 VPN 用戶名稱。

Click Authentication Settings…

[![](https://sammy.hk/wp-content/uploads/2021/06/osx-l2tp-ipsec-vpn-2-1024x866.png)](https://sammy.hk/wp-content/uploads/2021/06/osx-l2tp-ipsec-vpn-2.png)User Authentication: 的 Password: 輸入用戶密碼。
Machine Authentication: 的 Shared Secret: 輸入Pre-shared private key。

Click OK。

[![](https://sammy.hk/wp-content/uploads/2021/06/osx-l2tp-ipsec-vpn-3-1024x856.png)](https://sammy.hk/wp-content/uploads/2021/06/osx-l2tp-ipsec-vpn-3.png)Click Advanced，在 Options 的 Session Options: click Send all traffic over VPN connection。

Click OK。
Click Apply。

Click Connect 就可以試試連接 VPN 了。

Click Show VPN status in menu bar 可以在 Menu Bar 看見狀態，也可用來 connect 或 disconnect VPN。

如果你喜歡本文，歡迎你透過 Patreon 定期支持開源人網誌工作。

[Linux Harbour](https://linuxharbour.com) 提供商業 Linux 顧問服務。

### 相關文章：

1. [難以預先安裝 Linux 在套裝電腦上嗎？](https://sammy.hk/difficult-preinstallation-brandmade-computer/ "難以預先安裝 Linux 在套裝電腦上嗎？")
2. [換硬碟的好幫手 – SystemRescueCd](https://sammy.hk/harddisk-replacement-with-systemrescuecd/ "換硬碟的好幫手 – SystemRescueCd")
3. [香港Linux軟件庫嚴重短缺](https://sammy.hk/shortage-of-linux-mirrors-in-hong-kong/ "香港Linux軟件庫嚴重短缺")
4. [OpenSSL Heartbleed (CVE-2014-0160) 解決方法](https://sammy.hk/openssl-heartbleed-cve-2014-0160-solution/ "OpenSSL Heartbleed (CVE-2014-0160) 解決方法")
