找回密码
 立即注册
CeraNetworksBGVM服务器主机交流IP归属甄别会员请立即修改密码
查看: 145|回复: 9

CentOS上OpenVPN的安装和配置详解

[复制链接]

965

主题

5377

回帖

2万

积分

论坛元老

积分
21384
发表于 2009-3-29 12:18:47 | 显示全部楼层 |阅读模式
yum install openvpn
yum install pam*
updatedb
locate openvpn

默认安装好OpenVPN的主配置路径/etc/openvpn/下是空的, 将模版中的easy-rsa的目录复制到/etc/openvpn/的路径下:
[root@VPNSRV01]# cp -r /usr/share/doc/openvpn-2.0.9/easy-rsa/ /etc/openvpn/
编辑/etc/openvpn/easy-rsa/vars,并在最后添加:

     46 export KEY_COUNTRY=CN
     47 export KEY_PROVINCE=Shanghai
     48 export KEY_CITY=Shanghai
     49 export KEY_ORG=”YiXin-VPN”
     50 export KEY_EMAIL=”zlei@igncorp.com

保存退出,再运行:
source vars
./clean-all
./build-ca

只需要填写Common Name, 其他全部回车即可,如果这里发觉自己填错的话,执行clean-all脚本,清空keys下刚生成的密钥。然后使用build-ca重新做。如果到后面生成服务器和客户端的证书和密钥的时候想要返工就工作量大了。)

./build-key-server opensvr1
./build-client client1
./build-dh

编辑配置文件/etc/openvpn/server.conf

;local a.b.c.d  (设定监听在本机的哪个网络接口上,这里使用默认注释,表示监听所有本机上的网络接口)
port 1194   (这里设定的监听端口,默认是1194)
proto udp (设定在传输层使用的协议,这里设定为默认的UDP协议)
dev tun(设定传输设备节点。如提示信息,tun是一个三层设备,tap是一个二层设备。而这里我们要的是IP路由,是三层的方式,因此选择tun)
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key? # This file should be kept secret
dh /etc/openvpn/keys/dh1024.pem
#设定根证书CA、服务器证书、以及服务器密钥文件的位置。注意,这里我都是写上了绝对路径,那是因为我更动了它们的位置。这里也可以直接写文件名而不写绝对路径,表示使用默认路径,默认为/etc/openvpn/下
server 192.168.1.0 255.255.255.0 (设定Server端虚拟出来的VPN网段)
ifconfig-pool-persist ipp.txt(设定虚拟地址租约文件,用于记录某个Client获得的IP地址,类似于dhcpd.lease文件,防止openvpn重新启动后“忘记”Client曾经使用过的IP地址)
push “route 192.168.1.0 255.255.255.0″(设定Push路由。当Client连接Server的时候,自动会得到这些路由条目并添加到它们的路由表中,由于是Server那里传过来的,因此叫Push路由。当Client从Server处断开的时候这些Push路由将自动在Client的路由表中删除。一个需要提醒注意的地方就是既然是加入路由,那么必须要填写的是”route 网段 子网掩码”的格式,如果不是像第三条那样是添加针对某一个主机的路由的话,那么一定要写的是网段!如果要针对一个目的网段的路由,而却写的是主机地址的话,那么这个Push路由将失败)
;push “redirect-gateway”
;push “dhcp-option DNS 10.8.0.1″
;push “dhcp-option WINS 10.8.0.1″
(设定其他的Push信息。redirect-gateway为接入Client重新指定出口网关,如果不设定的话则是使用Server路由表当中的默认出口网关。dhcp-option下的DNS和WIN则是为接入Client重新分配域名服务器和名称服务器的IP地址。除非特殊的规划,一般这里没有设定的必要,保持注释)
;duplicate-cn
(设定是否允许单证书多连接。如果有多个Client使用相同的证书接入Server,亦或Client使用的CA的Common Name有重复了,或者说客户都使用相同的CA和keys连接VPN,一定要打开这个选项,否则只允许一个人连接VPN。但是如果出于安全考虑,比如一个证书只能由一个Client对应的情况,这里就建议关闭而保留注释)
client-to-client(设定接入的Client之间能够被允许互相访问,默认情况下接入的Client是不能互相访问的。如果需要使它们互相访问的话请去掉默认的注释)
keepalive 10 120(设定保活参数。这里的意思是每10秒钟通过Ping来确定Client是否存活,当然这个Ping的进行是在虚拟通道中而不是在真实外部链路上的,超过120秒无反馈表示丢失该Client)
tls-auth /etc/openvpn/vpnkeys/ta.key 0 # This file is secret
(设定ta密钥的路径。之前提到的HMAC防火墙,防止DOS攻击,对于所有的控制信息,都使用HMAC signature,没有HMAC signature的控制信息不予处理,注意server端后面的数字肯定使用0,client使用1)
comp-lzo(使用Lzo功能对虚拟链路进行压缩。如果Server端开启的话,那么连接它的Client端也要在配置文件中开启)
user nobody
group nobody(设定OpenVPN服务的宿主用户,这里设定nobody。使用vipw来快速查看/etc/passwd文件,可以发现nobody是系统内置的,并且UID和GID分别为99。另外要注意的是,既然将OpenVPN服务的宿主用户设定为nobody,那么凡是关于OpenVPN服务进程相关或者需要读写的文件,请都要赋予nobody权限)
persist-key (设定连接保持密钥功能。在由于keepalive检测超时后而重新启动VPN的情况,不重新读取keys,而保留第一次使用的keys)
persist-tun(设定连接保持在线功能。在由于keepalive检测超时后而重新启动VPN的情况,一直保持tun或者tap设备是linkup的,否则网络连接会先linkdown然后linkup)
status openvpn-status.log (设定状态记录日志路径。状态记录日志会定期把openvpn的一些状态信息写到文件中,以便自己写程序计费或者进行其他操作。注意,如果更改过OpenVPN服务宿主用户的话,请记得这里将此文件赋予宿主用户一定的权限)

log         /var/log/openvpn.log (设定OpenVPN的服务日志路径。注意,如果更改过OpenVPN服务宿主用户的话,请记得这里将此文件赋予宿主用户一定的权限)
log-append  /var/log/openvpn.log (此项和log项配合使用,每次重新启动openvpn后保留原有的log信息,新信息追加到文件最后)
verb 4

启动

chown nobody:nobody openvpn-status.log
chown nobody:nobody /var/log/openvpn.log
/usr/sbin/openvpn –config /usr/local/openvpn/etc/server.conf

检查系统防火墙,是否为VPN开启了。
# iptables -A INPUT -p udp –dport 9988 -j ACCEPT
# iptables -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT
检查防火墙
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination        
ACCEPT     udp  –  anywhere             anywhere            udp dpt:9988
ACCEPT     all  –  anywhere             anywhere            state RELATED,ESTABLISHED
4.转发开关是否打开,打开这个路由转发开关
# echo 1 > /proc/sys/net/ipv4/ip_forward
并且顺带将这个命令添加到启动脚本/etc/rc.d/rc.local当中,使得每次系统启动的时候都会打开这个开关

再次启动openvpn

service start openvpn

ifconfig

会看到eth0,lo之外还有个tun0,这个tun0设备就是OpenVPN虚拟出来的网络接口。另外需要提到的一点是,这个设备会被初始化,但是只有在OpenVPN的服务进程成功运行起来的时候才会有这个设备,如果运行OpenVPN进程失败的话,是看不到这个设备的。也所以,当要使用OpenVPN的时候,切记要为它的网络接口开放特定的防火墙,或者干脆关闭防火墙。

查看Server的路由

route

配置客户端

将Server上之前为测试Client生成的证书密钥以及一些相关文件复制到Client端的/etc/openvpn/目录下这里,Client需要向Server得到五个重要的文件
CA证书文件               (我这里是ca.crt)
Client证书文件           (我这里是vpnclnt00.crt)
Client密钥文件            (我这里是vpnclnt00.key)
Client的SSL证书请求文件  (我这里是vpnclnt00.csr)
ta密钥文件               (我这里是ta.key)
配置Client端的主配置文件
Client上也安装了OpenVPN的包,所以也有模版文件
/usr/share/doc/openvpn-2.0.9/sample-config-files/client.conf
这里将Client的主配置模版文件复制到OpenVPN的主配置路径/etc/openvpn/下

可以看到在Client的主配置路径下一共有6个文件。5个是证书密钥,1个是Client端的主配置文件client.conf
现在我们来编辑客户端主配置文件client.conf,

client(声明这个配置文件作用于客户端)
dev tun(使用tun三层虚拟连接设备)
proto udp(使用UDP协议)
remote 123.123.123.233 9988(设定远程Server的IP地址和端口,这里要和Server对应起来。如果有多个Server可以连接的话可以配置多条remote,一行一条)
resolv-retry infinite(始终重新解析Server的IP地址,如果remote后面跟的是域名,保证Server IP地址是动态的使用DDNS动态更新DNS后,Client在自动重新连接时重新解析Server的IP地址,这样无需人为重新启动,即可重新接入VPN)
nobind(表示Client端不像Server端那样需要开放特定的端口,nobind的意思就是Client的不绑定特定的监听端口)
user nobody
group nobody(指定OpenVPN服务进程的宿主用户)
persist-key(设定连接保持密钥功能。在由于keepalive检测超时后而重新启动VPN的情况,不重新读取keys,而保留第一次使用的keys)
persist-tun(设定连接保持在线功能。在由于keepalive检测超时后而重新启动VPN的情况,一直保持tun或者tap设备是linkup的,否则网络连接会先linkdown然后linkup)
ca ca.crt
cert vpnclnt00.crt
key vpnclnt00.key(设定CA证书、Client证书以及Client密钥文件的路径。与之前我Server端的配置不同,这里我没有写绝对路径而只是写了一些文件名。这是因为我使用了默认的OpenVPN的主路径/etc/openvpn/。如果不是使用默认路径而是定制更改了这些文件的位置的话,那么就需要注上详细的绝对路径了)
ns-cert-type server(这一项是Server配置文件server.conf当中没有的。Server使用build-key-server脚本什成的,在x509 v3扩展中加入了ns-cert-type选项,为的是防止黑客操纵他们的VPN Client模拟成VPN Server,然后使用他们的keys + DNS欺骗其他的Client连接他们假冒的VPN Server。因为他们的CA里没有这个扩展)
tls-auth ta.key 1(设定ta密钥的路径。这里我使用/etc/openvpn/的默认路径。所以直接打了文件名而不是绝对路径。这个配置项是关于HMAC防火墙,防止DOS攻击,对于所有的控制信息,都使用HMAC signature,没有HMAC signature的控制信息不予处理。这里Client使用的值是1,注意server端后面的数字肯定使用0)
comp-lzo
verb 3  (Debug等级)
;mute 20(意义同Server端的设定)

在Windows下安装

1.下载并安装openvpn-2.0.9-gui-1.0.3-install.exe,注意版本号和服务器端保持一致,安装选项一切采用默认设置

2. 将ca.crt、client1.crt、client1.key复制到C

rogram FilesOpenVPNconfig。(不同用户使用不同的证书,每个证书包括.crt和.key两个文件,如client2.crt和client2.key)
回复

使用道具 举报

965

主题

5377

回帖

2万

积分

论坛元老

积分
21384
 楼主| 发表于 2009-3-29 12:20:01 | 显示全部楼层
参考2:

==============
For Server
==============
1. Download package files 下载文件
#wget http://openvpn.net/release/openvpn-2.0.9.tar.gz
#wget http://openvpn.net/release/lzo-1.08-4.rf.src.rpm

2. Install package
#rpmbuild –rebuild lzo-1.08-4.rf.src.rpm
#rpm -Uvh /usr/src/redhat/RPMS/i386/lzo-*.rpm

#rpmbuild -tb openvpn-2.0.9.tar.gz
#rpm -Uvh /usr/src/redhat/RPMS/i386/openvpn-2.0.9-1.i386.rpm

如果顺利就不会出下面提示了,

Error:
Failed build dependencies:
autoconf is needed by lzo-1.08-4.rf.i386
Answer:
yum install autoconf.noarch

Error:
Failed build dependencies:
pam-devel is needed by openvpn-2.0.9-1.i386
Answer:
yum install pam-devel

3. Copy the sample configuration files 按这个操作即可
#cp -r /usr/share/doc/openvpn-2.0.9/easy-rsa/ /etc/openvpn/
#cp /usr/share/doc/openvpn-2.0.9/sample-config-files/server.conf /etc/openvpn/

4. Configure the CA
#cd /etc/openvpn/easy-rsa/
#vi vars
export KEY_COUNTRY=CN
export KEY_PROVINCE=Beijing
export KEY_CITY=Beijing
export KEY_ORG=”VPN”
export KEY_EMAIL=”service@myemail”

#. ./vars
#./clean-all  

./clean-all  只需要做一次哟!

下面开始建立ca证书

5. Build the root CA
#./build-ca
……++++++
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) [VPN]:
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server’s hostname) []:VPN
Email Address [vpn@myvpn.ca.mail]:

6. Build the server key and crt
#./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) [Beijing]:
Locality Name (eg, city) [Beijing]:
Organization Name (eg, company) [VPN]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) []:VPN
Email Address [service@myemail]:

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/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:’Beijing’
localityName :PRINTABLE:’Beijing’
organizationName :PRINTABLE:’VPN’
organizationalUnitName:PRINTABLE:’’
commonName :PRINTABLE:’VPN’
emailAddress :IA5STRING:’service@myemail’
Certificate is to be certified until Jun 27 05:21:29 2017 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

这里 Common Name 是必须要填的,其他默认即可,之后的建立 key ,Common Name都是要不同的,

7. Build the Diffie Hellman parameters
#./build-dh
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
……….

8. Install the CA file which server request
#cp keys/ca.crt ../
#cp keys/dh1024.pem ../
#cp keys/ovpnsrv1.key ../
#cp keys/ovpnsrv1.crt ../

9. Setting the server configuration for OpenVPN
#cd ../
#vi server.conf
#proto tcp
proto udp
;dev tap
dev tun
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret
dh dh1024.pem
log         /var/log/openvpn.log
log-append  /var/log/openvpn.log
verb 5

10. Startup the service
#service openvpn restart
#chkconfig openvpn on

11. edit iptables

#vi /etc/sysconfig/iptables

-A RH-Firewall-1-INPUT -p udp -m udp –dport 1194 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp –dport 1194 -j ACCEPT
#service iptables restart

==============
For Client
==============

1.在 linux server 上建立客户端需要的 key

vpn新建帐号方法

[lilyenglish@lilyCenter ~]$ su -    ##################
Password:
[root@lilyCenter ~]# cd /etc/openvpn/easy-rsa/            #####################
[root@lilyCenter easy-rsa]# . ./vars                      #####################
NOTE: when you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
[root@lilyCenter easy-rsa]# ./build-key zb02   ############################
Generating a 1024 bit RSA private key
………….++++++
.++++++
writing new private key to ‘zb02.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) [LILYVPN]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) []:zb02   ####################  注意Common Name不能相同
Email Address [service@lilyenglish.com]:

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/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:’Beijing’
localityName          :PRINTABLE:’Beijing’
organizationName      :PRINTABLE:’LILYVPN’
commonName            :PRINTABLE:’zb02′
emailAddress          :IA5STRING:’service@lilyenglish.com
Certificate is to be certified until Dec  8 06:46:11 2018 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
[root@lilyCenter easy-rsa]# cp ./keys/zb02* /home/lilyenglish/keys/  ####################

客户端设置

拷贝  client.ovpn ca.crt zb01.key zb01.crt 到  C:\Program Files\OpenVPN\config 下

修改 client.ovpn 内容 88 ~90 行

ca ca.crt
cert zb01.crt
key zb01.key
回复

使用道具 举报

469

主题

1339

回帖

6315

积分

论坛元老

积分
6315
发表于 2009-10-11 20:41:49 | 显示全部楼层
"


这个怎么回事?
回复

使用道具 举报

965

主题

5377

回帖

2万

积分

论坛元老

积分
21384
 楼主| 发表于 2009-10-11 20:51:43 | 显示全部楼层
可能是源的问题,

可以自己编译安装。

wget http://openvpn.net/release/openvpn-2.1_rc20.tar.gz
tar xzf openvpn-2.1_rc20.tar.gz
cd openvpn-2.1_rc20
./configure
make
make install
回复

使用道具 举报

469

主题

1339

回帖

6315

积分

论坛元老

积分
6315
发表于 2009-10-11 21:18:59 | 显示全部楼层
太复杂了,放弃了
回复

使用道具 举报

965

主题

5377

回帖

2万

积分

论坛元老

积分
21384
 楼主| 发表于 2009-10-11 21:28:44 | 显示全部楼层
比pptpd复杂。
回复

使用道具 举报

20

主题

372

回帖

1358

积分

金牌会员

积分
1358
发表于 2009-10-12 09:41:50 | 显示全部楼层
太复杂了...
回复

使用道具 举报

1

主题

29

回帖

69

积分

注册会员

积分
69
发表于 2009-10-12 11:31:57 | 显示全部楼层
看来还是自己购买服务器搭建VPN比较稳定,用着不会断!
回复

使用道具 举报

965

主题

5377

回帖

2万

积分

论坛元老

积分
21384
 楼主| 发表于 2009-10-12 12:23:08 | 显示全部楼层
成本高呢。
回复

使用道具 举报

1

主题

29

回帖

69

积分

注册会员

积分
69
发表于 2009-10-12 15:15:51 | 显示全部楼层
不是有那种号称无限流量的虚拟机吗?当然不是真的听他那套,但那个价格还是可以的,一个人或几个人使用应该是不会超过CPU限制的吧!【自说自话ing···】
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|HS2V主机综合交流论坛

GMT+8, 2024-5-24 04:10 , Processed in 0.062116 second(s), 3 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表