簡介
OpenVPN 是一個(gè)功能齊全的 SSL VPN,它使用行業(yè)標(biāo)準(zhǔn) SSL/TLS 協(xié)議實(shí)現(xiàn) OSI 第 2 層或第 3 層安全網(wǎng)絡(luò)擴(kuò)展,支持基于證書、智能卡和/或用戶名/密碼憑據(jù)的靈活客戶端身份驗(yàn)證方法,并允許用戶或使用應(yīng)用于 VPN 虛擬接口的防火墻規(guī)則的特定于組的訪問控制策略。
應(yīng)用場景
在家庭網(wǎng)絡(luò)中有動(dòng)態(tài)公網(wǎng)IP的前提下,當(dāng)我們?cè)谕獠浚胍L問家庭內(nèi)部網(wǎng)絡(luò)。一般可以通過路由器端口映射的方式進(jìn)行,通過動(dòng)態(tài)公網(wǎng)IP和映射的指定端口,來訪問家庭內(nèi)部網(wǎng)絡(luò)環(huán)境中的一些網(wǎng)絡(luò)服務(wù)。在把家庭網(wǎng)絡(luò)環(huán)境中的服務(wù)直接暴露在公網(wǎng)環(huán)境下,可能會(huì)對(duì)家庭內(nèi)部網(wǎng)絡(luò)造成安全隱患。
內(nèi)部系統(tǒng)可能會(huì)存在一些漏洞未能及時(shí)修復(fù),造成網(wǎng)絡(luò)安全問題。這些漏洞為黑客們提供了各種機(jī)會(huì),絕大多數(shù)非法入侵、木馬、病毒都是通過漏洞來突破網(wǎng)絡(luò)安全防線的。
因此我們?cè)诳紤]網(wǎng)絡(luò)安全因素下,家庭網(wǎng)絡(luò)環(huán)境對(duì)外訪問只通過 VPN 服務(wù)進(jìn)行訪問,對(duì)外暴露服務(wù)只暴露VPN服務(wù)即可。從而保證有效提高了我們家庭內(nèi)部網(wǎng)絡(luò)環(huán)境的安全性。
硬件環(huán)境
樹莓派3B+
|
CPU主頻 |
1.4 GHz |
|
核心數(shù)量 |
4 核 |
|
線程數(shù)量 |
4 線程 |
|
核心架構(gòu) |
BCM |
樹莓派3B+所用的BCM2835是基于ARMv7 32位架構(gòu)的4核心ARMv7 Processor rev 4(v7l)處理器
雖然樹莓派3B+的硬件支持64位的系統(tǒng),但是官方的系統(tǒng)還是32位的,主要應(yīng)該是為了兼容之前的硬件
安裝openvpn和easy-rsa
sudo apt-get install -y easy-rsa
sudo apt-get install -y openvpn
- easy-rsa包 提供證書生成腳本、模板配置文件等創(chuàng)建相關(guān)目錄
- openvpn就是我們今天要安裝的包了
升級(jí)easy-rsa為v3.0.8版本
樹莓派中的easy-rsa源安裝的版本為v2.x.x,要把版本升級(jí)到v3.0.8。由于樹莓派系統(tǒng)是基于Debian創(chuàng)建“新的”分支系統(tǒng),我們可以使用Debian系統(tǒng)版本中的deb安裝包,來安裝easy-rsa v3.0.8
使用https://pkgs.org下載deb安裝包,樹莓派是ARM架構(gòu)
點(diǎn)擊easy-rsa_3.0.8-1_all.deb打開下載頁面
復(fù)制下載地址,使用wget下載文件
wget http://ftp.de.debian.org/debian/pool/main/e/easy-rsa/easy-rsa_3.0.8-1_all.deb
安裝 easy-rsa_3.0.8-1_all.deb文件
dpkg -i easy-rsa_3.0.8-1_all.deb
配置證書密鑰
復(fù)制easy-rsa配置信息
# sudo cp -rf /usr/share/easy-rsa/3.0.8 /etc/openvpn/server/easy-rsa
sudo cp -rf /usr/share/easy-rsa /etc/openvpn/server/easy-rsa
cd /etc/openvpn/server/easy-rsa
編輯vars文件
vim vars
# 國家
set_var EASYRSA_REQ_COUNTRY "CN"
# 地區(qū)
set_var EASYRSA_REQ_PROVINCE "Shandong"
# 城市
set_var EASYRSA_REQ_CITY "Jinan"
# 組織
set_var EASYRSA_REQ_ORG "Nilorg CA"
# 郵箱
set_var EASYRSA_REQ_EMAIL "[email protected]"
# 擁有者
set_var EASYRSA_REQ_OU "Home"
使變量生效
source ./vars
生成 CA 根證書
cd /etc/openvpn/server/easy-rsa
sudo ./easyrsa init-pki
sudo ./easyrsa build-ca nopass
init-pki:初始化,創(chuàng)建pki文件夾,用來存放即將生成的證書
生成 OpenVPN 服務(wù)器/客戶端證書和密鑰
# 生成服務(wù)端證書, nopass 參數(shù)設(shè)定證書無密碼
sudo ./easyrsa build-server-full server nopass
# 生成客戶端證書, nopass 參數(shù)設(shè)定證書無密碼
sudo ./easyrsa build-client-full client1 nopass
# 創(chuàng)建Diffie-Hellman
sudo ./easyrsa gen-dh
# 生成ta.key
openvpn --genkey --secret ta.key
- build-server-full : 生成服務(wù)端/客戶端證書
- Diffie-Hellman,迪菲·赫爾曼密鑰交換 是一種安全協(xié)議。它可以讓雙方在完全沒有對(duì)方任何預(yù)先信息的條件下通過不安全信道創(chuàng)建起一個(gè)密鑰。這個(gè)密鑰可以在后續(xù)的通訊中作為對(duì)稱密鑰來加密通訊內(nèi)容。
- ta.key用于防御DoS、UDP淹沒等惡意攻擊。
配置 OpenVPN 服務(wù)端
復(fù)制配置文件
# server.conf 如果server.conf不存在,需要對(duì)目錄下的
cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf /etc/openvpn/
復(fù)制證書及密鑰文件
然后將CA的證書, VPN Server的證書和密鑰, 以及Diffie-Hellman參數(shù)文件復(fù)制到 /etc/openvpn/server 目錄下
cp /etc/openvpn/server/easy-rsa/pki/ca.crt /etc/openvpn/server
cp /etc/openvpn/server/easy-rsa/pki/issued/server.crt /etc/openvpn/server
cp /etc/openvpn/server/easy-rsa/pki/private/server.key /etc/openvpn/server
cp /etc/openvpn/server/easy-rsa/pki/dh.pem /etc/openvpn/server/
編輯配置文件
編輯server.conf配置,去除無用配置項(xiàng)
local 0.0.0.0
port 1194
proto tcp
# dev tap
dev tun
ca /etc/openvpn/server/easy-rsa/pki/ca.crt
cert /etc/openvpn/server/easy-rsa/pki/issued/server.crt
key /etc/openvpn/server/easy-rsa/pki/private/server.key
dh /etc/openvpn/server/easy-rsa/dh.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist /etc/openvpn/server/ipp.txt
push "route 192.168.0.0 255.255.255.0"
# If enabled, this directive will configure
# all clients to redirect their default
#.NETwork gateway through the VPN, causing
# all IP traffic such as web browsing and
# and DNS lookups to go through the VPN
# (The OpenVPN server machine may need to NAT
# or bridge the TUN/TAP interface to the internet
# in order for this to work properly).
# push "redirect-gateway def1 bypass-dhcp"
# push "dhcp-option DNS 208.67.222.222"
# push "dhcp-option DNS 208.67.220.220"
client-to-client
keepalive 10 120
# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
# openvpn --genkey --secret ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
tls-auth /etc/openvpn/server/ta.key 0 # This file is secret
# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
# Note that 2.4 client/server will automatically
# negotiate AES-256-GCM in TLS mode.
# See also the ncp-cipher option in the manpage
cipher AES-256-CBC
comp-lzo
max-clients 100
persist-key
persist-tun
status /var/log/openvpn/status.log
log /var/log/openvpn/openvpn.log
log-Append /var/log/openvpn/openvpn.log
verb 3
mute 20
配置iptables
配置iptables設(shè)置nat規(guī)則和打開路由轉(zhuǎn)發(fā)
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
iptables -vnL -t nat
# 打開路由轉(zhuǎn)發(fā)
vim /etc/sysctl.conf
# 修改net.ipv4.ip_forward
net.ipv4.ip_forward = 1
sysctl -p
驗(yàn)證服務(wù)
systemctl status [email protected]
systemctl restart [email protected]
配置OpenVPN客戶端
復(fù)制配置文件
# 創(chuàng)建客戶端配置文件
mkdir -p ~/openvpn/client1
# client.conf 如果client.conf不存在,需要對(duì)目錄下的
cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/openvpn/client1/client1.ovpn
復(fù)制證書及密鑰文件
然后將CA的證書、客戶端的證書和密鑰,以及Diffie-Hellman參數(shù)文件復(fù)制到 ~/openvpn/client1 目錄下
cp /etc/openvpn/server/easy-rsa/pki/ca.crt ~/openvpn/client1/ca.crt
cp /etc/openvpn/server/easy-rsa/ta.key ~/openvpn/client1/ta.key
cp /etc/openvpn/server/easy-rsa/pki/private/client1.key ~/openvpn/client1/client1.key
cp /etc/openvpn/server/easy-rsa/pki/issued/client1.crt ~/openvpn/client1/client1.crt
編輯配置文件
編輯client1.ovpn配置,去除無用配置項(xiàng)
client
dev tun
proto tcp
# remote <服務(wù)器端IP> <服務(wù)器端端口>
remote 192.168.xx.xx 1194
resolv-retry infinite
# Most clients don't need to bind to
# a specific local port number.
nobind
persist-key
persist-tun
ca ca.crt
cert client1.crt
key client1.key
remote-cert-tls server
tls-auth ta.key 1
comp-lzo
verb 5
# 不拉取服務(wù)端路由配置,可選可不選。
route-nopull
# 指定訪問哪些網(wǎng)段走vpn隧道
route 192.168.0.0 255.255.0.0 vpn_gateway
打包客戶端配置文件
cd ~/openvpn
tar -zcvf client1.tar.gz ./client1
使用OpenVPN Connect客戶端
使用OpenVPN Connect客戶端連接服務(wù)






