手把手教你搭建自己的 VPS 服务器
总有一些时候,你想要一台自己的 VPS 。本文分享了作者在实践过程中的一些经验,可以给那些自己搭建 VPS 的朋友一点帮助。
前期准备
需要购买一台拥有 root 权限的 VPS ,当时购买的是 512 M 内存 5 G SSD,500 G 流量/月, 9.99 刀每年,但是好像现在这种低价套餐已经结束了。有意的朋友可以看一下其他的套餐或者别的公司的 VPS。有的朋友说有些的速度非常快,看YouTube直接 1440p,但是我还没测试过,目前我的VPS速度能满足我的需求,而且 DO 的价格比较昂贵。
下图是搭了 SS 和 IPsec VPN 服务的使用情况,仅供参考:
使用情况
服务器购买后,安装 CentOS7,因为以下教程都是基于 CentOS7 的,安装新的 OS 后,搬瓦工会告诉你 SSH 的端口和 root 的密码,这些是自己无法自定义的,要记住了如果实在忘了也可以重置 root 密码,或者直接使用搬瓦工提供的在线SSH登录来操作也可,就是反应比较慢,所以我们以后还是常用 ssh 登录来配置 VPS ,Mac 下直接使用终端就好,win 下自行寻找一个 ssh 工具就好。
登录 ssh 的命令:
<span class="pln">$ </span><span class="kwd">ssh</span><span class="pun">-</span><span class="pln">p vps </span><span class="pun">端口号</span><span class="pln"> root@vpsIP </span><span class="pun">地址</span>
登录上以后就相当于在本地操作一样了,你可以使用各种 Linux 命令来操作了。
配置防火墙
如果 SSH 无法登录,那说明防火墙关闭了 SSH 端口,需要通过在线 SSH 登录进去关闭防火墙重新配置。
清除防火墙配置
<span class="pln">$ iptables </span><span class="pun">-</span><span class="pln">F</span>
清除 iptabels 所有表项,同时 nat 设置也没了,但是我们后续的脚本里会配置的,不用担心。如果 SSH 登录正常就不用管防火墙。
安装 firewalld
<span class="pln">$ </span><span class="kwd">yum</span><span class="pln"> install firewalld firewall</span><span class="pun">-</span><span class="pln">config</span>
<span class="pln">$ </span><span class="kwd">systemctl</span><span class="pln"> start firewalld</span>
P.S. 我在安装完 firewalld 之后然后启动服务的时候一直显示失败,然后重启了一遍服务器就可以正常的启动 firewalld 服务了,有类似情况的朋友可以重启一下服务器。
修改 SSH 端口
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">firewalld</span><span class="pun">/</span><span class="pln">services</span><span class="pun">/</span><span class="kwd">ssh</span><span class="pun">.</span><span class="pln">xml</span>
会出现以下的内容:
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="tag"><service></span>
<span class="tag"><short></span><span class="pln">SSH</span><span class="tag"></short></span>
<span class="tag"><description></span><span class="pln">Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</span><span class="tag"></description></span>
<span class="tag"><port</span><span class="atn">protocol</span><span class="pun">=</span><span class="atv">"tcp"</span><span class="atn">port</span><span class="pun">=</span><span class="atv">"22"</span><span class="tag">/></span>
<span class="tag"></service></span>
将 port=”22”,修改成搬瓦工提供给你的端口号,然后重载 firewalld 就 OK。
vi 的命令: 按 “i
” 是编辑模式,编辑后按 “esc
” 退出编辑模式,然后按 Shift
输入“:
” 和 “wq
” 保存退出 vi。
<span class="pln">$ firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">permanent </span><span class="pun">--</span><span class="pln">add</span><span class="pun">-</span><span class="pln">service</span><span class="pun">=</span><span class="kwd">ssh</span>
<span class="pln">$ firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">reload</span>
OK,现在准备工作都已就绪,安装了源,安装配置了防火墙,下一步开始搭建服务了。
搭建 Shadowsocks 服务
这个服务是最简单也是最常用的。
安装组件
<span class="pln">$ </span><span class="kwd">yum</span><span class="pln"> install m2crypto python</span><span class="pun">-</span><span class="pln">setuptools</span>
<span class="pln">$ easy_install pip</span>
<span class="pln">$ pip install shadowsocks</span>
安装时部分组件需要输入 Y 确认。小内存 VPS 可以分别安装组件。
安装完成后配置服务器参数
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">shadowsocks</span><span class="pun">.</span><span class="pln">json</span>
写入如下配置:
<span class="pun">{</span>
<span class="str">"server"</span><span class="pun">:</span><span class="str">"0.0.0.0"</span><span class="pun">,</span>
<span class="str">"server_port"</span><span class="pun">:</span><span class="lit">8388</span><span class="pun">,</span>
<span class="str">"local_address"</span><span class="pun">:</span><span class="str">"127.0.0.1"</span><span class="pun">,</span>
<span class="str">"local_port"</span><span class="pun">:</span><span class="lit">1080</span><span class="pun">,</span>
<span class="str">"password"</span><span class="pun">:</span><span class="str">"mypassword"</span><span class="pun">,</span>
<span class="str">"timeout"</span><span class="pun">:</span><span class="lit">300</span><span class="pun">,</span>
<span class="str">"method"</span><span class="pun">:</span><span class="str">"aes-256-cfb"</span><span class="pun">,</span>
<span class="str">"fast_open"</span><span class="pun">:</span><span class="kwd">false</span><span class="pun">,</span>
<span class="str">"workers"</span><span class="pun">:</span><span class="lit">1</span>
<span class="pun">}</span>
将上面的 mypassword 替换成你的密码, server_port 也是可以修改的,例如 443 是 Shadowsocks 客户端默认的端口号。
如果需要修改端口,需要在防火墙里打开响应的端口,用 firewalld 操作就比较简单了:
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">firewalld</span><span class="pun">/</span><span class="pln">services</span><span class="pun">/</span><span class="kwd">ss</span><span class="pun">.</span><span class="pln">xml</span>
下面代码粘贴到里面:
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span>
<span class="tag"><service></span>
<span class="tag"><short></span><span class="pln">SS</span><span class="tag"></short></span>
<span class="tag"><description></span><span class="pln">Shadowsocks port</span>
<span class="tag"></description></span>
<span class="tag"><port</span><span class="atn">protocol</span><span class="pun">=</span><span class="atv">"tcp"</span><span class="atn">port</span><span class="pun">=</span><span class="atv">"自定义ss端口号"</span><span class="tag">/></span>
<span class="tag"></service></span>
保存退出,然后重启 firewalld 服务:
<span class="pln">$ firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">permanent </span><span class="pun">--</span><span class="pln">add</span><span class="pun">-</span><span class="pln">service</span><span class="pun">=</span><span class="kwd">ss</span>
<span class="pln">$ firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">reload</span>
运行命令,启动 Shadowsocks 服务
运行下面的命令:
<span class="pln">$ ssserver </span><span class="pun">-</span><span class="pln">c </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">shadowsocks</span><span class="pun">.</span><span class="pln">json</span>
至此 shadowsocks 搭建完成,shadowsocks 已经可以使用,如果你没有过高的要求,下面的步骤可以省略,下面是后台运行 Shadowsocks 的步骤。
安装 supervisor 实现后台运行
运行以下命令下载 supervisor:
<span class="pln">$ </span><span class="kwd">yum</span><span class="pln"> install python</span><span class="pun">-</span><span class="pln">setuptools</span>
<span class="pln">$ easy_install supervisor</span>
然后创建配置文件:
<span class="pln">$ echo_supervisord_conf </span><span class="pun">></span><span class="str">/etc/</span><span class="pln">supervisord</span><span class="pun">.</span><span class="pln">conf</span>
修改配置文件:
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">supervisord</span><span class="pun">.</span><span class="pln">conf</span>
在文件末尾添加:
<span class="pun">[</span><span class="pln">program</span><span class="pun">:</span><span class="pln">ssserver</span><span class="pun">]</span><span class="pln">command </span><span class="pun">=</span><span class="pln"> ssserver </span><span class="pun">-</span><span class="pln">c </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">shadowsocks</span><span class="pun">.</span><span class="pln">json</span>
<span class="pln">autostart</span><span class="pun">=</span><span class="kwd">true</span>
<span class="pln">autorestart</span><span class="pun">=</span><span class="kwd">true</span>
<span class="pln">startsecs</span><span class="pun">=</span><span class="lit">3</span>
设置 supervisord 开机启动,编辑启动文件:
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc</span><span class="pun">.</span><span class="kwd">local</span>
在末尾另起一行添加:
<span class="pln">$ supervisord</span>
保存退出(和上文类似)。另 centOS7 还需要为 rc.local
添加执行权限:
<span class="pln">$ </span><span class="kwd">chmod</span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc</span><span class="pun">.</span><span class="kwd">local</span>
至此运用 supervisord 控制 Shadowsocks 开机自启和后台运行设置完成。重启服务器即可。
搭建 Strongswan 实现在 iOS 上连接 VPN
如果你只是需要在 Android, PC 上使用 VPN,那可以直接忽略此章内容, Shadowsocks 已经可以非常完美的帮助以上设备实现翻墙。 但是由于 iOS 上无法使用 Shadowsocks 所以需要使用 Strongswon 建立 IPsecVPN。
下载并编译 Strongswan
首先我们来编译 Strongswan, 因为直接用 yum install 的不能用,原因不明,所以直接下载源码和依赖包进行编译。
下载 Strongswan 的源码:
<span class="pln">$ </span><span class="kwd">wget</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//download.strongswan.org/strongswan.tar.gz && tar zxvf strongswan* </span>
<span class="pln">$ </span><span class="kwd">cd</span><span class="pln"> strongswan</span><span class="pun">*</span>
下载编译源码所需要的依赖包(小内存请分批下载):
<span class="pln">$ </span><span class="kwd">yum</span><span class="pln"> install </span><span class="pun">-</span><span class="pln">y </span><span class="kwd">make</span><span class="kwd">gcc</span><span class="pln"> gmp</span><span class="pun">-</span><span class="pln">devel openssl openssl</span><span class="pun">-</span><span class="pln">devel</span>
因搬瓦工是 OpenVZ 的所以用下面的命令来进行配置:
<span class="pln">$ </span><span class="pun">./</span><span class="pln">configure </span><span class="pun">--</span><span class="pln">sysconfdir</span><span class="pun">=/</span><span class="pln">etc </span><span class="pun">--</span><span class="pln">disable</span><span class="pun">-</span><span class="pln">sql </span><span class="pun">--</span><span class="pln">disable</span><span class="pun">-</span><span class="pln">mysql </span><span class="pun">--</span><span class="pln">disable</span><span class="pun">-</span><span class="pln">ldap </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">dhcp </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">identity </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">mschapv2 </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">md4 </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">xauth</span><span class="pun">-</span><span class="pln">eap </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">peap </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">md5 </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">openssl </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">shared </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">unity </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">tls </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">ttls </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">tnc </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">dynamic </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">addrblock </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">radattr </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">nat</span><span class="pun">-</span><span class="pln">transport </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">kernel</span><span class="pun">-</span><span class="pln">netlink </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">kernel</span><span class="pun">-</span><span class="pln">libipsec</span>
非 OpenVZ 的请用下面的命令来进行配置:
<span class="pun">.</span><span class="str">/configure --sysconfdir=/</span><span class="pln">etc </span><span class="pun">--</span><span class="pln">disable</span><span class="pun">-</span><span class="pln">sql </span><span class="pun">--</span><span class="pln">disable</span><span class="pun">-</span><span class="pln">mysql </span><span class="pun">--</span><span class="pln">disable</span><span class="pun">-</span><span class="pln">ldap </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">dhcp </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">identity </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">mschapv2 </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">md4 </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">xauth</span><span class="pun">-</span><span class="pln">eap </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">peap </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">md5 </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">openssl </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">shared </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">unity </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">tls </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">ttls </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">tnc </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">eap</span><span class="pun">-</span><span class="pln">dynamic </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">addrblock </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">radattr </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">nat</span><span class="pun">-</span><span class="pln">transport </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">kernel</span><span class="pun">-</span><span class="pln">netlink</span>
开始编译源代码:
<span class="pln">$ </span><span class="kwd">make</span><span class="pun">&&</span><span class="kwd">sudo</span><span class="kwd">make</span><span class="pln"> install</span>
没有错误出现后,可进行下一步。
生成证书
建立个临时目录来生成证书:
<span class="pln">$ </span><span class="kwd">mkdir</span><span class="pun">~</span><span class="str">/ipsec_cert && cd ~/</span><span class="pln">ipsec_cert</span>
生成服务器证书
创建脚本。SERVER 换成自己的域名或IP 都行。
<span class="pln">$ </span><span class="kwd">wget</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//gist.githubusercontent.com/songchenwen/14c1c663ea65d5d4a28b/raw/cef8d8bafe6168388b105f780c442412e6f8ede7/server_key.sh</span>
<span class="pln">$ sh server_key</span><span class="pun">.</span><span class="pln">sh SERVER</span>
生成客户端证书
同样是他的脚本,这个脚本还会生成一个 .p12 证书,这个证书需要导入到 iOS 里,USER 换成你自己的用户名 EMAIL 换成你自己的 email。
<span class="pln">$ </span><span class="kwd">wget</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//gist.githubusercontent.com/songchenwen/14c1c663ea65d5d4a28b/raw/54843ae2e5e6d1159134cd9a90a08c31ff5a253d/client_key.sh</span>
<span class="pln">$ sh client_key</span><span class="pun">.</span><span class="pln">sh USER EMAIL</span>
复制证书到 /etc/ipsec.d/
Strongswan 需要的是 cacerts/strongswanCert.pem
、certs/vpnHostCert.pem
、private/vpnHostKey.pem
这三个文件。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">cp</span><span class="pln"> cacerts</span><span class="pun">/</span><span class="pln">strongswanCert</span><span class="pun">.</span><span class="pln">pem </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">ipsec</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">cacerts</span><span class="pun">/</span><span class="pln">strongswanCert</span><span class="pun">.</span><span class="pln">pem </span>
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">cp</span><span class="pln"> certs</span><span class="pun">/</span><span class="pln">vpnHostCert</span><span class="pun">.</span><span class="pln">pem </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">ipsec</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">certs</span><span class="pun">/</span><span class="pln">vpnHostCert</span><span class="pun">.</span><span class="pln">pem</span>
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">cp</span><span class="kwd">private</span><span class="pun">/</span><span class="pln">vpnHostKey</span><span class="pun">.</span><span class="pln">pem </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">ipsec</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="kwd">private</span><span class="pun">/</span><span class="pln">vpnHostKey</span><span class="pun">.</span><span class="pln">pem</span>
同步客户端证书到本地
客户端需要的是 .p12 证书和 cacerts/strongswanCert.pem
将这两个证书同步到本地,然后通过邮件发送到 iOS 设备中并安装
<span class="pln">$ </span><span class="kwd">scp</span><span class="pun">-</span><span class="pln">P </span><span class="kwd">ssh</span><span class="pun">端口</span><span class="pln"> root@</span><span class="pun">服务器</span><span class="kwd">ip</span><span class="pun">:~</span><span class="str">/ipsec_cert/</span><span class="pun">****.</span><span class="pln">p12 </span><span class="pun">~/</span>
<span class="pln">$ </span><span class="kwd">scp</span><span class="pun">-</span><span class="pln">P </span><span class="kwd">ssh</span><span class="pun">端口</span><span class="pln"> root@</span><span class="pun">服务器</span><span class="kwd">ip</span><span class="pun">:~</span><span class="str">/ipsec_cert/</span><span class="pln">cacerts strongswanCert</span><span class="pun">.</span><span class="pln">pem </span><span class="pun">~/</span>
配置 Strongswan
编辑 /etc/ipsec.conf
:
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">ipsec</span><span class="pun">.</span><span class="pln">conf</span>
将下面的代码覆盖原有内容:
<span class="pln">config setup</span>
<span class="com">### strictcrlpolicy=yes</span>
<span class="com">### uniqueids = replace</span>
<span class="com">### charondebug="cfg 2, dmn 2, ike 2, net 0" ### 要看Log时,取消注释本行</span>
<span class="pln">conn </span><span class="pun">%</span><span class="kwd">default</span>
<span class="pln">keyexchange</span><span class="pun">=</span><span class="pln">ikev1</span>
<span class="pln">dpdaction</span><span class="pun">=</span><span class="pln">hold</span>
<span class="pln">dpddelay</span><span class="pun">=</span><span class="lit">600s</span>
<span class="pln">dpdtimeout</span><span class="pun">=</span><span class="lit">5s</span>
<span class="pln">lifetime</span><span class="pun">=</span><span class="lit">24h</span>
<span class="pln">ikelifetime</span><span class="pun">=</span><span class="lit">240h</span>
<span class="pln">rekey</span><span class="pun">=</span><span class="kwd">no</span>
<span class="pln">left</span><span class="pun">=</span><span class="pln">emptyzone</span><span class="pun">.</span><span class="pln">github</span><span class="pun">.</span><span class="pln">io </span><span class="com">### 这里换成你登录 VPN 用的域名或 IP,与生成证书时相同 </span>
<span class="pln">leftsubnet</span><span class="pun">=</span><span class="lit">0.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">/</span><span class="lit">0</span>
<span class="pln">leftcert</span><span class="pun">=</span><span class="pln">vpnHostCert</span><span class="pun">.</span><span class="pln">pem</span>
<span class="pln">leftsendcert</span><span class="pun">=</span><span class="pln">always</span>
<span class="pln">right</span><span class="pun">=%</span><span class="pln">any</span>
<span class="pln">rightdns</span><span class="pun">=</span><span class="lit">8.8</span><span class="pun">.</span><span class="lit">8.8</span>
<span class="pln">rightsourceip</span><span class="pun">=</span><span class="lit">10.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">/</span><span class="lit">8</span>
<span class="pln">conn </span><span class="typ">CiscoIPSec</span>
<span class="pln">rightauth</span><span class="pun">=</span><span class="pln">pubkey</span>
<span class="pln">rightauth2</span><span class="pun">=</span><span class="pln">xauth</span>
<span class="kwd">auto</span><span class="pun">=</span><span class="pln">add</span>
编辑 /etc/ipsec.secrets
, 创建用户名及密码:
<span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">ipsec</span><span class="pun">.</span><span class="pln">secrets</span>
将以下内容添加进去:
<span class="com">### 验证用户所需的信息</span>
<span class="com">### 用户名 : EAP "密码"</span>
<span class="pun">:</span><span class="pln"> RSA vpnHostKey</span><span class="pun">.</span><span class="pln">pem</span>
<span class="pun">你的用户名</span><span class="pun">:</span><span class="pln"> EAP </span><span class="str">"你的密码"</span>
使用 firewalld 配置防火墙
用 firewalld 开放 4500、500 端口和 esp 协议。
<span class="pln">$ </span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">firewalld</span><span class="pun">/</span><span class="pln">services</span><span class="pun">/</span><span class="pln">ipsec</span><span class="pun">.</span><span class="pln">xml</span>
内容如下:
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span>
<span class="tag"><service></span>
<span class="tag"><short></span><span class="pln">IPsec</span><span class="tag"></short></span>
<span class="tag"><description></span><span class="pln">Internet Protocol Security (IPsec) incorporates security for network transmissions directly into the Internet Protocol (IP). IPsec provides methods for both encrypting data and authentication for the host or network it sends to. If you plan to use a vpnc server or FreeS/WAN, do not disable this option.</span><span class="tag"></description></span>
<span class="tag"><port</span><span class="atn">protocol</span><span class="pun">=</span><span class="atv">"ah"</span><span class="atn">port</span><span class="pun">=</span><span class="atv">""</span><span class="tag">/></span>
<span class="tag"><port</span><span class="atn">protocol</span><span class="pun">=</span><span class="atv">"esp"</span><span class="atn">port</span><span class="pun">=</span><span class="atv">""</span><span class="tag">/></span>
<span class="tag"><port</span><span class="atn">protocol</span><span class="pun">=</span><span class="atv">"udp"</span><span class="atn">port</span><span class="pun">=</span><span class="atv">"500"</span><span class="tag">/></span>
<span class="tag"><port</span><span class="atn">protocol</span><span class="pun">=</span><span class="atv">"udp"</span><span class="atn">port</span><span class="pun">=</span><span class="atv">"4500"</span><span class="tag">/></span>
<span class="tag"></service></span>
然后输入以下命令后,至此整个搭建过程就结束了。
<span class="pln">$ firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">permanent </span><span class="pun">--</span><span class="pln">add</span><span class="pun">-</span><span class="pln">service</span><span class="pun">=</span><span class="pln">ipsec</span>
<span class="pln">$ firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">permanent </span><span class="pun">--</span><span class="pln">add</span><span class="pun">-</span><span class="pln">masquerade</span>
<span class="pln">$ firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">reload</span>
把下载的两个证书用 email 发送到你的 iOS 上,安装后建立个 VPN 连接,选 IPsec,使用证书,选择你的用户名的证书即可,登录下试试吧。