Router.HK

華文科技網站

科技新聞

【武漢肺炎】在家工作使用虛擬網絡連接辦公室

在武漢肺炎疫情影響下,不少公司打算安排員工在家工作,可是還需要解決員工存取辦公室的電腦文件檔案。在辦公室安裝Linux主機,建立虛擬網絡伺服器是一個可取而廉價的方案。

Ubuntu是一個較方便快捷安裝的Linux系統,在安裝Linux之後,就可以安裝虛擬網絡相關的package。為簡化Windows及macOS系統連接方法,所以本文介紹安裝L2TP/IPSec的256-but 虛擬網絡的方法。

在安裝 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
conn L2TP-IPSEC
authby=secret
rekey=no
keyingtries=3
type=transport
esp=aes128-sha1
ike=aes128-sha-modp1024
ikelifetime=8h
keylife=1h
left=XXX.XXX.XXX.XXX # your system’s external IP
leftprotoport=17/1701
right=%any
rightprotoport=17/%any
rightsubnet=0.0.0.0/0
auto=add
dpddelay=30
dpdtimeout=120
dpdaction=clear
#force all to be nat’ed. because of iOS
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,加入以下一段。

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

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

[global]
ipsec saref = no
debug tunnel = no
debug avp = no
debug network = no
debug state = no
access control = no
rand source = dev
port = 1701
auth file = /etc/ppp/chap-secrets

[lns default]
ip range = 192.168.2.10-192.168.2.99
local ip = 192.168.2.1
require authentication = yes
name = l2tp
pass peer = yes
ppp debug = no
length bit = yes
refuse pap = yes
refuse chap = yes
pppoptfile = /etc/ppp/options.xl2tpd

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

# Secrets for authentication using CHAP
# client server secret IP addresses
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
$ /etc/init.d/xl2tpd restart

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

VPN用戶電腦設定和連接

在設定 VPN 伺服器後,可以嘗試從其他地方經互聯網連接VPN。L2TP/IPSec VPN 用家需要主機網址或名稱、用戶名稱、密碼、Pre-shared private key用作連接。在 Mac OSX, Windows, iPhone 等,選擇設定 L2TP/IPSec VPN 並輸入用作連接的相關資料便可。

Click Wifi > Open Network Preferences… 出現 Network 設定。

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

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

Click Authentication Settings…

User Authentication: 的 Password: 輸入用戶密碼。
Machine Authentication: 的 Shared Secret: 輸入Pre-shared private key。

Click OK。

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。

【商業服務】Linux Harbour提供虛擬網絡(VPN)顧問服務,安裝及設定Linux伺服器及虛擬網絡伺服器,推廣價只需港幣900起,按此作電郵查詢。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.