Freebsd+openvpn

本文是在freebsd6.2操作系统下

  

安装openvpn

root登陆服务器后

因为需要数据压缩,所以需要装LZO

[root@dns-master openvpn]# cd /usr/ports/archivers/lzo

[root@dns-master lzo]# pwd

/usr/ports/archivers/lzo

 Make install clean

安装openvpn

[root@dns-master ~]# cd /usr/ports/security/openvpn

[root@dns-master openvpn]# pwd

/usr/ports/security/openvpn

然后输入make install with-lzo-headers=/usr/local/include with-lzo-lib=/usr/local/lib  

二、配置openvpn.conf

可以去拷贝一个

cd /usr/ports/security/openvpn/work/openvpn-2.0.6/sample-config-files

[root@dns-master sample-config-files]# pwd

/usr/ports/security/openvpn/work/openvpn-2.0.6/sample-config-files

[root@dns-master sample-config-files]# cp server.conf /usr/local/etc/openvpn/openvpn

然后用vi编辑openvpn.conf

我的配置文件如下

 

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

# 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 ca.crtcert server.crt

key server.key  # This file should be kept secret

 

# 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 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.0.0.0 255.255.255.0 

# 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

# Push routes to the client to allow it

# to reach other private subnets behind

# the server.  Remember that these

# private subnets will also need

# to know to route the OpenVPN client

# address pool (10.8.0.0/255.255.255.0)

# back to the OpenVPN server.

push “route 172.16.10.0 255.255.255.0″

# To assign specific IP addresses to specific

# clients or if a connecting client has a private

# subnet behind it that should also have VPN access,

# use the subdirectory “ccd” for client-specific

# configuration files (see man page for more info).

# EXAMPLE: Suppose you want to give

# Thelonious a fixed VPN IP address of 10.9.0.1.

# First uncomment out these lines:

client-config-dir ccd 

# 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 

# 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 

# For extra security beyond that provided

# by SSL/TLS, create an “HMAC firewall”

# to help block DoS attacks and UDP port flooding.

#

 

# Enable compression on the VPN link.

# If you enable it here, you must also

# enable it in the client config file.

comp-lzo 

# It’s a good idea to reduce the OpenVPN

# daemon’s privileges after initialization.

#

# You can uncomment this out on

# non-Windows systems.

user nobodygroup nobody 

# The persist options will try to avoid

# accessing certain resources on restart

# that may no longer be accessible because

# of the privilege downgrade.

persist-keypersist-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

三、生成服务器端和客户端证书和密钥

首先切换到一下目录

[root@dns-mastereasy-rsa]#cd/usr/ports/security/openvpn/work/openvpn-2.0.6/easy-rsa

[root@dns-master easy-rsa]# pwd

/usr/ports/security/openvpn/work/openvpn-2.0.6/easy-rsa

然后编辑vars文件内容如下,根据自己实际情况修改

[root@dns-master easy-rsa]# more vars

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

echo 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.

export KEY_COUNTRY=CNexport KEY_PROVINCE=BeiJingexport KEY_CITY=BeiJingexport KEY_ORG=”Redpoo.com”export KEY_EMAIL=bosonmaster@gmail.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) [redpoo.com]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) []:redpoo.com //
这里随便输入即可
Email Address [bosonmaster@gmail.com]:
3.1vpn server 产生证书 ./build-key-server server
Common Name
处填server,其他默认,为上面编辑vars 文件时设置的值。“Sign the certificate?
[y/n]”
“1 out of 1 certificate requests certified, commit? [y/n]“两处选y3.2vpn client 产生证书 ./build-key bosonCommon Name 处分别填boson 其他同server如果产生多个证书的话,重复上边操作。3.3 产生生成Diffie Hellman 参数  ./build-dh
   
3.4配置现在生成了所需的文件。目录keys 下生成的文件中,ca.crt 是所有vpn 机器都需要的。vpn server 需要ca.crt ca.key dh1024.pem server.crt server.key,拷贝到vpn server /usr/local/etc/openvpn目录。vpn boson 需要ca.crt boson.crt boson.key 

四、修改客户端配置

以下是以windows为客户端为例配置文件如下;############################################### Sample client-side OpenVPN 2.0 config file ## for connecting to multi-client server.     ##                                            ## This configuration can be used by multiple ## clients, however each client should have   ## its own cert and key files.                ##                                            ## On Windows, you might want to rename this  ## file so it has a .ovpn extension           ############################################### # Specify that we are a client and that we# will be pulling certain config file directives# from the server.client # Use the same setting as you are using on# the server.# On most systems, the VPN will not function# unless you partially or fully disable# the firewall for the TUN/TAP interface.dev tun # Are we connecting to a TCP or# UDP server?  Use the same setting as# on the server.proto udp # The hostname/IP and port of the server.# You can have multiple remote entries# to load balance between the servers.Remote IP  1194 # 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 don’t need to bind to# a specific local port number.nobind # Downgrade privileges after initialization (non-Windows only)user nobodygroup nobody # Try to preserve some state across restarts.persist-keypersist-tun # SSL/TLS parms.# See the server config file for more# description.  It’s best to use# a separate .crt/.key file pair# for each client.  A single ca# file can be used for all clients.ca ca.crtcert  boson.crtkey  boson.key# Enable compression on the VPN link.# Don’t enable this unless it is also# enabled in the server config file.comp-lzo # Set log file verbosity.verb 3 # Silence repeating messages;mute 20keepalive 10 120

五、配置openvpn自动启动

Vi编辑/etc/rc.conf添加如下一行,保存退出Openvpn_enable=”yes”

六、启动openvpn

/usr/local/etc/rc.d/openvpn start停止openvpn/usr/local/etc/rc.d/openvpn stop可以通过以下命令确认启动[root@dns-master easy-rsa]# netstat -an |grep 1194udp4       0      0  *.1194            



1 条评论

  • At 2008.11.06 11:33, Arseny said:

    It is beautifully turned out .. I liked ..) would be continuously zahazhivat to you.

    (Required)
    (Required, will not be published)