OpenVPN 部署
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上。
接下来连接测试即可。
最新文章
- Java实现多线程断点下载(下载过程中可以暂停)
- 第二轮冲刺-Runner站立会议03
- 创建寄宿在Windows服务中的WCF服务
- uniq-删除重复
- 关于IIS中WEB网站访问弹“验证输入框”及“401限制访问”的解决办法
- linux 文件在磁盘上的表示
- 为什么要使用jQuery?
- BZOJ1997 [Hnoi2010]Planar (2-sat)
- CSS鼠标点击式变化图片透明度
- C#快速剔除字符串中不合法的文件名或者文件路径字符
- BZOJ 1901 Zju2112 Dynamic Rankings
- jQ小图标上下滑动特效
- 将DataTable中的数据一次性提交到数据库中
- Linux查看端口
- PS调出最美海滨城市俯拍照
- php中相对路径和绝对路径如何使用(详解)
- Spring Boot 官方文档学习(一)入门及使用
- Selenium2+python自动化-xpath定位语法
- (mark)ubuntu16.04下安装&;配置anaconda+tensorflow新手教程
- hdu 1166线段树 单点更新 区间求和
热门文章
- UVA-11367 Full Tank? (dijkstra)
- Ansible 小手册系列 九(Playbook)
- CF 916
- SQL Server(MSSQLSERVER)启动失败,提示“请求失败或服务未及时响应
- 编写configure.ac
- 异形Modbus客户端 和 异形modbus服务器之间的通讯 侦听模式的modbus-tcp客户端通讯
- 高并发异步uwsgi+web.py+gevent
- PHP循环嵌套例子
- 小程序开发之scroll-view中id不能以数字开头的问题
- js之数码时钟加随机变色