Thursday, April 22, 2010

CentOS OpenVPN 配置

系统是CentOS 5 X86

首先,你要联系客服打开VPS的tun/tap、iptables要支持NAT(有些VPS的控制面板可以直接打开),也可以告诉客服,你要用 OpenVPN,让他把应该打开的都打开。

一:服务器

1. 下载LZO和OpenVPN

wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.03.tar.gz
wget http://openvpn.net/release/openvpn-2.1.1.tar.gz


2.安装OpenSSL,Gcc 和 make

yum install -y openssl-devel
yum install gcc
yum install make

5. 安装LZO和OpenVPN

tar zxvf lzo-2.03.tar.gz //解压
cd lzo-2.03
./configure
make
make install //安装
cd ..
tar zxvf openvpn-2.1_rc15.tar.gz //解压
cd openvpn-2.1_rc15
./configure
make
make install //安装
cd ..
cp /root/openvpn-2.1_rc15/easy-rsa/ -r /etc/openvpn //拷贝文件

6.生成证书

初始化PKI

cd /etc/openvpn/2.0/
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=FJ
export KEY_CITY=FZ
export KEY_ORG="Rex" //引号要用英文半角符号
export KEY_EMAIL="mail@mail.com" //引号要用英文半角符号
. vars

7.创建证书颁发机构(CA)

./clean-all //这行命令是清空Keys文件夹下的所有文件
./build-ca

接下来的信息你可以自己填写

8. 建立server key

./build-key-server server

接下来的信息你可以自己填写

9. 生成客户端 key

./build-key client1 // client1 可以自定义 这个是客户的名字

接下来的信息你可以自己填写

10.以此类推建立其他客户端 key,当然你也可以不用,也可以在使用中在添加,不过比较麻烦,文章后会有教程

./build-key client2
./build-key client3

注意在进入 Common Name (eg, your name or your server’s hostname) []: 的输入时, 每个证书输入的名字必须不同.

11. 生成Diffie Hellman参数 (这个很重要)

./build-dh

12. 将 keys 下的所有文件打包下载到本地(可以通过winscp,http,ftp等等……)

tar zcvf yourkeys.tar.gz keys/

13. 创建服务端配置文件

mkdir /etc/openvpn/2.0/conf
cp /root/openvpn-2.1_rc15/sample-config-files/server.conf /etc/openvpn/2.0/conf/server.conf

14.服务端配置文件(server.conf) 我建议使用winscp 把/etc/openvpn/2.0/conf/server.conf 下的server.conf 文件下载到本地用Notepad++工具编辑。这里给个参考

port 1194
proto udp
dev tun
ca /etc/openvpn/2.0/keys/ca.crt //这个需要按照具体目录设置
cert /etc/openvpn/2.0/keys/server.crt //这个需要按照具体目录设置
key /etc/openvpn/2.0/keys/server.key //这个需要按照具体目录设置
dh /etc/openvpn/2.0/keys/dh1024.pem //这个需要按照具体目录设置
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 10.8.0.1"
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
client-to-client
keepalive 10 120
comp-lzo
user nobody
group nobody
persist-key
persist-tun
status openvpn-status.log
verb 3

15.启动OpenVPN

setsid /usr/local/sbin/openvpn --config /etc/openvpn/2.0/conf/server.conf // 这个命令是能在你退出Putty继续让服务工作
/etc/rc.d/init.d/iptables start

16. OpenVPN 访问外网的设置(如果不设置,你连上后只能在你和VPS之间建立VPN)

关闭Putty后重新登录 输入

ifconfig

输出:如:

venet0:1 Link en

cap:UNSPEC

Hwaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00

inet addr:你的VPSIP地址 P-t-P: 你的VPSIP地址 Bcast: 你的VPSIP地址 Mask:255.255.255.255

“venet0”要记录下来!(不同的地方不一样)

继续输入

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j MASQUERADE

其中的venet0 按情况更改

继续输入

/etc/init.d/iptables save
/etc/init.d/iptables restart

注意 有的时候可能不行 那么可以尝试用这个命令

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source 你的服务器IP

17.查看

输入

sysctl -a | grep for

查看结果 如:

net.ipv4.conf.tun0.mc_forwarding = 0
net.ipv4.conf.tun0.forwarding = 1
net.ipv4.conf. venet0.mc_forwarding = 0
net.ipv4.conf. venet0.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

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

vi /etc/rc.local
/usr/local/sbin/openvpn --config /etc/openvpn/2.0/conf/server.conf &

19.到此 基本就可以链接上去试试了,访问下如ip138.com 这类的网站,看看你的IP哦~

二:客户端配置

1. 下载 openvpn-2.1_rc15-install.exe(此版本集成 OpenVPN GUI)

2.安装OpenVPN GUI(用默认安装)

3.配置

将上面yourkeys.tar.gz中的下列证书文件解压到 你的OpenVPN GUI安装路径\OpenVPN\config文件夹下

ca.crt client1.key client1.crt

记住ca.key 千万别泄露哦

4. 修改client.ovpn

把你的OpenVPN安装路径\OpenVPN\sample-config下的client.ovpn文件复制到你的OpenVPN安装路径\OpenVPN\config文件夹下,用记事本打开client.ovpn

找到remote my-server-1 1194,把my-server-1改成你的VPS IP地址

5. 双击 client.ovpn 即可启动 openvpn, 或者通过 OpenVPN 的控制启动 VPN

6.出现 Initialization Sequence Completed 表示成功

三:如何在一起都安装好之后继续生成客户端配置文件呢?

1.登录 Putty,输入

/cd /etc/openvpn/2.0/
. vars
./build-key client2 //client2是客户端名字 然后按照步骤继续生成即可

备注:
附上一个用于停止OpenVPN的命令:

killall -9 openvpn

=========================================================

然后cd /etc/openvpn/easy-rsa/2.0进入,生成OpenVPN需要的证书。

vi vars来编辑环境变量,这里涉及到编辑器vi的用法,不会用就自己google一下。

把最后几行根据实际情况修改:

export KEY_COUNTRY="CN"
export KEY_PROVINCE="GD"
export KEY_CITY="GZ"
export KEY_ORG="Black-Xstar Net Empire"
export KEY_EMAIL="webmaster [at] black-xstar.com"

保存后运行. vars设置生效。

接下来运行./build-ca server创建证书颁发机构。

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) [GZ]:回车
Organization Name (eg, company) [Black-Xstar Net Empire]:回车
Organizational Unit Name (eg, section) []:回车
Common Name (eg, your name or your server's hostname) [Black-Xstar Net Empire CA]:回车
Name []:回车
Email Address [webmaster@black-xstar.com]:回车

注意红色的地方,需要按回车的。

创建CA之后来生成服务器证书,输入./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) [GZ]:回车
Organization Name (eg, company) [Black-Xstar Net Empire]:回车
Organizational Unit Name (eg, section) []:回车
Common Name (eg, your name or your server's hostname) [server]:回车
Name []:回车
Email Address [webmaster@black-xstar.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/2.0/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :P RINTABLE:'CN'
stateOrProvinceName :P RINTABLE:'GD'
localityName :P RINTABLE:'GZ'
organizationName :P RINTABLE:'Black-Xstar Net Empire'
commonName :P RINTABLE:'server'
emailAddress :IA5STRING:'webmaster@black-xstar.com'
Certificate is to be certified until Nov 18 17:25:15 2019 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

同样注意红色字的部分,是需要人工输入的。

服务器证书生成完了,我们来生成客户端证书,理论上每个OpenVPN用户都有独立的证书,我们先来生成一个试试。

输入命令:./build-key client1,这里的client1是客户端名称,如果第二个就是client2了。

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) [GZ]:回车
Organization Name (eg, company) [Black-Xstar Net Empire]:回车
Organizational Unit Name (eg, section) []:回车
Common Name (eg, your name or your server's hostname) [client1]:回车
Name []:回车
Email Address [webmaster@black-xstar.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/2.0/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :P RINTABLE:'CN'
stateOrProvinceName :P RINTABLE:'GD'
localityName :P RINTABLE:'GZ'
organizationName :P RINTABLE:'Black-Xstar Net Empire'
commonName :P RINTABLE:'client1'
emailAddress :IA5STRING:'webmaster@black-xstar.com'
Certificate is to be certified until Nov 18 17:31:21 2019 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

注意红色字的部分,和上面生成服务器证书差不多。

最后生成Diffie Hellman参数:./build-dh,这个需要一点时间的。

完成上面的过程后,把/etc/openvpn/2.0/keys里面的东西下载回来。

接下来我们开始配置OpenVPN了,我的配置文件只是一个参考,可以根据实际情况修改。

首先cd ..回到上一级目录,然后vi server.conf新建一个配置文件,输入下面内容:

port 443
proto tcp
dev tun
ca /etc/openvpn/easy-rsa/2.0/keys/ca.crt
cert /etc/openvpn/easy-rsa/2.0/keys/server.crt
key /etc/openvpn/easy-rsa/2.0/keys/server.key
dh /etc/openvpn/easy-rsa/2.0/keys/dh1024.pem
server 10.8.0.0 255.255.255.0
push "redirect-gateway def1"
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
client-to-client
keepalive 10 120
comp-lzo
persist-key
persist-tun
verb 3

其中DNS服务器地址可以换成主机商的,也可以和我一样用opendns。另外我有了tcp协议和443端口,是为了方便我在cmwap下使用,也 可以换别的。

到这里为止OpenVPN就配置好了,接下来我们设置外网访问。

输入vi /etc/sysctl.conf开始编辑,找到net.ipv4.ip_forward = 0改成net.ipv4.ip_forward = 1保存。然后执行sysctl -p这个命令。

输入iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source 1.2.3.4添加规则,注意最后1.2.3.4改成你的VPS的IP地址。

完成后用/etc/init.d/iptables save保存iptables设置,然后/etc/init.d/iptables restart重新启动下。

把OpenVPN添加到开机启动,用vi /etc/rc.local进入编辑,在后面加入/usr/sbin/openvpn --config /etc/openvpn/server.conf &这一行。

需要在服务器上完成的操作到这里就结束了,输入openvpn --config /etc/openvpn/server.conf &启动。

1 comment:

Bill Shi said...

Great to read your blog here:)

Bill