openwrt+openvpn配置


Server配置端:

一、安装openvpn

Yum install –y lzo

Yum install –y openvpn

Wget https://github.com/OpenVPN/easy-rsa

1unzipeasy-rsa-master.zip

2cp easy-rsa-master/etc/openvpn/easy-rsa

3cd/etc/openvpn/easy-rsa/easyrsa3/

4cp vars.example vars

5vi vars 修改配置文件

export EASYRSA_REQ_COUNTRY="CN"

export EASYRSA_REQ_PROVINCE="SH"

export EASYRSA_REQ_CITY="SH"

export EASYRSA_REQ_ORG="LINGYU"

exportEASYRSA_REQ_EMAIL="[email protected]"

export EASYRSA_REQ_OU="LINGYUOPENVPN"

6cd/etc/openvpn/easy-rsa/easyrsa3/执行. ./vars

二、创建服务端证书及key

(1).初始化: ./easyrsa init-pki

技术分享

 

(2).创建根证书:  ./easyrsabuild-ca

技术分享

 

(3).创建服务器端证书: ./easyrsa gen-req server nopass

技术分享

 

(4).签约服务器端证书: ./easyrsasign server server

技术分享

 

(5).创建Diffie-Hellman,确保key穿越不安全网络的命令:./easyrsa gen-dh

技术分享

 

五、创建客户端证书

1、在/etc/下创建openvpn_client目录

mkdir -p/etc/openvpnClient

cd /etc/openvpnClient/

unzipeasy-rsa-master.zip

mv easy-rsa-mastereasy-rsa

cd  easy-rsa/easyrsa3

2、初始化:./easyrsainit-pki

技术分享

 

3、创建客户端key及生成证书:  ./easyrsa gen-reqlingyu    ###lingyu为创建根证书是输入的Common Name

技术分享

 

4、将得到的lingyu.req导入并签约证书

cd/etcopenvpn/easy-rsa/easyrsa3

导入req:   ./easyrsaimport-req /etc/openvpnClient/easy-rsa/easyrsa3/pki/reqs/lingyu.reqlingyu

技术分享

 

5、签约证书: ./easyrsa signclient lingyu

技术分享

六、检查服务端及客户端生成的文件

1、服务端:

/etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt

/etc/openvpn/easy-rsa/easyrsa3/pki/dh.pem

/etc/openvpn/easy-rsa/easyrsa3/pki/reqs/LINGYU.req

/etc/openvpn/easy-rsa/easyrsa3/pki/reqs/server.req

/etc/openvpn/easy-rsa/easyrsa3/pki/private/ca.key

/etc/openvpn/easy-rsa/easyrsa3/pki/private/server.key

/etc/openvpn/easy-rsa/easyrsa3/pki/issued/lingyu.crt

/etc/openvpn/easy-rsa/easyrsa3/pki/issued/server.crt

2、客户端:

/etc/openvpnClient/easy-rsa/easyrsa3/pki/private/LINGYU.key

/etc/openvpnClient/easy-rsa/easyrsa3/pki/reqs/LINGYU.req   ###这个文件被我们导入到了服务端文件,所以那里也有

 

七、拷贝文件

1、拷贝服务器密钥及证书等到openvpn目录

Cp /etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt/etc/openvpn/

cp /etc/openvpn/easy-rsa/easyrsa3/pki/private/server.key/etc/openvpn/

cp/etc/openvpn/easy-rsa/easyrsa3/pki/issued/server.crt/etc/openvpn/

cp/etc/openvpn/easy-rsa/easyrsa3/pki/dh.pem/etcopenvpn/

 

2、拷贝客户端密钥及证书等到client目录

cp /etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt/etc/openvpn_client/

cp/etc/openvpn/easy-rsa/easyrsa3/pki/issued/lingyu.crt/etc/openvpn_client/

cp/etc/openvpnClient/easy-rsa/easyrsa3/pki/private/lingyu.key/etc/openvpn_client/  ##注意是拷贝客户端目录里的文件

 

八、为服务端编写配置文件

1、cd/etcopenvpn/

2、mkdir conf

3、mkdir log

4、cp /usr/share/doc/openvpn-2.3.6/sample/sample-config-files/server.conf/etc/openvpn/conf/server.conf

vi server.conf

 

# 设置监听IP,默认是监听所有IP

;local a.b.c.d

 

# 设置监听端口,必须要对应的在防火墙里面打开

port 1194

 

# 设置用TCP还是UDP协议?

;proto tcp

proto tcp

 

# 设置创建tun的路由IP通道,还是创建tap的以太网通道路由IP容易控制,所以推荐使用它;

# 但如果如IPX等必须使用第二层才能通过的通讯,则可以用tap方式,tap也就是以太网桥接

;dev tap

dev tun

 

# 这里是重点,必须指定SSL/TLS rootcertificate (ca),

# certificate(cert), and private key (key)

# ca文件是服务端和客户端都必须使用的,但不需要ca.key

# 服务端和客户端指定各自的.crt和.key

# 请注意路径,可以使用以配置文件开始为根的相对路径,

# 也可以使用绝对路径

# 请小心存放.key密钥文件

ca /etc/openvpn/easy-rsa/easy-rsa/2.0/keys/ca.crt

cert /etc/openvpn/easy-rsa/easy-rsa/2.0/keys/openvpn.example.com.crt

key /etc/openvpn/easy-rsa/easy-rsa/2.0/keys/openvpn.example.com.key # Thisfile should be kept secret

 

# 指定Diffie hellmanparameters.

dh /etc/openvpn/easy-rsa/easy-rsa/2.0/keys/dh2048.pem

 

# 配置VPN使用的网段,OpenVPN会自动提供基于该网段的DHCP服务,但不能和任何一方的局域网段重复,保证唯一

server 10.8.0.0 255.255.255.0

 

# 维持一个客户端和virtual IP的对应表,以方便客户端重新连接可以获得同样的IP

ifconfig-pool-persist ipp.txt

 

# 为客户端创建对应的路由,以另其通达公司网内部服务器

# 但记住,公司网内部服务器也需要有可用路由返回到客户端

;push "route 192.168.20.0 255.255.255.0"

push "route 10.6.0.0 255.255.0.0"

 

# 若客户端希望所有的流量都通过VPN传输,则可以使用该语句

# 其会自动改变客户端的网关为VPN服务器,推荐关闭

# 一旦设置,请小心服务端的DHCP设置问题

;push "redirect-gateway"

 

# 用OpenVPN的DHCP功能为客户端提供指定的DNS、WINS等

;push "dhcp-option DNS 10.8.0.1"

;push "dhcp-option WINS 10.8.0.1"

 

# 默认客户端之间是不能直接通讯的,除非把下面的语句注释掉

client-to-client

 

# 下面是一些对安全性增强的措施

# For extra security beyond that provided by SSL/TLS, create an "HMACfirewall"

# to help block DoS attacks and UDP port flooding.

#

# Generate with:

# openvpn --genkey --secret ta.key

#

# The server and each client must have a copy of this key.

# The second parameter should be 0 on the server and 1 on the clients.

tls-auth ta.key 0 # This file is secret

 

# 使用lzo压缩的通讯,服务端和客户端都必须配置

comp-lzo

 

# 输出短日志,每分钟刷新一次,以显示当前的客户端

status  /etc/openvpn/log/openvpn-status.log

 

# 缺省日志会记录在系统日志中,但也可以导向到其他地方

# 建议调试的使用先不要设置,调试完成后再定义

log         /etc/openvpn/log/openvpn.log

log-append   /etc/openvpn/log/openvpn.log

 

# 设置日志的级别

#

# 0 is silent, except for fatal errors

# 4 is reasonable for general usage

# 5 and 6 can help to debug connection problems

# 9 is extremely verbose

verb 3

启动服务

# service openvpn start

开机启动

chkconfig openvpn on

找到net.ipv4.ip_forward = 0

0改成1

sysctl -p

设置iptables(这一条至关重要,通过配置natvpn网段IP转发到server内网)

# iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

设置openvpn端口通过:

# iptables -A INPUT -p TCP --dport 1194 -j ACCEPT

# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

重启iptables:

注:这里提一下,INPUT策略是执行后即时生效的,POSTROUTING需要保存并重启服务才能生效

# service iptables save

# service iptables restart

配置iptables开机启动

# chkconfig iptables on

 

 

 

 

Client端配置

Openwrt默认是没有网络的得手动设置为静态IP

配置ip地址

Vi /etc/config/network

 

 

config interface ‘loopback‘

         optionifname ‘lo‘

         optionproto ‘static‘

         optionipaddr ‘127.0.0.1‘

         optionnetmask ‘255.0.0.0‘

 

config interface ‘lan‘

         optionifname ‘eth0‘

         optiontype ‘bridge‘

         optionproto ‘static‘

         optionipaddr ‘192.168.192.215‘

         optionnetmask ‘255.255.255.0‘

   option gateway ‘192.168.192.1‘

   option dns ‘8.8.8.8‘

         optionip6assign ‘60‘

 

config interface ‘wan6‘

         optionifname ‘@wan‘

         optionproto ‘dhcpv6‘

 

config globals ‘globals‘

         optionula_prefix ‘fd8e:ad26:8d12::/48‘

 

可以上网之后

安装配置openvpn

你需要先更新下opkg的缓存:

Opkg update

Opkg install  openvpn-openssl

将服务端生成的client端认证文件导入/etc/openvpnClient/目录下,

三个文件分别是:

/etc/openvpnClient/easy-rsa/easyrsa3/pki/private/LINGYU.key

/etc/openvpnClient/easy-rsa/easyrsa3/pki/reqs/LINGYU.req 

/etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt

将服务端的client配置文件(server端服务器中/usr/share/doc/openvpn-2.3.6/sample/sample-config-files/client.conf)导入到客户端服务器中,

本次导入到客户端/etc/openvpnClient/目录中

修改配置文件

Vi /etc/openvpnClient/client.conf

 

client                                         

dev tun         #网上配置是tcp,但是配置为TCP会报错,改成tun就成功                           

proto udp                                      

remote xxx.xxx.xxx.xxx 1194     #serverIP                   

resolv-retry infinite                           

nobind                                           

persist-key                                                  

persist-tun                                                  

ca /etc/openvpnClient/ca.crt              #秘钥的绝对路径                   

cert /etc/openvpnClient/lingyu.crt          #秘钥的绝对路径                  

key /etc/openvpnClient/lingyu.key           #秘钥的绝对路径                  

remote-cert-tls server                                       

comp-lzo                                                     

verb 0   #0是不打开日志,由于openwrt系统文件目录较小,日志写满空间后就会不连接。                                      

 

在服务器上重启openvpn服务:

/usr/sbin/openvpn --cd /etc/openvpnClient/--config client.conf

 

验证成功拨号:

技术分享

验证路由成功:

技术分享

 

 

 

配置路由转发

iptables -I FORWARD -obr-lan -j ACCEPT #允许br-lan端口流量被转发
iptables -I FORWARD -o tun0 -j ACCEPT #允许tun0端口流量被转发
iptables -t nat -I POSTROUTING -o tun0 -j MASQUERADE #tun0出口的流量SNAT出去

 

 


本文出自 “william” 博客,请务必保留此出处http://williamkangle.blog.51cto.com/9319898/1598428

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。