https://blog.frognew.com/2017/09/installing-openvpn.html#21-%E5%AE%89%E8%A3%85%E4%BE%9D%E8%B5%96

为了方便团队成员从异地访问开发环境,考虑使用OpenVPN搭建虚拟局域网。部署的环境和版本信息如下:

  • CentOS 7
  • OpenVPN

1.easy-rsa生成证书

这里下载easy-rsa。

unzip easy-rsa-old-master.zip
cd easy-rsa-old-master/easy-rsa/2.0

ls
build-ca        build-key-pkcs12  inherit-inter      pkitool
build-dh        build-key-server  list-crl           revoke-full
build-inter     build-req         openssl-0.9.6.cnf  sign-req
build-key       build-req-pass    openssl-0.9.8.cnf  vars
build-key-pass  clean-all         openssl-1.0.0.cnf  whichopensslcnf

ln -s openssl-1.0.0.cnf openssl.cnf

可修改vars文件中定义的变量用于生成证书的基本信息。下面生成CA证书:

source vars
./clean-all
./build-ca

因为已经在var中填写了证书的基本信息,所以一路回车即可。生成证书如下:

ls keys/
ca.crt  ca.key  index.txt  serial

生成服务器端秘钥:

./build-key-server server
......
Common Name (eg, your name or your server's hostname) [server]:
A challenge password []:1234
......

ls keys
01.pem  ca.crt  ca.key  index.txt  index.txt.attr  index.txt.old  serial  serial.old  server.crt  server.csr  server.key

生成客户端证书:

./build-key client
......
Common Name (eg, your name or your server's hostname) [client]:
A challenge password []:1234
......

Common Name用于区分客户端,不同的客户端应该有不同的名称。

Generating DH parameters:

./build-dh

ls keys/
01.pem  02.pem  ca.crt  ca.key  client.crt  client.csr  client.key  dh2048.pem  index.txt  index.txt.attr  index.txt.attr.old  index.txt.old  serial  serial.old  server.crt  server.csr  server.key

2.编译OpenVPN

2.1 安装依赖

pam-devel:

yum install -y pam-devel

lzo:

wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.10.tar.gz
tar -zxvf lzo-2.10.tar.gz
cd lzo-2.10
./configure --enable-shared
make
make install

2.2 编译安装OpenVPN

下载OpenVPN源码:

编译安装OpenVPN:

tar -zxvf openvpn-2.4.4.tar.gz
cd openvpn-2.4.4
./configure --prefix=/usr/local/openvpn
make
make install

3.配置OpenVPN

创建配置文件目录和证书目录:

mkdir -p /etc/openvpn
mkdir -p /etc/openvpn/pki

生成tls-auth key并将其拷贝到证书目录中:

/us/local/openvpn/sbin/openvpn --genkey --secret ta.key
mv ta.key /etc/openvpn/pki

将签名生成的CA证书秘钥和服务端证书秘钥拷贝到证书目录中:

cp ca.key ca.crt server.crt server.key dh2048.pem /etc/openvpn/pki/

ls /etc/openvpn/pki/
ca.crt  ca.key  dh2048.pem  server.crt  server.key

将OpenVPN源码下的配置文件sample/sample-config-files/server.conf拷贝到/etc/openvpn目录。

编辑服务端配置文件/etc/openvpn/server.conf:

local 192.168.1.2 # 服务端IP
port 1194

proto tcp
dev tun

ca /etc/openvpn/pki/ca.crt
cert /etc/openvpn/pki/server.crt
key /etc/openvpn/pki/server.key
dh /etc/openvpn/pki/dh2048.pem

server 10.8.0.0 255.255.255.0 # 分配给客户端的虚拟局域网段
ifconfig-pool-persist ipp.txt

# 推送路由和DNS到客户端
push "route 192.168.1.0 255.255.255.0"
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 192.168.1.1"
push "dhcp-option DNS 8.8.8.8"

client-to-client

keepalive 10 120

tls-auth /etc/openvpn/pki/ta.key 0

cipher AES-256-CBC

comp-lzo

max-clients 10

user nobody
group nobody

persist-key
persist-tun

status /var/log/openvpn-status.log
log  /var/log/openvpn.log
log-append  /var/log/openvpn.log

verb 3

确认内核已经开启路由转发功能:

sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1

确认iptables filter表的FOWARD链是ACCEPT状态:

iptables -nvL

iptables -P FORWARD ACCEPT

添加iptables转发规则,对所有源地址(openvpn为客户端分配的地址)为10.8.0.0/24的数据包转发后进行源地址转换,伪装成openvpn服务器内网地址192.168.1.2, 这样VPN客户端就可以访问服务器内网的其他机器了。

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o em1 -j SNAT --to-source 192.168.1.2

创建openvpn的systemd unit文件:

cat > /etc/systemd/system/openvpn.service <<EOF
[Unit]
Description=openvpn
After=network.target

[Service]
EnvironmentFile=-/etc/openvpn/openvpn
ExecStart=/usr/local/openvpn/sbin/openvpn \
       --config /etc/openvpn/server.conf
Restart=on-failure
Type=simple
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

启动并设置为开机启动:

systemctl start openvpn
systemctl enable openvpn

查看端口监听:

netstat -nltp | grep 1194
tcp        0      0 192.168.1.2:1194        0.0.0.0:*                           88462/openvpn

4.客户端连接测试

这里下载OPENVPN的windows客户端,安装完成后。 将以下证书和秘钥文件拷贝到安装目录中C:\Program Files\OpenVPN\config:

ca.crt
client.crt
client.key
ta.key

在这个目录下创建客户端的配置文件client.ovpn:

client
dev tun
proto tcp
remote xxx.xxx.xxx.xxx 11194
resolv-retry infinite
nobind
persist-key
persist-tun

ca ca.crt
cert client.crt
key client.key
remote-cert-tls server
tls-auth ta.key 1
cipher AES-256-CBC

comp-lzo
verb 3
  • 其中 xxx.xxx.xxx.xxx 11194是外网IP和端口映射到了内网服务器的192.168.1.2 1194上。

接下来连接测试即可。

OpenVPN 部署的更多相关文章

  1. 基于daridus认证的openvpn部署

    基于daridus认证的openvpn部署 安装openvpn 1.安装openvpn依赖包 #yum -y install gcc gcc-c++ #yum -y install openssl o ...

  2. OpenVPN部署,实现访问云服务器的内网

    本教程不描述如何FQ 一.OpenVPN服务端部署 $ yum -y install net-tools lzo lzo-devel openssl-devel pam-devel gcc gcc-c ...

  3. openvpn部署

    原文发表于cu:2016-03-29 参考文档: 安装:http://qicheng0211.blog.51cto.com/3958621/1575273 安装:http://www.ipython. ...

  4. openvpn部署之快速入门实战+一键部署openvpn脚本

    个人原创禁止转载 软件环境: Centos6.9 x64 openvpn-2.4.3-1.el6.x86_64.rpm easy-rsa-2.2.2-1.el6.noarch.rpm    #推荐使用 ...

  5. openvpn部署账号密码登录

    1.开启服务器端路由转发功能: 修改配置文件/etc/sysctl.conf中 net.ipv4.ip_forward = 0 改为 net.ipv4.ip_forward = 1 [root@nod ...

  6. openvpn部署centos7

    [root@openvpn ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 安装包 yum upgrade yum i ...

  7. OpenVpn部署使用easy-rsa-3.0

    OpenVpn简介 OpenVPN是一个用于创建虚拟专用网络(Virtual Private Network加密通道的免费开源软件.使用OpenVPN可以方便地在家庭.办公场所.住宿酒店等不同网络访问 ...

  8. Centos7.2下OpenVPN 环境完整部署记录

    关于OpenVPN的有关介绍及为何使用OpenVPN在此就不做赘述了,下面直接记录Centos7.2系统下部署OpenVPN环境的操作过程: 1) 先将本机的yum换成阿里云的yum源 [root@t ...

  9. openvpn路由配置

    openvpn路由配置 通常openvpn部署好以后,客户端连接VPN后会被配置一些路由,其客户端的路由会被修改为所有的流量都通过VPN来传输.但有时候,我们需要客户端的某些IP走VPN或者本地网关. ...

随机推荐

  1. (3) iOS开发之UI处理-UIView篇

    在UIView作为许多子控件的容器的时候,首先我们需要动态的计算出UIView下的所有子控件的高度,并布局排列好,然后我们还要把作为容器的UIView的高度调整到刚好包裹着所有子控件,不会过矮,也不会 ...

  2. day37 爬虫2(web微信、高性能相关、Scrapy)

    s16day37 爬虫2 参考博客:http://www.cnblogs.com/wupeiqi/articles/6229292.html 课堂代码:https://github.com/liyon ...

  3. 73条日常Linux shell命令汇总

    1.检查远程端口是否对bash开放: echo >/dev/tcp/8.8.8.8/53 && echo "open" 2.让进程转入后台: Ctrl + z ...

  4. javascript打开新窗体

    open -- 打开(弹出)一个新的窗体 open,中文"打开"的意思 引用网址:http://www.dreamdu.com/javascript/window.open Jav ...

  5. I/O流+统计文件词频

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  6. 将VIM打造成强大的IDE

    转载自:所需即所获:像 IDE 一样使用 vim 如侵犯您的版权,请联系:2378264731@qq.com --------------------------------------------- ...

  7. FusionCharts制作实时刷新图

    转自:http://yklovejava-163-com.iteye.com/blog/1889949 下面介绍的是用FusionCharts制作实时刷新图的过程(FusionCharts确实太好用了 ...

  8. UNIX发展史(BSD,GNU,linux)(转)

    转自 UNIX发展史(BSD,GNU,linux) 这篇文章写的非常好,在这里转一下. 先前的一個理想 UNIX 系统自 1969 年 Ken ThompsonKen Thompson 与 Denni ...

  9. apache 支持.htaccess重写url

    1. httpd.conf 添加: <Directory /> Options +Indexes +FollowSymLinks +Multiviews AllowOverride all ...

  10. Js/Jquery获取网页屏幕可见区域高度

    获取浏览器窗口的可视区域高度和宽度,滚动条高度有需要的朋友可参考一下. 1 document.body.clientWidth ==> BODY对象宽度 2 document.body.clie ...