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. BZOJ 1003 [ZJOI2006]物流运输trans ★(Dijkstra + DP)

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=1003 思路 先Dijkstra暴力求出i..j天内不变换路线的最少花费,然后dp[i] = ...

  2. firefox与ie 的javascript区别

    1. Document.form.item 问题     现有问题: 现有代码中存在许多 document.formName.item("itemName") 这样的语句,不能在 ...

  3. hdu3829

    题解: 对于每一个孩子裂点+建边 如果i孩子讨厌的和j孩子喜欢的相同,那么建边 然后跑最大独立集 代码: #include<cstdio> #include<cstring> ...

  4. Annotation方式实现AOP

    1.添加其他jar包 2.配置applicationContext.xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?xml version=&quo ...

  5. MySQL学习(三): 初识数据表

    打开数据库: USE db_name : 打开数据库. 创建数据表: 查看数据表: 查看数据表结构: 数据简单的插入与查找: 插入:INSERT [INTO] tbl_name [(col_name) ...

  6. 2018.11.14 Chopin’s

    The lineaments of Chopin’s肖邦 short, dramatic life are familiar to most classical-music enthusiasts. ...

  7. ss-libev 源码解析udp篇 (4)

    本篇分析remote_recv_cb,这是整个udp转发的反方向,即读取从后端发送过来的数据再发送给前端.对于ss-server,读取到的数据是目标地址的udp服务器发送回来的响应数据,ss-serv ...

  8. clean-css 安装 使用

    https://github.com/jakubpawlowicz/clean-css-cli https://davidwalsh.name/clean-css

  9. 提问:错误提示--命名空间“System.Web”中不存在类型或命名空间名称“UI”(是缺少程序集引用吗?) 如何解决??

    转自:http://topic.csdn.net/u/20080723/21/7e1e06cb-d013-4675-ba9b-9b39d49d0a0e.html 1) 添加对System.Web.dl ...

  10. Goroutine是如何工作的?

    翻译原文链接 转帖/转载请注明出处英文原文链接 发表于2014/02/24 Go语言 如果你刚刚接触Go语言,或者说你并不理解“并发不等于并行”这句话的含义,那么Rob Pike的讲座值得一看(在yo ...