红联Linux门户
Linux帮助

在Ubuntu 12.04下安装配置OpenVPN

发布时间:2015-04-11 11:10:11来源:linux网站作者:puhaitao1987

一开始,我的vps装的是Ubuntu 10.10,在这个版本上我折腾了好几天,都没有成功,后来在线升级了系统之后再安装配置openvpn,终于,能用了。如果有新的系统版本,用户刚刚登陆进去的时候就会有提示。

也就是说,直接在shell中输入“do-release-upgrade”就可以升级到下一版本。由于我是linux菜鸟,故在此做个记录。下面就开始我们的openvpn之旅吧。


1.安装openvpn

这个很容易,在线安装就可以了:apt-get  install openvpn,不作赘述了。


2.复制配置文件到/etc下: cp -R /usr/share/doc/openvpn/examples/easy-rsa  /etc/openvpn/


3.修改var文件 :vim /etc/openvpn/easy-rsa/2.0/vars。其实也可以不修改,但是为了后续步骤能够一路enter,所以就先在这里修改下吧。主要是要修改末尾的几行。

export KEY_COUNTRY=" "
export KEY_PROVINCE=" "
export KEY_CITY=" "
export KEY_ORG=" "
export KEY_EMAIL=""
export KEY_EMAIL=
export KEY_CN=
export KEY_NAME=
export KEY_OU=
export PKCS11_MODULE_PATH=
export PKCS11_PIN=

=号后边就自己填入吧,我不知道大小写有没有影响,为了以防万一,还是按照它默认的格式来写吧。编辑完之后要记得保存。


4.产生证书

#cd /etc/openvpn/easy-rsa/2.0/
#chown -R root:admin .  
#chmod g+w .
#source ./vars    //这一步如果提示没有openssl.cnf,所以把openssl-1.0.0.cnf 重命名为openssl.cnf即可.
#./build-ca  选项默认即可。
# ./build-key-server server
# ./build-key  client
#./build-dh

新产生的证书可以在/etc/openvpn/easy-rsa/2.0/keys/目录下找到。


5.进行服务器端文件配置

首先需要复制一份配置样本,然后再在这个基础上进行修改:

#cp  /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz        /etc/openvpn/
#cd  /etc/openvpn/
#gunzip server.conf.gz

这样就会解压出一个server.conf文件,打开编辑吧,如果你是从头按我的步骤来的,那么可以直接复制我的配置,其中l有些地方要根据自己的实际情况改一下。需要注意的是标红色字体的地方。

# Which local IP address should OpenVPN
# listen on? (optional)
local xx.xx.xx.xx  #这里的ip用你自己机器上网卡ip

# Which TCP/UDP port should OpenVPN listen on?
# If you want to run multiple OpenVPN instances
# on the same machine, use a different port
# number for each one.  You will need to
# open up this port on your firewall.
port 1194  #设置端口,注意要与客户端配置一致

# TCP or UDP server?
;proto tcp
proto udp    #使用的协议

# "dev tun" will create a routed IP tunnel,
# "dev tap" will create an ethernet tunnel.
# Use "dev tap0" if you are ethernet bridging
# and have precreated a tap0 virtual interface
# and bridged it with your ethernet interface.
# If you want to control access policies
# over the VPN, you must create firewall
# rules for the the TUN/TAP interface.
# On non-Windows systems, you can give

# an explicit unit number, such as tun0.
# On Windows, use "dev-node" for this.
# 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 or higher,
# you may need to selectively disable the
# Windows firewall for the TAP adapter.
# Non-Windows systems usually don't need this.
;dev-node MyTap

# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key).  Each client
# and the server must have their own cert and
# key file.  The server and all clients will
# use the same ca file.
#
# See the "easy-rsa" directory for a series
# of scripts for generating RSA certificates
# and private keys.  Remember to use
# a unique Common Name for the server
# and each of the client certificates.
#
# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).

#下边三行注意要写对路径
ca easy-rsa/2.0/keys/ca.crt
cert easy-rsa/2.0/keys/server.crt
key easy-rsa/2.0/keys/server.key

# Diffie hellman parameters.
# Generate your own with:
#  openssl dhparam -out dh1024.pem 1024
# Substitute 2048 for 1024 if you are using
# 2048 bit keys.
dh easy-rsa/2.0/keys/dh1024.pem  #注意路径

# Configure server mode and supply a VPN subnet
# for OpenVPN to draw client addresses from.
# The server will take 10.8.0.1 for itself,
# the rest will be made available to clients.
# Each client will be able to reach the server
# on 10.8.0.1. Comment this line out if you are
# ethernet bridging. See the man page for more info.
server 10.9.0.0 255.255.255.0  #虚拟网卡上的ip地址段

# Maintain a record of client <-> virtual IP address
# associations in this file.  If OpenVPN goes down or
# is restarted, reconnecting clients can be assigned
# the same virtual IP address from the pool that was
# previously assigned.
ifconfig-pool-persist ipp.txt

# 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"

# Certain Windows-specific network settings
# can be pushed to clients, such as DNS
# or WINS server addresses.  CAVEAT:
# http://openvpn.net/faq.html#dhcpcaveats
# The addresses below refer to the public
# DNS servers provided by opendns.com.
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"

# Uncomment this directive to allow different
# clients to be able to "see" each other.
# By default, clients will only see the server.
# To force clients to only see the server, you
# will also need to appropriately firewall the
# server's TUN/TAP interface.
client-to-client

# Uncomment this directive if multiple clients
# might connect with the same certificate/key
# files or common names.  This is recommended
# only for testing purposes.  For production use,
# each client should have its own certificate/key
# pair.
#
# IF YOU HAVE NOT GENERATED INDIVIDUAL
# CERTIFICATE/KEY PAIRS FOR EACH CLIENT,
# EACH HAVING ITS OWN UNIQUE "COMMON NAME",
# UNCOMMENT THIS LINE OUT.
duplicate-cn  #允许一套配置同时多人使用

# The keepalive directive causes ping-like
# messages to be sent back and forth over
# the link so that each side knows when
# the other side has gone down.
# Ping every 10 seconds, assume that remote
# peer is down if no ping received during
# a 120 second time period.
keepalive 10 120

# Enable compression on the VPN link.
# If you enable it here, you must also
# enable it in the client config file.
comp-lzo

# The maximum number of concurrently connected

# clients we want to allow.
;max-clients 100

# It's a good idea to reduce the OpenVPN
# daemon's privileges after initialization.
#
# You can uncomment this out on
# non-Windows systems.
user nobody
group nogroup

# The persist options will try to avoid
# accessing certain resources on restart
# that may no longer be accessible because
# of the privilege downgrade.
persist-key
persist-tun

# Output a short status file showing
# current connections, truncated
# and rewritten every minute.
status openvpn-status.log

# Set the appropriate level of log
# file verbosity.
#
# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 3

# Silence repeating messages.  At most 20
# sequential messages of the same message
# category will be output to the log.

;mute 20


6.配置防火墙数据包转发

#sysctl -wnet.ipv4.ip_forward=1
# iptables -t nat -A POSTROUTING -s 10.9.0.0/24 -o eth0 -j MASQUERADE


7.运行openvpn服务端 # openvpn /etc/openvpn/server.conf


8.配置客户端

a.  复制生成的ca.crt,client.key,client.crt到本地

b. 编辑client.ovpn文件

# 定义是一个客户端
client
# 定义使用路由IP模式,与服务端一致
;dev tap
dev tun
# 定义Windows下使用的网卡名称,linux不需要
;dev-node
# 定义使用的协议,与服务端一致
;proto tcp
proto udp

# 指定服务端地址和端口,可以用多行指定多台服务器
# 实现负载均衡(从上往下尝试)
remote xx.xx.xx.xx 1194  #输入服务器的ip以及端口
# 若上面配置了多台服务器,让客户端随机连接
;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 do not need to bind to
# a specific local port number.
nobind
# 也是为了让Openvpn也nobody运行(安全)
# 注意:Windows不能设置
;user nobody
;group nobody

# Try to preserve some state across restarts.
persist-key
persist-tun
# 若客户端通过HTTP Proxy,在这里设置
# 要使用Proxy,不能使用UDP为VPN的通讯协议
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# 无线网络有很多多余的头文件,设置忽略它
;mute-replay-warnings
# 重点,就是指定ca和客户端的证书
# 注意,下面的两个号是连在一起的,之间没有空格
ca ca.crt
cert client01.crt
key client01.key
# 如果服务端打开了PAM认证模块,客户端需要另其有效
;auth-user-pass
;tls-auth ta.key 1

# 一些安全措施
# 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.
# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
;cipher x
# 使用lzo压缩,与服务端一致
comp-lzo
# Set log file verbosity.
verb 3
# Silence repeating messages
mute 20

c. 把ca.crt、client.crt、client.key和client.ovpn文件复制到openvpn-gui安装目录下的config文件夹中。


9.启动openvpn客户端