如何在Ubuntu 16.04上設定OpenVPN


在本文中,我們將學習如何在Ubuntu Linux上配置OpenVPN伺服器。是的,我們可以使用VPN(虛擬專用網路)來安全私密地從不受信任的網路連線到工作場所。例如,如果您在酒店或咖啡館,並希望透過智慧手機或筆記型電腦使用Wi-Fi網路安全地訪問工作環境。

OpenVPN是一個功能齊全的開源安全套接字層(SSL) VPN解決方案,它可以適應各種配置。

安裝OpenVPN

要開始安裝,我們需要一臺Ubuntu機器,該機器擁有具有sudo許可權的非root使用者。

使用“sudo”使用者登入Ubuntu機器,然後繼續執行以下步驟。

由於OpenVPN在Ubuntu的預設儲存庫中可用,我們需要更新伺服器,並將安裝easy-rsa包以獲取與我們的VPN伺服器一起使用的內部CA(證書頒發機構)。

$ sudo apt-get update

更新伺服器後,我們將安裝OpenVPN和easy-rsa。

$ sudo apt-get install openvpn easy-rsa

設定CA目錄

OpenVPN是一個TLS/SSL VPN。這意味著它將使用證書來加密伺服器和客戶端之間的流量。為了頒發可信證書,我們將設定我們自己的簡單證書頒發機構(CA)。

我們將使用make-cadir命令將easy-rsa模板目錄複製到我們的主目錄。

$ sudo make-cadir ~/openvpn-ca

更改到新建立的目錄以開始配置CA。

$ cd ~/openvpn-ca

配置CA變數

我們需要編輯目錄中“vars”檔案的值。現在開啟檔案進行編輯。

$ source vars

我們將找到一些可以調整的變數,以確定如何建立我們的證書。在這個演示中,我們將更改其中一些變數。

我們將找到一些設定,這些設定設定新證書的預設值,如下所示。

. . .
export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="SanFrancisco"
export KEY_ORG="Fort-Funston"
export KEY_EMAIL="me@myhost.mydomain"
export KEY_OU="MyOrganizationalUnit"
. . .

根據您的區域和需求編輯這些值。以下是演示目的的資訊。

. . .
# These are the default values for fields
# which will be placed in the certificate.
# Don't leave any of these fields blank.
export KEY_COUNTRY="in"
export KEY_PROVINCE="AP"
export KEY_CITY="Hyderabad"
export KEY_ORG="RIG"
export KEY_EMAIL="ckadarla@rigaps.com"
export KEY_OU="Resource Infomatics Group"
# X509 Subject Field
. . .
We will also edit the KEY_NAME value which is just below this section, for demo purpose we will call as vpn_server
export KEY_NAME="vpn_server"
Save the file and close it.

構建CA(證書頒發機構)

我們可以使用我們在easy-rsa實用程式中設定的變數來構建我們的證書頒發機構,更改到我們在前面步驟中建立的CA目錄,然後使用source命令載入我們編輯過的vars檔案。

$ cd ~/openvpn-
ca$ source
vars$ ./clean-all
$ ./build-ca

這將建立根證書和具有證書的授權金鑰。由於我們已經在vars檔案中提供了資訊,只需按ENTER鍵即可。

Country Name (2 letter code) [US]:
State or Province Name (full name) [NY]:
Locality Name (eg, city) [New York City]:
Organization Name (eg, company) [DigitalOcean]:
Organizational Unit Name (eg, section) [Community]:
Common Name (eg, your name or your server's hostname) [DigitalOcean CA]:
Name [server]:
Email Address [admin@email.com]:
Creating the Server Certificate, Key and Encryption Files.
We will create the Server Certificate, Key and Encryption and also some additional files used for the encryption process.
We can use the below command to generate key certificate and key pair.
$ ./build-key-server serverCountry Name (2 letter code) [in]:
State or Province Name (full name) [ap]:
Locality Name (eg, city) [Hyderabad]:
Organization Name (eg, company) [rigaps]:
Organizational Unit Name (eg, section) [RIGAPS]:
Common Name (eg, your name or your server's hostname) [server]:
Name [RIGAPS_EasyRSA]:
Email Address [ckadarla@rigaps.com]:
Please enter the following 'extra' attributesto be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /home/ubuntu/openvpn-ca/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :                 PRINTABLE:'in'
stateOrProvinceName :         PRINTABLE:'ap'
localityName :                PRINTABLE:'Hyderabad'
organizationName :            PRINTABLE:'rigaps'
organizationalUnitName:       PRINTABLE:'RIGAPS'
commonName :                  PRINTABLE:'server'
name :                        T61STRING:'RIGAPS_EasyRSA'
emailAddress :                IA5STRING:'ckadarla@gmail.com'
Certificate is to be certified until Jun 4 10:03:24 2026 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

按ENTER鍵接受預設值。在結束之前,也不要輸入設定密碼。

我們可以生成與金鑰交換一起使用的Diffie-Hellman金鑰。

$ ./build-dh
$./build-dh
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
............................................................................................................+................++*++*
We will generate HMAC Signature to strengthen the servers TLS integrity verfication.
$ openvpn --genkey --secret keys/ta.key

生成客戶端證書和金鑰對

我們將使用以下命令生成客戶端金鑰和證書,讓我們以client1為例作為演示客戶端:

$ cd ~/openvpn-ca
$ source vars
$ ./build-key client1

配置OpenVPN服務

我們可以使用剛剛生成的憑據和檔案配置OpenVPN。

將檔案複製到OpenVPN目錄

我們需要將所有生成的檔案複製到/etc/openvpn。

$ cd ~/openvpn-ca/keys
$ sudo cp ca.crt ca.key server.crt server.key ta.key dh2048.pem /etc/openvpn

然後我們需要複製並解壓示例配置檔案到該目錄。

$ gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz | sudo tee /etc/openvpn/server.conf

我們需要修改伺服器配置檔案。

$ sudo nano /etc/openvpn/server.conf

需要取消一些指令“redirect-gateway”的註釋。

…. push “redirect-gateway def1 bypass-dhcp” …. push “dhcp-option DNS 208.67.222.222” push “dhcp-option DNS 208.67.220.220” … tls-auth ta.key 0 # 此檔案是秘密的

並新增以下行

key-direction 0
....

最後,透過刪除“;”來取消以下行的註釋。

user nobody
group nogroup

儲存並關閉檔案。

設定伺服器網路配置

我們需要調整伺服器上的一些設定,網路設定來路由OpenVPN流量。

允許伺服器上的IP轉發

我們將允許伺服器轉發傳入流量,這是VPN伺服器設定的重要步驟。

為此,我們需要修改/etc/sysctl.conf檔案。

$ sudo nano /etc/sysctl.conf

找到net.ipv4.ip_forward行,並刪除“#”以取消該行的註釋。

Net.ipv4.ip_forward=1

儲存檔案。

要在當前會話中應用更改,請使用以下命令。

$ sudo sysctl –p

啟動和啟用OpenVPN服務

我們必須在伺服器上啟動OpenVPN服務,為此,我們將使用配置檔案作為變數的例項啟動OpenVPN伺服器,配置檔案位於伺服器上的/etc/openvpn/server.conf,當我們啟動伺服器時,我們將在命令末尾新增@server。

$ sudo systemctl start openvpn@server

我們可以使用以下命令檢查服務狀態。

$ sudo systemclt status openvpn@server
$ openvpn@server.service - OpenVPN connection to server
Loaded: loaded (/lib/systemd/system/openvpn@.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2016-06-06 13:30:05 EDT; 37s ago
Docs: man:openvpn(8)
https://community.openvpn.net/openvpn/wiki/Openvpn23ManPage
https://community.openvpn.net/openvpn/wiki/HOWTO
Process: 5852 ExecStart=/usr/sbin/openvpn --daemon ovpn-%i --status /run/openvpn/%i.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/%i.conf --writepid /run/openvpn/%i.pid (code=exited, sta
Main PID: 5856 (openvpn)
Tasks: 1 (limit: 512)
CGroup: /system.slice/system-openvpn.slice/openvpn@server.service
└─5856 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/server.conf --writepid /run/openvpn/server.pid
June 06 13:30:40 openvpn2 ovpn-server[5856]: /sbin/ip addr add dev tun0 local 192.168.0.1 peer 192.168.0.2
June 06 13:30:40 openvpn2 ovpn-server[5856]: /sbin/ip route add 192.168.0.0/24 via 192.168.0.2
June 06 13:30:40 openvpn2 ovpn-server[5856]: GID set to nogroup
June 06 13:30:40 openvpn2 ovpn-server[5856]: UID set to nobody
June 06 13:30:40 openvpn2 ovpn-server[5856]: UDPv4 link local (bound): [undef]
June 06 13:30:40 openvpn2 ovpn-server[5856]: UDPv4 link remote: [undef]
June 06 13:30:40 openvpn2 ovpn-server[5856]: MULTI: multi_init called, r=256 v=256
June 06 13:30:40 openvpn2 ovpn-server[5856]: IFCONFIG POOL: base=192.168.0.4 size=62, ipv6=0
June 06 13:30:40 openvpn2 ovpn-server[5856]: IFCONFIG POOL LIST
June 06 13:30:40 openvpn2 ovpn-server[5856]: Initialization Sequence Completed

我們可以使用以下命令檢查OpenVPN tun0介面。

$ ip addr show tune0
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 100
link/none
inet 10.8.0.1 peer 10.8.0.2/32 scope global tun0
valid_lft forever preferred_lft forever

現在,我們將設定服務以在啟動時自動啟動。

$ sudo systemctl enable openvpn@server

建立客戶端配置

我們需要建立客戶端主目錄來儲存檔案。

$ mkdir –p ~/clients/files

出於安全原因,我們需要更改資料夾許可權,因為此資料夾包含客戶端機器的金鑰。

$ chmod 7000 ~/clients/files

建立客戶端基本配置

我們可以使用示例客戶端配置,為此,我們需要將檔案複製到當前位置。

$ cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client/base.conf

使用任何文字編輯器開啟配置檔案。

$ vi ~/client/base.conf

我們需要在檔案中進行一些更改,搜尋檔案中的遠端部分。

…….
Remote server_IP_Address 1194
(Here we needed to add our public IP address)
….
user nobody
group nogroup
(Here we needed to un-comment the above lines)
….
# ca ca.crt
# cert client.crt
# key client.key
(We needed to comment the above lines in the configuration file)
….
….
Key-direction 1
(We needed to add the key-direction to the file at the end)
Save the file

生成建立配置檔案的指令碼

我們將建立一個簡單的指令碼來生成證書、金鑰和加密檔案,並編譯我們在前面步驟中編輯的基本配置。使用以下程式碼建立檔案。

$ nano ~/client/generate_config.sh
KEY_DIR=~/openvpn-ca/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf
cat ${BASE_CONFIG} \
<(echo -e '<ca>') \
${KEY_DIR}/ca.crt \
<(echo -e '</ca>
<cert>') \ ${KEY_DIR}/${1}.crt \ <(echo -e '</cert>
<key>') \ ${KEY_DIR}/${1}.key \ <(echo -e '</key>
<tls-auth>') \ ${KEY_DIR}/ta.key \ <(echo -e '</tls-auth>') \ > ${OUTPUT_DIR}/${1}.ovpn

建立檔案後,我們需要使該檔案可執行。

$ chmod 7000 ~/client/generate_config.sh

生成客戶端配置檔案

我們將使用以下命令為client1生成客戶端證書和金鑰。

$ cd ~/clients
$ ./generate_config.sh client1

如果所有指令碼都執行良好,我們的~/client/files目錄中將有一個client.ovpn檔案。

我們需要使用WinSCP將客戶端檔案傳輸到客戶端機器,這裡我們使用Windows機器作為客戶端。

在Windows上安裝客戶端配置

我們需要在Windows機器上下載OpenVPN軟體,並將.ovpn檔案(即client1.ovpn檔案)複製到c:\Program Files\OpenVPN\config。

**注意** - 我們需要以管理員許可權執行OpenVPN GUI,這意味著以管理員身份執行。

OpenVPN開啟後,它將啟動連線並最小化到系統托盤,右鍵單擊OpenVPN小程式圖示,選擇client1並選擇“連線”。

我們可以使用任何客戶端作業系統(例如OSX、Android、IOS等)連線到伺服器。

我們需要生成客戶端證書和金鑰對,為每個裝置生成客戶端配置檔案。

完成此配置和設定後,我們可以安全地連線到辦公室或環境,保護身份免受窺探者和審查者的侵害。

更新於:2019年10月18日

瀏覽量:505

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.