搭建局域网VPN,可以使用strongswan VPN,配置简单,便于维护。
只需要在每段有一台linux服务器即可。
wget https://download.strongswan.org/strongswan-5.4.0.tar.bz2
yum install gcc gmp gmp-devel
tar jxvf strongswan-5.4.0.tar.bz2
./configure --prefix=/usr/local/strongswan-5.4.0
make && make install
编辑/etc/sysctl.conf把
net.ipv4.ip_forward = 0
改成
net.ipv4.ip_forward = 1
如果此文件中没有这个选项则将其添加上就行。
然后执行命令:#sysctl -p
使其生效。
编辑/usr/local/strongswan-5.4.0/etc/ipsec.conf
config setup
uniqueids = never
charondebug="ike 4, knl 4, net 4, cfg 4"
conn %defualt
type=tunnel
ikelifetime=60m
keylife=5m
dpddelay=10s
rekeymargin=3m
keyingtries=3
mobike=no
#北京到天津IDC
#北京端的配置,天津端的配置和北京一样,只需要修改IP地址即可
conn site-to-site-TJ
keyexchange=ikev2
left=0.0.0.0
leftid=123.56.x.x (本端公网ip)
leftsubnet=172.x.0.0/16 (本段内网网段)
leftfirewall=no
right=60.x.x.x (对端公网IP)
rightid=60.x.x.x (对端公网IP))
rightsubnet=172.x.0.0/16 (对端内网网段)
authby=secret
ike=aes256-sha2_256-modp1024
esp=aes256-sha2_256
auto=start
#北京到腾讯云
conn site-to-site-tencent
keyexchange=ikev1
left=0.0.0.0
leftid=123.56.x.x
leftsubnet=172.16.0.0/16
leftfirewall=no
right=123.206.x.x
rightid=123.206.x.x
rightsubnet=172.18.x.0/23
authby=secret
ike=aes128-sha1-modp1024
esp=aes128-sha1
auto=start
ipsec.secrets配置
编辑/usr/local/strongswan-5.4.0/etc/ipsec.secrets
123.56.x.x 60.28.x.x : PSK "abcd" (格式:本端公网IP 对端公网IP :PSK "密钥")
123.56.x.x 123.206.x.x : PSK "abcd"
strongswan.conf配置
编辑/usr/local/strongswan-5.4.0/etc/strongswan.conf
charon {
interfaces_use = eth0 #是配置有公网IP的那个网卡名称
load_modular = yes
filelog {
/var/log/strongswan.charon.log {
time_format = %b %e %T
default = 1
append = yes
flush_line = yes
}
}
plugins {
include strongswan.d/charon/*.conf
}
}
include strongswan.d/*.conf
启动服务
/usr/local/strongswan-5.4.0/sbin/ipsec start
可以通过/usr/local/strongswan-5.4.0/sbin/ipsec status 查看VPN是否已经建立成功。
后面可以通过脚本来检测VPN的状态,检测VPN是否正常。如果发现VPN不通,可以通过脚本来重置VPN