使用openVPN建立虚拟专用网络,其中Linux作为服务端,windows作为客户端

来源:互联网 发布:mac 编译php7 编辑:程序博客网 时间:2024/05/16 12:35

2014.09.17更新

在虚拟机中也可以如下面的操作部署openvpn,出现错误与解决如下:

下载 openvpn :sudo apt-get install openvpn
在下载openvpn的时候会提示 :建议安装软件包 :resolvconf
安装完 openvpn 后再 安装resolvconf
(若没有安装 resolvconf  在启动 openvpn的时候提示 /usr/sbin/openssl-vulnkey: not found)


把 client的密钥匙cp 到 /etc/openvpn
运行 :openvpn --config /etc/openvpn/client.conf
提示 :
hzh@hzh:~/桌面$ openvpn --config /etc/openvpn/client.conf 
Tue May 27 20:16:49 2008 OpenVPN 2.1_rc7 i486-pc-linux-gnu [SSL] [LZO2] [EPOLL] built on May 14 2008
Tue May 27 20:16:49 2008 WARNING: No server certificate verification method has been enabled.  See http://openvpn.net/howto.html#mitm for more info.
Tue May 27 20:16:49 2008 Cannot load certificate file client.crt: error:02001002:system library:fopen:No such file or directory: error:20074002:BIO routines:FILE_CTRL:system lib: error:140AD002:SSL routines:SSL_CTX_use_certificate_file:system lib
Tue May 27 20:16:49 2008 Exiting

没权限
以root运行
提示 :
root@hzh:/etc/openvpn# openvpn --config client.conf 
Tue May 27 20:10:38 2008 OpenVPN 2.1_rc7 i486-pc-linux-gnu [SSL] [LZO2] [EPOLL] built on May 14 2008
Tue May 27 20:10:38 2008 WARNING: No server certificate verification method has been enabled.  See http://openvpn.net/howto.html#mitm for more info.
Tue May 27 20:10:38 2008 /usr/sbin/openssl-vulnkey -q client.key
Tue May 27 20:10:38 2008 Control Channel Authentication: using 'ta.key' as a OpenVPN static key file
Tue May 27 20:10:38 2008 Outgoing Control Channel Authentication: Using 160 bit message hash 'SHA1' for HMAC authentication
Tue May 27 20:10:38 2008 Incoming Control Channel Authentication: Using 160 bit message hash 'SHA1' for HMAC authentication
Tue May 27 20:10:38 2008 Control Channel MTU parms [ L:1543 D:168 EF:68 EB:0 ET:0 EL:0 ]
Tue May 27 20:10:38 2008 Data Channel MTU parms [ L:1543 D:1450 EF:43 EB:4 ET:0 EL:0 ]
Tue May 27 20:10:38 2008 Local Options hash (VER=V4): 'd8421bb0'
Tue May 27 20:10:38 2008 Expected Remote Options hash (VER=V4): 'c413e92e'
Tue May 27 20:10:38 2008 failed to find GID for group nobody
Tue May 27 20:10:38 2008 Exiting

没有添加 nobody

添加 nobody
root@hzh:/etc/openvpn# groupadd nobody

再运行
root@hzh:/etc/openvpn# openvpn --config client.conf 

.......
......
......
Tue May 27 20:10:55 2008 GID set to nobody
Tue May 27 20:10:55 2008 UID set to nobody
Tue May 27 20:10:55 2008 Initialization Sequence Completed


大功告成 
启动方式 sudo /etc/init.d/openvpn start

--------------------------------------------------------------------------------------------------------------------------------------------------------

由于安装有hadoop2.2.0的Linux服务器跟本机不在一个网段,因此我们使用openvpn建立一个vpn网络,这样使得在windows 7 下也可以使用Eclipse编写hadoop程序。

主要的步骤参考以下链接:

http://www.xiaohui.com/dev/server/20070514-install-openvpn.htm

http://blog.sina.com.cn/s/blog_4ada12a50100gix8.html

现转载如下:

注意:在设置openvpn之前要先开端口,具体方法是:

打开路由器界面——选择转发规则——虚拟服务器——添加新条目,在之后打开的界面里:


端口号填1194(openvpn)的默认端口号,ip地址填写server的地址

注意:openvpn安装完成以后,启动openvpn的命令:openvpn --config /etc/openvpn/server.conf(client.conf) --daemon(daemon命令作用是使服务后台运行)

该命令必须以root账户执行。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

一. OpenVPN 安装环境

    Server 端的环境
  1. redhat, kernel版本: 2.4.20-31.9, IP 为 70.8.7.6
  2. kernel 需要支持 tun 设备, 需要加载 iptables 模块. 
    检查 tun 是否安装: 
    代码:
    root@a [/]# modinfo tun filename:    /lib/modules/2.4.20-31.9/kernel/drivers/net/tun.o description:  author:       license:     "GPL" 
    如果没有 modinfo 命令, 直接找一下, 看看 kernel 里是否有 tun.o 文件: 
    代码:
    find -name tun.o ./lib/modules/2.4.20/kernel/drivers/net/tun.o 
    检查iptables 模块, 查看是否有下列文件: 
    /etc/init.d/iptables
  3. OpenSSL。如果需要启用 SSL 连接,则需要先安装 OpenSSL。安装 OpenSSL 的方法在这里不做介绍,具体可以用 Google 搜索。CentOS 下可以用 yum install:
    yum install opensslyum install openssl-devel
  4. 安装的 OpenVPN 的版本: 2.0.5. 现在似乎已经有一个更新的版本了. 可在http://openvpn.net 上下载.

    Client 端的环境:
  1. Windows XP PRO SP2
  2. OpenVPN GUI For windows 1.0.3 , 可在 openvpn.se 下载 
    注意: OpenVPN GUI for windows 的版本要和 OpenVPN Server 的版本配套. 
    例如, 服务器装的是 OpenVPN 2.0.5, 那么下载的 OpenVPN GUI fow windows 应该是: openvpn-2.0.5-gui-1.0.3-install.exe 
    OpenVPN GUI的所有历史版本: http://openvpn.se/files/install_packages/

二. OpenVPN 服务端安装过程

http://www.xiaohui.com/dev/server/20070514-install-openvpn.htm
  1. 用 SecureCRT 登录到 host, 进入根目录 代码:
    cd / 
  2. 下载 LZO,解压到lzo-2.02.

    地址: http://www.oberhumer.com/opensource/lzo/download/ 代码:

    wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.02.tar.gz
  3. 下载 OpenVPN, 解压到openvpn-2.0.5

    地址: http://openvpn.net/download.html 代码:

    wget http://openvpn.net/release/openvpn-2.0.5.tar.gz
  4. 安装 LZO 代码:
    cd /lzo-2.02 ./configure make make check make install 
  5. 安装 OpenVPN

    代码:

    cd /openvpn-2.0.5./configure # 或用指定dir: (注:下述命令, 应该在一行写完. 为了方便显示, 这里分成了四行)# ./configure --with-lzo-headers=/usr/local/include #  --with-lzo-lib=/usr/local/lib #  --with-ssl-headers=/usr/local/include/openssl #  --with-ssl-lib=/usr/local/lib make make install 
  6. 生成证书Key

    初始化 PKI

    (如果没有 export 命令也可以用 setenv [name] [value] 命令)

    代码:

    cd /openvpn-2.0.5/easy-rsa export D=`pwd` export KEY_CONFIG=$D/openssl.cnf export KEY_DIR=$D/keys export KEY_SIZE=1024 export KEY_COUNTRY=CN export KEY_PROVINCE=GD export KEY_CITY=SZ export KEY_ORG="xiaohui.com" export KEY_EMAIL="your-email [at] xiaohui.com" 
    Build:

    代码:

    ./clean-all ./build-ca Generating a 1024 bit RSA private key ................++++++ ........++++++ writing new private key to 'ca.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [CN]: State or Province Name (full name) [GD]: Locality Name (eg, city) [SZ]: Organization Name (eg, company) [xiaohui.com]: Organizational Unit Name (eg, section) []:xiaohui.com Common Name (eg, your name or your server's hostname) []:server Email Address [your-email [at] xiaohui.com]: 
    # 建立 server key 代码: 代码:
    ./build-key-server server Generating a 1024 bit RSA private key ......++++++ ....................++++++ writing new private key to 'server.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [CN]: State or Province Name (full name) [GD]: Locality Name (eg, city) [SZ]: Organization Name (eg, company) [xiaohui.com]: Organizational Unit Name (eg, section) []:xiaohui.com Common Name (eg, your name or your server's hostname) []:server Email Address [your-email [at] xiaohui.com]: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:abcd1234 An optional company name []:xiaohui.com Using configuration from /openvpn-2.0.5/easy-rsa/openssl.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows countryName           :PRINTABLE:'CN' stateOrProvinceName   :PRINTABLE:'GD' localityName          :PRINTABLE:'SZ' organizationName      :PRINTABLE:'xiaohui.com' organizationalUnitName:PRINTABLE:'xiaohui.com' commonName            :PRINTABLE:'server' emailAddress          :IA5STRING:'your-email [at] xiaohui.com' Certificate is to be certified until Mar 19 08:15:31 2016 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 

    #生成客户端 key

    代码:

    ./build-key client1 Generating a 1024 bit RSA private key .....++++++ ......++++++ writing new private key to 'client1.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [CN]: State or Province Name (full name) [GD]: Locality Name (eg, city) [SZ]: Organization Name (eg, company) [xiaohui.com]: Organizational Unit Name (eg, section) []:xiaohui.com Common Name (eg, your name or your server's hostname) []:client1    #重要: 每个不同的 client 生成的证书, 名字必须不同. Email Address [your-email [at] xiaohui.com]: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:abcd1234 An optional company name []:xiaohui.com Using configuration from /openvpn-2.0.5/easy-rsa/openssl.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows countryName           :PRINTABLE:'CN' stateOrProvinceName   :PRINTABLE:'GD' localityName          :PRINTABLE:'SZ' organizationName      :PRINTABLE:'xiaohui.com' organizationalUnitName:PRINTABLE:'xiaohui.com' commonName            :PRINTABLE:'client1' emailAddress          :IA5STRING:'your-email [at] xiaohui.com' Certificate is to be certified until Mar 19 08:22:00 2016 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 

    依次类推生成其他客户端证书/key

    代码:

    ./build-key client2 ./build-key client3 
    注意在进入 Common Name (eg, your name or your server's hostname) []: 的输入时, 每个证书输入的名字必须不同.
  7. 生成 Diffie Hellman 参数 。代码:
    ./build-dh 
  8. 将 keys 下的所有文件打包下载到本地

    代码:

    tar -cf mykeys.tar /openvpn-2.0.5/easy-rsa/keys cp mykeys.tar /home/xiaohui.comsys/public_html/mykeys.tar 
    将 mykeys.tar 移到 web public(绝对路径因人而异) 上, 然后用 http://www.a.com/mykeys.tar 方式将其下载到本地保存, 然后将其从server删除: 代码:
    rm /home/xiaohui.comsys/public_html/mykeys.tar 
    也可以用其他方法把 key file搞到本地,例如 ftp.
  9. 创建服务端配置文件

    从样例文件创建:

    代码:

    cd $dir/sample-config-files/ # 进入源代码解压目录下的sample-config-files子目录 cp server.conf /usr/local/etc  # cp服务器配置文件到/usr/local/etc vi /usr/local/etc/server.conf 
    我建立的server.conf 的内容稍后另附.
  10. 创建客户端配置文件

    代码:

    cd $dir/sample-config-files/  #进入源代码解压目录下的sample-config-files子目录 cp client.conf /usr/local/etc  #cp客户端配置文件到/usr/local/etc vi /usr/local/etc/client.conf 
    我建立的client.conf 的内容稍后另附.
  11. 启动Openvpn: openvpn [server config file] 代码:
    /usr/local/sbin/openvpn --config /usr/local/etc/server.conf 

三. OpenVPN GUI For Windows 客户端安装过程

  1. 安装 OpenVPN GUI For Windows, 到 http://openvpn.se 下载. 目前的版本是 1.0.3. 注意: OpenVPN GUI 的版本要和 OpenVPN Server 的版本配套. 详见第一节一. 安装环境中的说明.
  2. 依屏幕指示安装openvpn gui.
  3. 配置 openvpn gui

    安装结束后, 进入安装文件夹下的 config 目录, 然后将上面第 10 步建立的 client.conf 文件从 server 上下载到此文件夹, 并更名为 client.ovpn

    同时, 将第8 步打包的 mykeys.tar 中的下列证书文件解压到此文件夹:

    代码:

    ca.crt client1.crt client1.csr client1.key 
    然后双击 client.ovpn 即可启动 openvpn, 或者通过 OpenVPN GUI 的控制启动 VPN.

    如果双击 client.ovpn 没有反应, 则在任务栏点 OpenVPN GUI 的小图标右键, 选择 edit config, 将内容复制过去再保存. 然后再点右键中的 connect即可.

    如果需要第二台机器上使用 vpn , 进行同样的配置, 只需要将 client1.crt, client1.csr, client1.key 换成对应的 client2.xxx 即可, 然后将 client.ovpn 中的对应key文件值改掉.

四. OpenVPN 配置样例文件

  1. OpenVPN 服务端:server.conf

    代码:

    local 70.8.7.6 port 1194 proto udp dev tun ca /openvpn-2.0.5/easy-rsa/keys/ca.crt cert /openvpn-2.0.5/easy-rsa/keys/server.crt key /openvpn-2.0.5/easy-rsa/keys/server.key  # This file should be kept secret dh /openvpn-2.0.5/easy-rsa/keys/dh1024.pem server 10.8.0.0 255.255.255.0 client-to-client keepalive 10 120 comp-lzo persist-key persist-tun status /openvpn-2.0.5/easy-rsa/keys/openvpn-status.log verb 4 push "dhcp-option DNS 10.8.0.1" push "dhcp-option DNS 70.88.98.10"  # name server 地址, 如何获取见随后说明 push "dhcp-option DNS 70.88.99.11"  # name server 地址, 如何获取见随后说明 
    说明: 有些 domain 被 GFW 封掉了, 这时, 如果要访问这些网站, 应该将 server 上的 DNS push 到 client. 上面示例中的 dns ip: 70.88.98.10, 70.88.99.10, 可以在 /etc/resolv.conf 中找到: 代码:
    vi /etc/resolv.conf nameserver   70.88.98.10 nameserver   70.88.99.11 
  2. OpenVPN 客户端: client.ovpn

    代码:

    client dev tun proto udp remote 70.8.7.6 1194 persist-key persist-tun ca ca.crt cert client1.crt key client1.key ns-cert-type server comp-lzo verb 3 redirect-gateway def1 

五. OpenVPN 访问外网的设置

  1. 打开路由 VPN连接成功后, 还需要设置路由, 才能透过VPN访问Internet. 在 linux host 上添加路由: 代码:
    iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j SNAT --to-source 70.8.7.6 /etc/init.d/iptables save /etc/init.d/iptables restart 
    不同的机器,-o eth0 参数可能不一样,具体可输入 ifconfig 查看,搞清 ip(70.8.7.6)所在的网卡号.

    同时, 需要将 ip forward 打开. 不要用 echo 1 > /proc/sys/net/ipv4/ip_forward 的方式, 这种方式重启后无效. 先查看一下:

    代码:

    sysctl -a | grep for #查看结果: net.ipv4.conf.tun0.mc_forwarding = 0 net.ipv4.conf.tun0.forwarding = 1 net.ipv4.conf.eth0.mc_forwarding = 0 net.ipv4.conf.eth0.forwarding = 1 net.ipv4.conf.lo.mc_forwarding = 0 net.ipv4.conf.lo.forwarding = 1 net.ipv4.conf.default.mc_forwarding = 0 net.ipv4.conf.default.forwarding = 1 net.ipv4.conf.all.mc_forwarding = 0 net.ipv4.conf.all.forwarding = 1 net.ipv4.ip_forward = 1 
    如果你的主机上列数值不是为1, 则要将其改成1, 例如:

    代码:

    sysctl -w net.ipv4.ip_forward=1 
    依此类推.
  2. 开启域名服务器

    如果你需要访问一些已经被GFW封掉了域名的网站, 但你的 OpenVPN 服务器没有被封的话,那么你需要在你的主机上开启 name server, 并将 dns push 给 client。 一般的独立主机, 都带有 private dns server.

    代码:

    rpm -qa | grep bind /etc/init.d/named start 
    另外, 必须保证 server.conf 配置中, 有这三个配置:

    代码:

    push "dhcp-option DNS 10.8.0.1" push "dhcp-option DNS 70.88.98.10"  # name server 地址 push "dhcp-option DNS 70.88.99.11"  # name server 地址 
    当 client 连接成功后, 在 cmd 下执行 ipconfig /all, 应该有这类似这样的输出:

    代码:

    Ethernet adapter Local Area Connection 3:         Connection-specific DNS Suffix  . :         Description . . . . . . . . . . . : TAP-Win32 Adapter V8         Physical Address. . . . . . . . . : 00-FF-AA-B0-60-2B         Dhcp Enabled. . . . . . . . . . . : Yes         Autoconfiguration Enabled . . . . : Yes         IP Address. . . . . . . . . . . . : 10.8.0.6         Subnet Mask . . . . . . . . . . . : 255.255.255.252         Default Gateway . . . . . . . . . : 10.8.0.5         DHCP Server . . . . . . . . . . . : 10.8.0.5         DNS Servers . . . . . . . . . . . : 10.8.0.1                                             70.88.98.10                                             70.88.99.11         Lease Obtained. . . . . . . . . . : 2006年5月25日 5:13:52         Lease Expires . . . . . . . . . . : 2007年5月25日 5:13:52 

六. 设置 OpenVPN 服务器 reboot后自动启动 openvpn

执行命令:

代码:

vi /etc/rc.local 
然后在最后面加入此行:

代码:

/usr/local/sbin/openvpn --config /usr/local/etc/server.conf > /dev/null 2>&1 & 

七. OpenVPN 测试

你可以用 VPN 登录上去之后, 测试 MSN, QQ, IE 等网络应用, 也可以尝试访问一些被 GFW 禁掉的网站, 当然, 前提是你的 VPN 服务器不在境内.

八. 使用 OpenVPN 的强烈注意事项

不建议用 VPN 登录 paypal 帐户和 google adsense 帐户. 否则有可能导致帐户受限或带来其他风险.

十. 一些补充

2011.01.11 补充:今天用 yum -y update 升级了 CentOS 之后,发现 OpenVPN 连接不上去了,老是说用户检验出错。经检查 server log,发现有以下日志:

Thu Feb 10 11:13:07 2011 us=3362 222.244.***.**:45771 TLS: Initial packet from 222.244.***.**:45771, sid=eec450eb 8673ceef

Thu Feb 10 11:13:10 2011 us=798063 222.244.***.**:45771 openvpn_execve: external program may not be called unless '--script-secur ity 2' or higher is enabled. Use '--script-security 3 system' for backward compatibility with 2.1_rc8 and earlier. See --help t ext or man page for detailed info.

Thu Feb 10 11:13:10 2011 us=798127 222.244.***.**:45771 TLS Auth Error: user-pass-verify script failed to execute: /usr/bin/php - q /home/xiaohui/openvpn-manager/admin/openvpn-auth.php

Thu Feb 10 11:13:10 2011 us=798141 222.244.***.**:45771 TLS Auth Error: Auth Username/Password verification failed for peer

经查,原来是 CentOS 在进行 yum update 时,将 OpenVPN 也由2.0 升级到了 2.1。而 OpenVPN 2.1 最大的改变之一,就是加了一个script-security参数。如果按我的这篇教程进行安装,但装的是 OpenVPN 2.1 及以上版本的话,记得在 server.conf 中再加上这么一行配置即可:

script-security 3
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

by:http://blog.vfocus.net

一、在网上下载openvpn的安装包http://openvpn.net/上 下载openvpn的最新版,时至今 日是openvpn-2.0.9.tar.gz。

    网上都说还需要lzo这个压缩库。这个包是压缩的,我没有安装,在编译程序的时候只需要加上--disable-lzo就可以了。然后再配置文件里面把这 项注释掉就可以了,这样所有要安装的就是一个包openvpn-2.0.9.tar.gz。

二、vpn服务端的网络结构

    vpn服务端是有两个ip 其中eth0 172.16.6.79 提供vpn服务,由外部通过172.31.16.0/24 通过1194端口拨入,然后通过 eth1 192.168.253.79访问192.168.253.0/24去维护服务器

三、安装openvpn

tar zxvf openvpn-2.0.9.tar.gz
cd openvpn-2.0.9
./configure –prefix=/usr/local/openvpn –-disable-lzo
make;make install

四、生成openvpn证书

    1、cd openvpn-2.0.9/ easy-rsa/

    vi vars

    根据自己的情况设置

根据自己的情况设置KEY_COUNTRY, KEY_PROVINCE, KEY_CITY, KEY_ORG, KEY_EMAIL,比如我的
export KEY_COUNTRY=CN
export KEY_PROVINCE=Beijing
export KEY_CITY=Beijing
export KEY_ORG=abc
export KEY_EMAIL=abc@123.com

然后保存
. ./vars //注意两个点中间有空格
./clean-all
./build-ca
build-ca 命令使用openssl 命令生成certificate authority (CA) certificate 和密钥:
./build-ca
Generating a 1024 bit RSA private key
....++++++
.......++++++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [Beijing]:
Locality Name (eg, city) [Beijing]:
Organization Name (eg, company) [BUAA]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:zhang //这里随便输入即可

    Email Address [zhangwenhao@wanmei.com]:

    这些步骤一直按回车就可以了

    2、为vpn server生成证书与密匙

./build-key-server server
Common Name 处填server,其他默认,为上面编辑vars 文件时设置的值。"Sign the certificate?
[y/n]" 和"1 out of 1 certificate requests certified, commit? [y/n]"两处选y。

    3、为vpn clients 生成证书与密匙

./build-key client1
./build-key client2
./build-key client3
Common Name 处分别填client1 client2 client3,别的同vpn server 设置

    4、生成diffie hellman 参数

./build-dh

现在生成了所需的文件。目录keys 下生成的文件中,ca.crt 是所有vpn 机器都需要的。vpn server 需要ca.crt ca.key dh1024.pem server.crt server.key,拷贝到vpn server 的/etc/openvpn目录。vpn client1 需要ca.crt ca.key lient1.crt client1.key,拷贝到vpn client1 的/etc/openvpn 目录。client2,client3 和client1 同,只是client1.crt 和client1.key 分别用client2 和client3 的对应文件。vpn client2 和vpn client3 也把对应的密钥文件拷贝到各自的/etc/openvpn 目录。

五、配置server端与client端

    1、server.conf 使用代码包目录sample-config-files 里的server.conf 修改即可。配置文件如下()

#################################################
;local a.b.c.d 绑定的IP 地址,可以不填
port 1194 绑定的端口
proto tdp 选择udp 协议,也可用tcp
# "dev tun" will create a routed IP tunnel,
# "dev tap" will create an ethernet tunnel.
;dev tap
#使用tun 设备,路由IP 通道。FreeBSD 默认支持TUN/TAP 驱动。
dev tun
#下面4 处填上面为vpn server 生成的证书和密钥
ca /etc/openvpn/ca.crt (如果启动的时候带了cd路径,则这里不需要绝对路径)

cert /etc/openvpn/server.crt
key /etc/openvpn/server.key  # This file should be kept secret
dh /etc/openvpn/dh1024.pem

#配置vpn 虚拟网段,会在vpn server 建立一条10.0.0.1 >10.0.0.2 的点对点虚拟链路。
server 10.8.0.0 255.255.255.0
#记录client 对应得虚拟地址,默认600 秒更新一次
ifconfig-pool-persist ipp.txt
#等价于ping 10 和ping-restart 120,对于vpn 机器有可能断开外网联线或者重起的,
#或者 vpn client 先于vpn server 运行的情况下很重要。Client 会重新连接。
keepalive 10 120
# 开启压缩支持。如果编译的时候没有使用LZO 库则注释本行
#comp-lzo
#初始化完成后改变OpenVPN 进程的用户ID 为nobody
user nobody
#同上,改变进程的组ID 为nobody。如果是Windows 系统,注释掉这两行
group nobody
persist-key
persist-tun
#默认每分钟更新状态文件,可以看到client 的真实IP 虚拟IP 等信息。
status openvpn-status.log
verb 3

    2、client端

client1.conf 使用源代码包目录sample-config-files 里的client.conf 修改即可

client
dev tun
proto tcp #和server.conf 一致即可
remote 172.16.6.79 1194 #指定vpn server 的IP 地址和端口
resolv-retry infinite #如果解析vpn server 失败则无限次重试
nobind
user nobody
group nobody
persist-key
persist-tun
ca ca.crt
cert client1.crt #这2 行填给client1 生成的密钥文件。client2 client3 相应更改。
key client1.key
#comp-lzo
keepalive 10 120

如果有其他的客户端需要连过来  也采取同样的配置

    3、分配client端的联入地址

给server.confi添加如下行:
client-config-dir ccd
并在vpn server 的/etc/openvpn 目录建立ccd 子目录,并在ccd 目录下建立文件client1,client2,client3(这里的文件名对应”为vpn clients 产生证书和密钥”步骤里的Common Name),当对应得client 连接时,vpn server 会读去对应文件里的配置信息。文件client1 里输入:
ifconfig-push 10.8.0.5 10.8.0.6,即给vpn client1 指定10.8.0.5,对端10.8.0.6 的虚拟链路。

如果有其他的客户端也同样处理,如

client2 内容:ifconfig-push 10.8.0.9 10.8.0.10
client3 内容:ifconfig-push 10.8.0.13 10.8.0.14

分别拷贝server.conf,client1.conf,client2.conf,client3.conf 到vpn server 和对应的client。

    4、启动vpn

分别在各个vpn 计算机启动vpn server 和vpn client

启动server的时候用 守护进程启动

./openvpn –daemon –config /etc/openvpn/server.conf

启动client的时候 用client的配置文件

./openvpn –daemon –config /etc/openvpn/client.conf

查看端口1194端口是否起来,如果两个都起来则查看ifconfig 看vpn网卡是否起来

到这一步,VPN 客户端和服务端可以相互通讯了。10.8.0.1 应该可以ping 通10.8.0.5,10.8.0.9,10.8.0.13。10.8.0.5,10.8.0.9,10.8.0.13 也可以ping 通10.8.0.1

但是此时还仅仅是vpn 客户端与服务器可以通,但此时,vpn的客户端还不能访问服务器端同网段内机器

    5、 vpn client 访问vpn server 所在172.16.6.0/24 网段内计算机

第一种方法,添加如下行到server.conf
push "route 172.16.6.0 255.255.255.0"
push 选项是把选项内容给连接的vpn 客户端执行,也就是在vpn 客户端执行“route 172.16.6.0 255.255.255.0”,即当client 连接时,添加路由到client 的路由表里。对于vpn client1,该选项相当于在vpn client1 的路由表里添加到网络172.16.6.0/24,下一跳为10.8.0.6的路由,这里10.8.0.6 为给vpn client1 分配的虚拟IP 对的服务器端IP,相当于路由到vpn server。

第二种方法,如果不改变 server.conf,在vpn client1 里route add –net 172.16.6.0 10.8.0.6 255.255.255.0 效果一样,也可route add –net 172.16.6.0 10.8.0.1 255.255.255.0 直接路由到vpn server 的本端IP 也可,但这样需要在所有的vpn 客户端改变路由表。

这样在vpn 客户端可以ping 通172.16.6.0/24 网段内部计算机了。到此,vpn客户端可以访问服务端同网段的服务器了。

    6、总结
在vpn server 的/etc/rc.local 文件添加如下行:
/usr/local/openvpn/openvpn –daemon --config /etc/openvpn/server.conf --cd /etc/openvpn/
在vpn client1 的/etc/rc.local 文件添加如下行:
/usr/local/openvpn/openvpn –daemon --config /etc/openvpn/client1.conf --cd /etc/openvpn/

这样完成了所有的配置。下面总结如下:
vpn server 的目录/etc/openvpn 下有文件ca.crt,ca.key,dh1024.pem,server.crt,server.key和server.conf 以及子目录ccd。/etc/openvpn/ccd 目录下有文件client1,client2 和client3。

/etc/openvpn/server.conf 内容如下:
;local a.b.c.d
port 1194
proto tcp
dev tun
ca ca.crt
cert server.crt
key server.key # This file should be kept secret
dh dh1024.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
keepalive 10 120
comp-lzo
user nobody
group nobody
persist-key
persist-tun
status openvpn-status.log
verb 3
client-config-dir ccd
#使vpn clients 能访问vpn server 内部网段计算机
push "route 172.16.6.0 255.255.255.0"
#route 172.31.13.0 255.255.255.0 #使vpn server 能访问中文网计算机

/etc/openvpn/ccd/client1 内容如下:
ifconfig-push 10.8.0.5 10.8.0.6
iroute 172.31.13.0 255.255.255.0

vpn client1 的目录/etc/openvpn 下有文件ca.crt,ca.key client1.crt,client1.key 和client1.conf。
/etc/openvpn/client1.conf 内容如下:
##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server. #
##############################################
client
dev tun
proto udp
remote 172.16.6.79 1194
resolv-retry infinite
nobind
user nobody
group nobody
persist-key
persist-tun
ca ca.crt
cert client1.crt
key client1.key
comp-lzo
verb 3
keepalive 10 120

0 0