本文介绍另一种IPsec的实现方式,服务器之间直接搭建IPsec隧道,适用于两端网络设备不支持IPsec的场景。
前置准备
两端均采用CentOS,使用开源的strongSwan软件包来实现IPsec协议。在这次场景下两端我们使用的是默认开局后的CentOS MINI,即基本网络环境配置完毕、selinux关闭、ntp配置完成的状态。这里附一个自己常用的简单开局。
systemctl stop NetworkManager systemctl disable NetworkManager setenforce 0 sed -i '/SELINUX=enforcing/s/enforcing/permissive/g' /etc/selinux/config yum -y install ntp systemctl enable ntpd systemctl start ntpd sed -i "s/^.*0\.centos\.pool\.ntp\.org.*$/server 0\.cn\.pool\.ntp\.org/" /etc/ntp.conf sed -i "s/^.*1\.centos\.pool\.ntp\.org.*$/server 1\.cn\.pool\.ntp\.org/" /etc/ntp.conf sed -i "s/^.*2\.centos\.pool\.ntp\.org.*$/server 2\.cn\.pool\.ntp\.org/" /etc/ntp.conf sed -i "s/^.*3\.centos\.pool\.ntp\.org.*$/server 3\.cn\.pool\.ntp\.org/" /etc/ntp.conf systemctl restart ntpd
为配合IPsec协议的正常工作,这里我们还要在防火墙上放行Ipsec服务:
firewall-cmd --zone=dmz --add-service=ipsec --permanent
一般情况下,我们创建IPsec隧道不是为了单纯的互联,所以还需要开启ip转发,并根据具体应用场景,辅助以相应的路由策略。
开始搭建
安装strongSwan:
yum install strongswan
编辑strongSwan配置文件:
vim /etc/strongswan/ipsec.conf # 配置内容 conn woyun # 第一阶段IKE版本 keyexchange=ikev1 # IKE协商模式(no表示默认为主动模式) aggressive=no # 本地认证方式 leftauth=psk # 对端认证方式 rightauth=psk # 本地ipsec地址 left=129.227.66.125 # 对端ipsec地址 right=39.106.1.68 # 本地ipsec标识 leftid=129.227.66.125 # 对端ipsec标识 rightid=39.106.1.68 # ike提议 ike=aes128-sha1-modp1024 # ike存活时间 ikelifetime=8h # 是否启用自动连接 auto=start # 本端内网网段,即感兴趣流 leftsubnet=10.37.0.0/16 # 对端内网网段,即感兴趣流 rightsubnet=10.254.37.0/24 # 第二阶段的sep加密和认证算法 esp=aes128-sha1 # esp存活时间 keylife=8h
创建预共享密钥:
vim /etc/strongswan/ipsec.secrets 129.227.66.125 39.106.1.68 : PSK XXX@123
除去常规的配置,/etc/strongswan/ipsec.conf还有个别特殊参数备用:
# ikuai不支持dpd,和如果对端是设备是ikuai时,这里需要显式指定关闭它 dpdaction=none
在第一阶段,如果需要使用野蛮模式进行强制协商(在某些场合下野蛮模式能够提高成功率),还需要在strongSwan功能里配合开启:
vim /etc/strongswan/strongswan.conf charon { load_modular = yes # 允许野蛮模式 i_dont_care_about_security_and_use_aggressive_mode_psk = yes plugins { include strongswan.d/charon/*.conf } }
上述任何配置发生变更后都需要重载服务:
systemctl restart strongswan systemctl status strongswan
需要注意的是,strongswan会在本地创建名为220的策略路由,从而实现两端感兴趣流的引导。和两端都是SW的情况相同,IPsec在工作时,需要规避本地NAT的影响;假如本机防火墙开启了masquerade,默认情况下是会影响IPsec交互的。
评论列表,共 0 条评论
暂无评论