一、环境介绍
操作系统centos7.4
openvpn版本:openvpn-2.1
lzo版本:lzo-2.03
二、搭建
关闭firewalld防火墙,并设置开机不启动。关闭selinux
systemctl stop firewalld systemctl disable firewalld setenforce 0
安装openvpn和必要的依赖包
yum install bridge-utils gcc gcc-c++ make openssl openssl-devel ntpdate* sh* pam pam-devel -y
编译安装lzo(数据加密和压缩功能)
#./configure #make #make install
编译安装openvpn(这里用的是openvpn-2.1_rc7.tar.gz)
#./configure #make #make install
需要将原来安装包中的easy-rsa 文件拷贝到openvpn安装目录下/etc/openvpn/
cp -R /root/openvpn-2.1_rc7/easy-rsa /etc/openvpn/
然后给安装目录下的2.0这个目录里面所有文件读写执行权限。
#cd /etc/openvpn/2.0/ #chmod +rwx *
在下面的文件的末端修改和添加以下内容
#vi vars export KEY_COUNTRY=”CN”#(国家) export KEY_PROVINCE=”BEIJING”#(省份) export KEY_CITY=” BEIJING”#(城市) export KEY_ORG=”gaosiedu”#(组织) export KEY_EMAIL=”[email protected]”#(邮件地址) export KEY_OU=”gaosi”#(单位)
执行以下命令,生成证书
#source ./vars #./clean-all 清楚openvpn所有证书相关的值 #./build-ca 生成CA信任的证书(Common Name:这里填入公司全拼) #./build-key-server server 生成服务器的证书和私钥 (Common Name:这里填入公司全拼) #./build-key client 生成客户端证书和私钥./build-key 后面加的是客户端名称(比如是某个地方或者某个人的名字) #./build-dh 创建Diffie-Hellman参数(防止恶意攻击,一种加密的散列消息验证码)
同步时间
# timedatectl set-timezone Asia/Shanghai #timedatectl set-ntp yes
mkdir /etc/openvpn/keys 在安装目录建立一个keys目录 cp /etc/openvpn/2.0/keys/* /etc/openvpn/keys/ 然后将前面生成的所有证书和秘钥文件拷贝到这里
复制之前做好的server.conf文件到openvpn安装目录,进行修改。 (解压的安装包里有所有的事例文件sample)
cp /root/server.conf /etc/openvpn/
修改配置文件
#vi /etc/openvpn/server.conf
配置文件修改内容
port 1195 openvpn默认端口号是1194,可以修改 proto tcp 使用TCP传输 dev tap0 这里使用tap0(还有tun三层路由模式) ca /etc/openvpn/keys/ca.crt CA信任证书位置 cert /etc/openvpn/keys/server.crt 服务器证书 key /etc/openvpn/keys/server.key # This file should be kept secret服务器秘钥 dh /etc/openvpn/keys/dh1024.pem 这里一般默认就OK, ifconfig 192.168.0.200 255.255.255.0 这里必须填写本机真实IP地址 ifconfig-pool-persist /etc/openvpn/ipp.txt 这里是客户端名称和对应的ip文件 这里前面必须的服务器真是的IP地址,后面的是openvpn的地址(一般情况下我们会设置跟真实的服务器一个ip地址段,前面是本机真实IP地址,后面是要分配给客户端的IP地址)。 server-bridge 192.168.32.100 255.255.255.0 192.168.32.200 192.168.32.205 push "route 192.168.0.0 255.255.255.0" 下发给客户端的路由 push "dhcp-option DNS 202.106.0.20" 下发给客户端的DNS client-to-client 允许拨入的openvpn客户端可以互相通信 keepalive 10 120 拨入的超时时间10-120秒 comp-lzo 启动网络传输压缩 max-clients 3 最大连接客户端数 user nobody openvpn服务自己的用户名 group nobody openvpn服务自己组 persist-key 默认OK persist-tun 默认OK status /etc/openvpn/openvpn-status.log 状态信息文件位置,自己生成 log /etc/openvpn/openvpn.log 日志文件,自己生成 log-append /etc/openvpn/openvpn.log verb 5
执行下面的命令
/usr/local/sbin/openvpn --daemon openvpn --config /etc/openvpn/server.conf --dev-typetap
将压缩包里面的这三个文件复制到/etc/init.d/,给执行权限,如果路径不对,需要更改里面的路径,
首次启动openvpn的时候需首先启动bridge-start,然后再启动openvpn
bridge-stop
bridge-start
openvpn
下面这三个文件需要放置/etc/init.d/下面,给755权限
#!/bin/bash ################################# # Set up Ethernet bridge on Linux # Requires: bridge-utils ################################# # Define Bridge Interface br="br0" # Define list of TAP interfaces to be bridged, # for example tap="tap0 tap1 tap2". tap="tap0" # Define physical ethernet interface to be bridged # with TAP interface(s) above. eth="ens33" eth_ip="172.16.5.238" eth_netmask="255.255.255.0" eth_broadcast="172.16.5.1" for t in $tap; do /usr/local/sbin/openvpn --mktun --dev $t done brctl addbr $br brctl addif $br $eth for t in $tap; do brctl addif $br $t done for t in $tap; do ifconfig $t 0.0.0.0 promisc up done ifconfig $eth 0.0.0.0 promisc up ifconfig $br $eth_ip netmask $eth_netmask broadcast $eth_broadcast ############################################################################################
#!/bin/bash #################################### # Tear Down Ethernet bridge on Linux #################################### # Define Bridge Interface br="br0" # Define list of TAP interfaces to be bridged together tap="tap0" ifconfig $br down brctl delbr $br for t in $tap; do /usr/local/sbin/openvpn --rmtun --dev $t done
openvpn_locations="/usr/sbin/openvpn /usr/local/sbin/openvpn" for location in $openvpn_locations do if [ -f "$location" ] then openvpn=$location fi done # Lockfile lock="/var/lock/subsys/openvpn" # PID directory piddir="/var/run/openvpn" # Our working directory work=/etc/openvpn # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. if [ ${NETWORKING} = "no" ] then echo "Networking is down" exit 0 fi # Check that binary exists if ! [ -f $openvpn ] then echo "openvpn binary not found" exit 0 fi # See how we were called. case "$1" in start) echo -n $"Starting openvpn: " /sbin/modprobe tun >/dev/null 2>&1 # From a security perspective, I think it makes # sense to remove this, and have users who need # it explictly enable in their --up scripts or # firewall setups. #echo 1 > /proc/sys/net/ipv4/ip_forward