openvpn service安装与配置
1.下载脚本wget https://git.io/vpn -O openvpn-install.sh
1 2 3 4
| chmod +x openvpn-install.sh
wget https://git.io/vpn -O openvpn-install.sh && bash openvpn-install.sh
|
2.运行脚本./openvpn-install.sh
,设置如下
- 监听地址设置为空 IP address:
- Protocol:[2]TCP
- Port:1194
- 不选DNS:
- client name: client_k2
- External IP : 112.74.51.136
3. 配置服务端vim /etc/openvpn/server.conf
1 2 3 4 5 6 7 8 9 10 11 12 13
| ;ifconfig-pool-persist ipp.txt ;push "redirect-gateway def1 bypass-dhcp"
push "route 10.14.0.0 255.255.255.0"
push "route 192.168.123.0 255.255.255.0"
route 192.168.123.0 255.255.255.0 10.14.0.2
client-config-dir ccd
client-to-client
|
4. 配置客户端路由mkdir /etc/openvpn/ccd
和vim /etc/openvpn/ccd/client_k2
1 2 3 4 5
| ifconfig-push 10.14.0.2 255.255.255.0
iroute 192.168.123.0 255.255.255.0 route 192.168.123.0 255.255.255.0
|
5.添加客户端./openvpn-install.sh
- Select an option[1-4]:1 (add a new user)
- client name: client_worker
1 2 3 4 5 6 7
| vim /etc/openvpn/server.conf
systemctl restart openvpn@server.service systemctl enable openvpn@server.service
|
6.下载ovpn文件,并修改配置,注释调#setenv opt block-outside-dns
7.常用命令
1 2 3 4 5 6
| systemctl restart openvpn@server.service
systemctl enable openvpn@server.service
scp root@112.74.51.136:/root/client_xuan_ubuntu.ovpn ./
|
openvpn client 安装与配置
1.安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| yum update yum install vim yum install epel-release yum clean all yum update yum makecache yum install openvpn iptables-services scp root@112.74.51.136:~/client_vm.ovpn /etc/openvpn/client/
openvpn --daemon --cd /etc/openvpn/client --config client_vm.ovpn --log-append /etc/openvpn/openvpn.log tail -100f /etc/openvpn/openvpn.log ps -ef | grep openvpn kill <pid>
vim /lib/systemd/system/openvpn-client\@.service
ExecStart=/usr/sbin/openvpn --suppress-timestamps --nobind --config %i.conf
ExecStart=/usr/sbin/openvpn --daemon --config %i.ovpn --log-append /etc/openvpn/openvpn.log
systemctl restart openvpn-client@client_vm
systemctl enable openvpn-client@client_vm
|
openvpn 服务端的局域网远程访问
准备工作,安装iptables
It is possible to go back to a more classic iptables setup. First, stop and mask the firewalld service:
1 2
| systemctl stop firewalld systemctl mask firewalld
|
Then, install the iptables-services package:
1
| yum install iptables-services
|
Enable the service at boot-time:
1
| systemctl enable iptables
|
Managing the service
1
| systemctl [stop|start|restart] iptables
|
Saving your firewall rules can be done as follows:
添加路由
1 2
| iptables -t nat -A POSTROUTING -s 10.14.208.0/24 -j SNAT --to-source 192.168.1.230 service iptables save
|
vim /etc/openvpn/server.conf
添加
1
| push "route 192.168.1.0 255.255.255.0"
|
腾讯云openvpn服务器所在内网供openvpn客户端访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| sudo iptables -t nat -A POSTROUTING -s 10.34.0.0/24 -o eth0 -j MASQUERADE
sudo iptables -nL -t nat Chain POSTROUTING (policy ACCEPT) target prot opt source destination MASQUERADE all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match src-type LOCAL MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0 MASQUERADE all -- 172.18.0.0/16 0.0.0.0/0 MASQUERADE tcp -- 172.18.0.11 172.18.0.11 tcp dpt:3306 MASQUERADE all -- 10.34.0.0 0.0.0.0/0
MASQUERADE all -- 10.34.0.0/24 0.0.0.0/0
vim /etc/sysctl.conf >net.ipv4.ip_forward = 1 sysctl -p
|
子网访问总结
服务端子网(服务端是网关)
服务端子网(服务端不是网关)
客户端子网(客户端是网关)
客户端子网(客户端不是网关)
不是网关统一需要在客户端或服务端设置转发
1 2 3 4 5 6
| # 10.34.0.0为openvpn网段 sudo iptables -t nat -A POSTROUTING -s 10.34.0.0/24 -o eth0 -j MASQUERADE #还需要检查是否开启ipv4转发 vim /etc/sysctl.conf >net.ipv4.ip_forward = 1 sysctl -p
|
注意事项
iptables不需要启动,也可以做路由转发
openvpn tun模式下客户端与内网机器通信
iptables规则的查看和清除
iptables 添加,删除,查看,修改
How can i use iptables on centos 7?
使用openvpn实现访问远程网络
参考
官网
脚本github官网Nyr/openvpn-install
openvpn的一个一键安装脚本“openvpn-install”让openvpn重放光彩,又可用openvpn翻墙了
How to Configure OpenVPN Server on CentOS 7.3
使用 OpenVPN 互联多地机房及Dokcer跨主机/机房通讯
扩大OpenVPN使用范围,包含服务器或客户端子网中的其他计算机