OpenVPN 安装和配置

文/Shouren Yang 2014-12-27 03:20:00

OpenVPN是一个用于创建虚拟专用网络加密通道的软件包,最早由James Yonan编写。OpenVPN允许建立的VPN使用公开密钥、电子证书、或者用户名/密码来进行身份验证。


服务器端配置

1. 安装 OpenVPN

Ubuntu

$ sudo apt-get update
$ sudo apt-get install openvpn

CentOS

$ sudo yum intall openvpn

2. 安装 easy-rsa

$ cd ~
$ git clone https://github.com/OpenVPN/easy-rsa.git
$ cd /root/easy-rsa/easyrsa3
$ ./easyrsa init-pki

下面这个命令有交互的步骤,需要输入密码

$ ./easyrsa build-ca 
$ ./easyrsa gen-dh

下面的命令会有交互的步骤,输入密码和build-ca那个步骤对应的密码即可

$ ./easyrsa build-client-full client0 
$ ./easyrsa build-server-full server 

复制证书和秘钥到 /etc/openvpn

$ sudo cp -r ca.crt dh.pem issued/*.crt private/*.key /etc/openvpn
$ cd /etc/openvpn
$ sudo openvpn --genkey --secret ta.key

3. 创建配置文件

$ touch server.conf

服务器端的配置文件的内容如下:

mode server
tls-server

local xxx.xxx.xxx.xxx ## ip/hostname of server
port 1194 ## default openvpn port
proto udp

#bridging directive
dev tun

persist-key
persist-tun

#certificates and encryption
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret
dh dh.pem
tls-auth ta.key 0 # This file is secret

cipher BF-CBC        # Blowfish (default)
comp-lzo

#DHCP Information
ifconfig-pool-persist ipp.txt
server 172.30.1.0 255.255.255.0
push "route 10.98.20.0 255.255.255.0"
## set this to the max number of clients that should be connected at a time
max-clients 10

#log and security
user nobody
group nobody
keepalive 10 120
status openvpn-status.log
verb 3

4. 启动 OpenVPN

启动 OpenVPN 服务

$ sudo service openvpn start

5. 配置 NAT

Ubuntu 在 /etc/ufw/before.rules这个文件中添加以下内容:

# nat Table rules
*nat
:POSTROUTING ACCEPT [0:0]

# Forward traffic from eth1 through eth0.
-A POSTROUTING -s 172.30.1.0/24 -o eth0 -j MASQUERADE

# allow openvpn client to access
-A ufw-before-input -p udp --dport 1194 -j ACCEPT

# allow ssh client to access
-A ufw-before-input -p tcp --dport 22 -j ACCEPT

# don't delete the 'COMMIT' line or these nat table rules won't be processed
COMMIT

然后执行

$ sudo ufw disbale & sudo ufw enable

CentOS

$ sudo iptables -t nat -A POSTROUTING -s 172.30.1.0/24 -o eth0 -j MASQUERADE
$ sudo service iptables start

服务器上的IP转发配置

$ sudo sysctl -w net.ipv4.ip_forward=1

客户端配置

获取服务器上的 ca.crt、client0.crt、 client0.key 和 ta.key 这四个文件,并和下面的配置文件放在同一个目录下, 需要注意的是,Linux下配置文件中上述证书和密钥可能需要改称绝对路径。

配置文件如下:

##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server.     #
#                                            #
# This configuration can be used by multiple #
# clients, however each client should have   #
# its own cert and key files.                #
#                                            #
# On Windows, you might want to rename this  #
# file so it has a .ovpn extension           #
##############################################

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client

# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one.  On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap

# Are we connecting to a TCP or
# UDP server?  Use the same setting as
# on the server.
;proto tcp
proto udp

# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote xxx.xxx.xxx.xxx 1194
;remote my-server-2 1194

# Choose a random host from the remote
# list for load-balancing.  Otherwise
# try hosts in the order specified.
;remote-random

# Keep trying indefinitely to resolve the
# host name of the OpenVPN server.  Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite

# Most clients don't need to bind to
# a specific local port number.
nobind

# Downgrade privileges after initialization (non-Windows only)
;user nobody
;group nobody

# Try to preserve some state across restarts.
persist-key
persist-tun

# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here.  See the man page
# if your proxy server requires
# authentication.
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# Wireless networks often produce a lot
# of duplicate packets.  Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
ca ca.crt
cert client.crt
key client.key

# Verify server certificate by checking
# that the certicate has the nsCertType
# field set to "server".  This is an
# important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the nsCertType
# field set to "server".  The build-key-server
# script in the easy-rsa folder will do this.
;ns-cert-type server

# If a tls-auth key is used on the server
# then every client must also have the key.
tls-auth ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
cipher BF-CBC

# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
comp-lzo

# Set log file verbosity.
verb 3

# Silence repeating messages
;mute 20
知识共享许可协议
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。

最新文章 全部