Monday, March 28, 2011

Linux下OpenVPN的培植具体教程


Linux下OpenVPN的培植具体教程本文是在Fedora Core 2情形下设置装备摆设的,因为机惺ё仝收集中不是在网关的位置,所以使用NAT体例来访谒内网,否则还涉及到设置装备摆设收集设备的路由。假设我的VPN Server有2块网卡,eth0对外,IP:61.1.1.2 eth1对内,IP:192.168.1.2,内网地址:192.168.0.0/16本文除设置装备摆设文件中行首的#是注释外,其他行首的#都是提醒符,如不美观在非设置装备摆设文件一行中第二次呈现#声名后面的是注释,书写呼吁时可以省略。获取并安装Openvpn:首先搜检系统是否安装lzo实时压缩工具$rpm -qa | grep lzo如 不美观没有安装可以在[url]http://www.oberhumer.com/opensource/lzo/[/url]找加入安装,安装体例详见压 缩包中的INSTALL文件,当然也可以用rpm包安装,记住必然要安装lzo-devel开首的阿谁包,因为OpenVPN需要使用lzo的头文件。$wget [url]http://mesh.dl.sourceforge.net/sourceforge/openvpn/openvpn-2.0_rc16.tar.gz[/url]$tar -zxvf openvpn-2.0_rc16.tar.gz$cd openvpn-2.0_rc16$./configure$make$su#make install按照INSTALL文件中的声名,做如下操作:#mknod /dev/net/tun c 10 200 #建树一个tun设备#echo alias char-major-10-200 tun /etc/modprobe.conf#echo 1 /proc/sys/net/ipv4/ip_forward #打开系统的转发功能接下来就生成处事器客户端需要使用的keys了,为了便利,我们使用OpenVPN包自带的剧本生成。#mkdir /etc/openvpn#cp -r easy-rsa /etc/openvpn #切换到OpenVPN源代码目录执行改削vars 文件————-CUT Here————-# easy-rsa parameter settings# NOTE: If … 继续阅读

Linux下OpenVPN的培植具体教程本文是在Fedora Core 2情形下设置装备摆设的,因为机惺ё仝收集中不是在网关的位置,所以使用NAT体例来访谒内网,否则还涉及到设置装备摆设收集设备的路由。假设我的VPN Server有2块网卡,eth0对外,IP:61.1.1.2 eth1对内,IP:192.168.1.2,内网地址:192.168.0.0/16本文除设置装备摆设文件中行首的#是注释外,其他行首的#都是提醒符,如不美观在非设置装备摆设文件一行中第二次呈现#声名后面的是注释,书写呼吁时可以省略。获取并安装Openvpn:首先搜检系统是否安装lzo实时压缩工具$rpm -qa | grep lzo如 不美观没有安装可以在[url]http://www.oberhumer.com/opensource/lzo/[/url]找加入安装,安装体例详见压 缩包中的INSTALL文件,当然也可以用rpm包安装,记住必然要安装lzo-devel开首的阿谁包,因为OpenVPN需要使用lzo的头文件。$wget [url]http://mesh.dl.sourceforge.net/sourceforge/openvpn/openvpn-2.0_rc16.tar.gz[/url]$tar -zxvf openvpn-2.0_rc16.tar.gz$cd openvpn-2.0_rc16$./configure$make$su#make install按照INSTALL文件中的声名,做如下操作:#mknod /dev/net/tun c 10 200 #建树一个tun设备#echo alias char-major-10-200 tun /etc/modprobe.conf#echo 1 /proc/sys/net/ipv4/ip_forward #打开系统的转发功能接下来就生成处事器客户端需要使用的keys了,为了便利,我们使用OpenVPN包自带的剧本生成。#mkdir /etc/openvpn#cp -r easy-rsa /etc/openvpn #切换到OpenVPN源代码目录执行改削vars 文件————-CUT Here————-# easy-rsa parameter settings# NOTE: If you installed from an RPM,# don’t edit this file in place in# /usr/share/openvpn/easy-rsa –# instead, you should copy the whole# easy-rsa directory to another location# (such as /etc/openvpn) so that your# edits will not be wiped out by a future# OpenVPN package upgrade.# This variable should point to# the top level of the easy-rsa# tree.export D=`pwd`# This variable should point to# the openssl.cnf file included# with easy-rsa.export KEY_CONFIG=$D/openssl.cnf# Edit this variable to point to# your soon-to-be-created key# directory.## WARNING: clean-all will do# a rm -rf on this directory# so make sure you define# it correctly!export KEY_DIR=$D/keys# Issue rm -rf warningecho NOTE: when you run ./clean-all, I will be doing a rm -rf on $KEY_DIR# Increase this to 2048 if you# are paranoid. This will slow# down TLS negotiation performance# as well as the one-time DH parms# generation process.export KEY_SIZE=1024# These are the default values for fields# which will be placed in the certificate.# Don’t leave any of these fields blank.# 界说你地址的国家,2个字符export KEY_COUNTRY=CN# 你地址的省份export KEY_PROVINCE=Liaoning# 你地址的城市export KEY_CITY=Shenyang# 你地址的组织export KEY_ORG=ELM OpenVPN ORG# 你的单元export KEY_OU=OpenVPN Service# 你的邮件地址export KEY_EMAIL=elm@elm.freetcp.com————-CUT Here—————–改削后保留,下面我们起头什成keys,以下为shell呼吁 # 为提醒符#. vars #使改削的变量生效NOTE: when you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys#./clean-all #初始化keys目录,建树所需要的文件和目录#./build-ca #什成Root CA证书,用于签发Server和Client证书,请呵护好keys/ca.key文件。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 incorporatedinto 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 blankFor 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) [Liaoning]:Locality Name (eg, city) [Shenyang]:Organization Name (eg, company) [ELM OpenVPN ORG]:Organizational Unit Name (eg, section) [OpenVPN Service]:Common Name (eg, your name or your server’s hostname) []:OpenVPN Root CAEmail Address [elm@elm.freetcp.com]:# ls keysca.crt ca.key index.txt serial我们可以看到ca.crt ca.key文件已经什成了。下面我们为处事器生成 Diffie-Hellman 文件# ./build-dh #TLS server 需要使用的一个文件Generating DH parameters, 1024 bit long safe prime, generator 2This is going to take a long time..+……………………………………………………..+…………………………………………………………..+….+……..+………+…………………………………………….+.+………………………………………………………………………………………………………………………………………………+…………………………………+……………………………+………….+………………………………………………………………………+……………………………………………….+………………………..++*++*++*建树并签发VPN Server使用的CA# ./build-key-server server # server 为建树后的文件名,分袂为server.crt server.keyGenerating 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 incorporatedinto 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 blankFor 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) [Liaoning]:Locality Name (eg, city) [Shenyang]:Organization Name (eg, company) [ELM OpenVPN ORG]:Organizational Unit Name (eg, section) [OpenVPN Service]:Common Name (eg, your name or your server’s hostname) []:Server No.1Email Address [elm@elm.freetcp.com]:Please enter the following ‘extra’ attributesto be sent with your certificate requestA challenge password []:An optional company name []:Using configuration from /etc/openvpn/easy-rsa/openssl.cnfCheck that the request matches the signatureSignature okThe Subject’s Distinguished Name is as followscountryName :P RINTABLE:’CN’stateOrProvinceName :P RINTABLE:’Liaoning’localityName :P RINTABLE:’Shenyang’organizationName :P RINTABLE:’ELM OpenVPN ORG’organizationalUnitName:PRINTABLE:’OpenVPN Service’commonName :P RINTABLE:’Server No.1′emailAddress :IA5STRING:’elm@elm.freetcp.com’Certificate is to be certified until Feb 26 14:43:44 2015 GMT (3650 days)Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]yWrite out database with 1 new entriesData Base Updated接下来为VPN Client公布CA证书,如不美观往后要为其他Client公布证书,直接使用build-key呼吁签发新证书。# ./build-key elmGenerating a 1024 bit RSA private key……..++++++………………..++++++writing new private key to ‘elm.key’—–You are about to be asked to enter information that will be incorporatedinto 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 blankFor 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) [Liaoning]:Locality Name (eg, city) [Shenyang]:Organization Name (eg, company) [ELM OpenVPN ORG]:Organizational Unit Name (eg, section) [OpenVPN Service]:Common Name (eg, your name or your server’s hostname) []:ELM #注重Common Name最好不要不异,如不美观不异[server要加duplicate-cn选项],那么Email地址也不能不异Email Address [elm@elm.freetcp.com]:Please enter the following ‘extra’ attributesto be sent with your certificate requestA challenge password []:An optional company name []:Using configuration from /etc/openvpn/easy-rsa/openssl.cnfCheck that the request matches the signatureSignature okThe Subject’s Distinguished Name is as followscountryName :P RINTABLE:’CN’stateOrProvinceName :P RINTABLE:’Liaoning’localityName :P RINTABLE:’Shenyang’organizationName :P RINTABLE:’ELM OpenVPN ORG’organizationalUnitName:PRINTABLE:’OpenVPN Service’commonName :P RINTABLE:’ELM’emailAddress :IA5STRING:’elm@elm.freetcp.com’Certificate is to be certified until Feb 26 14:45:36 2015 GMT (3650 days)Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]yWrite out database with 1 new entriesData Base Updated为防止恶意抨击袭击(如DOS、UDP port flooding),我们生成一个HMAC firewall#openvpn –genkey –secret keys/ta.key生成证书明日销链文件,防止日后有人丢失踪证书,被犯警用户接入VPN#./make-crl vpncrl.pemUsing configuration from /etc/openvpn/easy-rsa/openssl.cnf到此刻为止,一切筹备停当,下面起头写设置装备摆设文件,为了缩小篇幅,把原有注释都去失踪了。Server使用的设置装备摆设文件server.conf—————-CUT Here————-#声名本机使用的IP地址,也可以不声名;local a.b.c.d#声名使用的端口,默认1194port 1194#声名使用的和谈,默认使用UDP,如不美观使用HTTP proxy,必需使用TCP和谈;proto tcpproto udp#声名使用的设备可选tap和tun,tap是二层设备,撑持链路层和谈。#tun是ip层的点对点和谈,限制稍微多一些,本人习惯使用TAP设备dev tap;dev tun#OpenVPN使用的ROOT CA,使用build-ca生成的,用于验证客户是证书是否正当ca ca.crt#Server使用的证书文件cert server.crt#Server使用的证书对应的key,注重文件的权限,防止被盗key server.key # This file should be kept secret#CRL文件的声名,被明日销的证书链,这些证书将无法登录crl-verify vpncrl.pem#膳缦沔提到的生成的Diffie-Hellman文件dh dh1024.pem#这是一条呼吁的合集,如不美观你是OpenVPN的老用户,就知道这条呼吁的出处#这条呼吁等效于:# mode server #OpenVPN工作在Server模式,可以撑持多client同时动态接入# tls-server #使用TLS加密传输,本端为Server,Client端为tls-client## if dev tun: #如不美观使用tun设备,等效于以下设置装备摆设# ifconfig 10.8.0.1 10.8.0.2 #设置当地tun设备的地址# ifconfig-pool 10.8.0.4 10.8.0.251 #声名OpenVPN使用的地址池(用于分配给客户),分袂是肇端地址、竣事地址# route 10.8.0.0 255.255.255.0 #增添一条静态路由,省略下一特意址,下一跳为对端地址,这里是: 10.8.0.2# if client-to-client: #如不美观使用client-to-client这个选项# push route 10.8.0.0 255.255.255.0 #把这条路由发送给客户端,客户毗连成功后自动插手路由表,省略了下一特意址: 10.8.0.1# else# push route 10.8.0.1 #否则发送本条路由,这是一个主机路由,省略了子网掩码和下一特意址,分袂为: 255.255.255.255 10.8.0.1## if dev tap: #如不美观使用tap设备,则等效于以下呼吁# ifconfig 10.8.0.1 255.255.255.0 #设置装备摆设tap设备的地址# ifconfig-pool 10.8.0.2 10.8.0.254 255.255.255.0 #客户端使用的地址池,分袂是肇端地址、竣事地址、子网掩码# push route-gateway 10.8.0.1 #把情形变量route-gateway传递给客户机#server 10.8.0.0 255.255.255.0 #等效于以上呼吁#用于记实某个Client获得的IP地址,近似于dhcpd.lease文件,#防止openvpn年夜头启动后健忘Client曾经使用过的IP地址ifconfig-pool-persist ipp.txt#Bridge状况下近似DHCPD的设置装备摆设,为客户分配地址,因为这里工作在路由模式,所以不使用;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100#经由过程VPN Server往Client push路由,client经由过程pull指令获得Server push的所有选项并应用;push route 192.168.10.0 255.255.255.0;push route 192.168.20.0 255.255.255.0#VPN启动后,在VPN Server上增添的路由,VPN遏制后自动删除;route 10.9.0.0 255.255.255.252#Run script or shell command cmd to validate client#virtual addresses or routes. 具体查看manual;learn-address ./script#其他的一些需要PUSH给Client的选项##使Client的默认网关指向VPN,让Client的所有Traffic都经由过程VPN走;push redirect-gateway#DHCP的一些选项,具体查看Manual;push dhcp-option DNS 10.8.0.1;push dhcp-option WINS 10.8.0.1#如不美观可以让VPN Client之间彼此访谒直接经由过程openvpn轨范转发,#不用发送到tun或者tap设备后年夜头转发,优化Client to Client的访谒效率client-to-client#如不美观Client使用的CA的Common Name有一再了,或者说客户都使用不异的CA#和keys毗连VPN,必然要打开这个选项,否则只许可一小我毗连VPN;duplicate-cn#NAT后面使用VPN,如不美观VPN长时刻欠亨信,NAT Session可能会失踪效,#导致VPN毗连丢失踪,为防止之类工作的发生,keepalive供给一个近似于ping的机制,#下面暗示每10秒经由过程VPN的Control通道ping对方,如不美观持续120秒无法ping通,#认为毗连丢失踪,并年夜头启动VPN,年夜头毗连#(对于mode server模式下的openvpn不会年夜头毗连)。keepalive 10 120#膳缦沔提到的HMAC防火墙,防止DOS抨击袭击,对于所有的节制信息,都使用HMAC signature,#没有HMAC signature的节制信息不予措置,注重server端后面的数字必定使用0,client使用1tls-auth ta.key 0 # This file is secret#对数据进行压缩,注重Server和Client一致comp-lzo#界说最年夜毗连数;max-clients 100#界耸ё偎行openvpn的用户user nobodygroup nobody#经由过程keepalive检测超时后,年夜头启动VPN,不年夜头篡夺keys,保留第一次使用的keyspersist-key#经由过程keepalive检测超时后,年夜头启动VPN,一向连结tun或者tap设备是linkup的,#否则收集毗连会先linkdown然后linkuppersist-tun#按期把openvpn的一些状况信息写到文件中,以便自己写轨范计费或者进行其他操作status openvpn-status.log#记实日志,每次年夜头启动openvpn后删除原有的log信息log /var/log/openvpn.log#和log一致,每次年夜头启动openvpn后保留原有的log信息,新信息追加到文件最后;log-append openvpn.log#相当于debug level,具体查看manualverb 3————–Cut Here—————–把server.conf文件保留到/etc/opennvpn目录中,并把使用easy-rsa下的剧本什成的key都复制到/etc/openvpn目录下,呼吁如下:#cd /etc/openvpn#cp easy-rsa/keys/ca.crt .#cp easy-rsa/keys/server.crt .#cp easy-rsa/keys/server.key .#cp easy-rsa/keys/dh1024.pem .#cp easy-rsa/keys/ta.key .#cp easy-rsa/keys/vpncrl.pem .建树OpenVPN启动剧本,可以在源代码目录中找到,在sample-scripts目录下的openvpn.init文件,将其复制到/etc/init.d/目录中,更名为openvpn然后运行:#chkconfig –add openvpn#chkconfig openvpn on盗舸骠动openenvpn#/etc/init.d/openvpn start接下来设置装备摆设客户端的设置装备摆设文件client.conf:Linux或Unix下使用扩展名为.conf Windows下使用的是.ovpn,并把需要使用的keys复制到设置装备摆设文件地址目录ca.crt elm.crt elm.key ta.key————-Cut Here———————# 声名我们是一个client,设置装备摆设年夜server端pull过来,如IP地址,路由信息之类Server使用push指令push过来的client#指假寓口的类型,严酷和Server端一致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# 使用的和谈,与Server严酷一致;proto tcpproto udp#设置Server的IP地址和端口,如不美观有多台机械做负载平衡,可以多次呈现remote关头字remote 61.1.1.2 1194;remote my-server-2 1194# 随机选择一个Server毗连,否则按照挨次年夜上到虾毗连;remote-random# 始终年夜头解析Server的IP地址(如不美观remote后面跟的是域名),# 保证Server IP地址是动态的使用DDNS动态更新DNS后,Client在自动年夜头毗连时年夜头解析Server的IP地址# 这样无需酬报年夜头启动,即可年夜头接入VPNresolv-retry infinite# 在本机不邦定任何端口监听incoming数据,Client无需此操作,除非一对一的VPN有需要nobind# 运行openvpn用户的身份,旧版本在win下需要把这两行注释失踪,新版本无需此操作user nobodygroup nobody#在Client端增添路由,使得所有访谒内网的流量都经由VPN出去#当然也可以在Server的设置装备摆设文件里头设置,Server设置装备摆设里头使用的呼吁是# push route 192.168.0.0 255.255.255.0route 192.168.0.0 255.255.0.0# 和Server设置装备摆设上的功能一样如不美观使用了chroot或者su功能,最好打开下面2个选项,防止年夜头启动后找不到keys文件,或者nobody用户没有权限启动tun设备persist-keypersist-tun# 如不美观你使用HTTP代办代庖毗连VPN Server,把Proxy的IP地址和端口写到下面# 如不美观代办代庖需要验证,使用http-proxy server port [authfile] [auth-method]# 其中authfile是一个2行的文本文件,用户名和密码各占一行,auth-method可以省略,具体信息查看Manual;http-proxy-retry # retry on connection failures;http-proxy [proxy server] [proxy port #]# 对于无线设备使用VPN的设置装备摆设,看看就年夜白了# Wireless networks often produce a lot# of duplicate packets. Set this flag# to silence duplicate packet warnings.;mute-replay-warnings# Root CA 文件的文件名,用于验证Server CA证书正当性,经由过程easy-rsa/build-ca生成的ca.crt,和Server设置装备摆设里的ca.crt是统一个文件ca ca.crt# easy-rsa/build-key生成的key pair文件,膳缦沔生成key部门中有提到,分歧客户使用分歧的keys改削以下两行设置装备摆设并使用他们的keys即可。cert elm.crtkey elm.key# Server使用build-key-server剧本什成的,在x509 v3扩展中插手了ns-cert-type选项# 防止VPN client使用他们的keys + DNS hack棍骗vpn client毗连他们冒充的VPN Server# 因为他们的CA琅缦慊有这个扩展ns-cert-type server# 和Server设置装备摆设里一致,ta.key也一致,注重最后参数使用的是1tls-auth ta.key 1# 压缩选项,和Server严酷一致comp-lzo# Set log file verbosity.verb 4————–Cut Here———————Linux下Client的OpenVPN的安装体例一样,只是设置装备摆设文件和keys上的分歧,只要把client.conf ca.crt elm.crt elm.key ta.key复制到/etc/openvpn目录即可启动VPN。Win下OpenVPN的安装,WIN下有图形界面的OpenVPN-GUI轨范,下载地址[url]http://openvpn.se[/url]下 载安装,默认下一步就OK了,安装完事后在托盘上呈现一个新的停笔,把client.ovpn ca.crt elm.crt elm.key ta.key文件拷贝到C:Program Filesopenvpnconfig目录下,然后点右键connect就OK了。对于LinuxServer使用NAT的一些声名:首先要把系统的Forward打开,可以经由过程如下呼吁实现echo 1 /proc/sys/net/ipv4/ip_forward或者使用sysctl -w net.ipv4.ip_forward=1或者改削/etc/sysctl.conf文件,增添net.ipv4.ip_forward = 1设定SNAT的轨则,使用iptables呼吁iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth1 -j SNAT –to-source 192.168.1.2OK,OpenVPN Client可以访谒内网了。





Published by
Published by xFruits
Original source : http://www.vpn123.tk/?p=214...

No comments:

Post a Comment