本文介绍使用 StrongSwan 搭建 VPN 的过程,适合有一定 linux 基础的用户。
本文使用的服务器
- 1CPU,1G, 优惠码
- CentOS 8.3
- StrongSwan 5.8.2
StrongSwan 简介
StrongSwan 是基于 OpenSource IPsec 的 VPN 解决方案,官方网站:https://www.strongswan.org/ ,如果无法访问请使用科学上网,原因你懂的。
本文介绍使用 StrongSwan 搭建 VPN 的过程,适合有一定 linux 基础的用户。
StrongSwan 是基于 OpenSource IPsec 的 VPN 解决方案,官方网站:https://www.strongswan.org/ ,如果无法访问请使用科学上网,原因你懂的。
Shadowsocks 需要 python 运行.
1 2 3 4 5 6 7 8 9 10 11 12 | #安装 python setup tools yum install python-setuptools #安装pip easy_install pip #升级 pip pip install --upgrade pip #安装git yum install git #安装 shadowsocks, 当前版本为 3.0 pip install git+https://github.com/shadowsocks/shadowsocks.git@master #升级 shadowsocks pip install --upgrade git+https://github.com/shadowsocks/shadowsocks.git@master |
1 | nano /usr/lib/systemd/system/shadowsocks.service |
写入下面的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [Unit] Description=Shadowsocks Server Documentation=https://github.com/shadowsocks/shadowsocks After=network.target remote-fs.target nss-lookup.target [Service] Type=forking #设置启动时的配置文件,根据自己的需求改. ExecStart=/usr/bin/ssserver -c /usr/share/nginx/etc/shadowsocks.json -d start ExecReload=/bin/kill -HUP $MAINPID ExecStop=/usr/bin/ssserver -d stop [Install] WantedBy=multi-user.target |
保存退出
添加配置文件
1 2 | #改为上一步中的路径, 确保各级目录存在 nano /usr/share/nginx/etc/shadowsocks.json |
写入配置内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | { "port_password": { "8388":"密码1", "端口2":"密码2" }, "_comment": { "8388":"注释1,日志中标识用户用的", "端口2":"注释2" }, "timeout":300, "method":"aes-256-gcm", "fast_open": false } |
保存退出
启动服务
1 2 | systemctl enable shadowsocks systemctl start shadowsocks |
配置完成
记得在防火墙中开放相应端口
首先下载客户端, 虽然原作者删除了服务端代码, 但他仍在开发客户端.
下载后解压, 配置服务器参数即可使用, 方法就不多说了.
Let’ s Encrypt 是一个免费的 SSL/TLS 证书发行机构, 证书有效期为90天, 到期前30内可续期, 实现永久免费.
本次安装使用的服务器配置:
Let’ s Encrypt SSL 证书的的获取并不是像其他网站一样, 在页面上填写资申请证书, 而是需要在域名所在的服务器上安装一个客户端(python写的)去获取证书和续期.
目前 Let’ s Encrypt 支持两种类型的证书,一种是必须显示指定域名的证书,另一种是通配符证书,两种证书的申请方式略有不同。
Certbot 客户端是现在官方推荐的客户端
1 | yum install certbot |
运行 certbot,测试程序是否正常。
1 | certbot |
如果运行正常,将出现类似下面的提示。
1 2 | Saving debug log to /var/log/letsencrypt/letsencrypt.log Certbot doesn't know how to automatically configure the web server on this system. However, it can still get a certificate for you. Please run "certbot certonly" to do so. You'll need to manually configure your web server to use the resulting certificate. |
如果某些依赖包版本过低或不匹配,则可能会出现类似下面的提示。
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 27 28 | Traceback (most recent call last): File "/usr/bin/certbot", line 9, in <module> load_entry_point('certbot==0.26.1', 'console_scripts', 'certbot')() File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 570, in load_entry_point return get_distribution(dist).load_entry_point(group, name) File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2751, in load_entry_point return ep.load() File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2405, in load return self.resolve() File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2411, in resolve module = __import__(self.module_name, fromlist=['__name__'], level=0) File "/usr/lib/python2.7/site-packages/certbot/main.py", line 18, in <module> from certbot import account File "/usr/lib/python2.7/site-packages/certbot/account.py", line 18, in <module> from acme import messages File "/usr/lib/python2.7/site-packages/acme/messages.py", line 7, in <module> from acme import challenges File "/usr/lib/python2.7/site-packages/acme/challenges.py", line 11, in <module> import requests File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 58, in <module> from . import utils File "/usr/lib/python2.7/site-packages/requests/utils.py", line 32, in <module> from .exceptions import InvalidURL File "/usr/lib/python2.7/site-packages/requests/exceptions.py", line 10, in <module> from .packages.urllib3.exceptions import HTTPError as BaseHTTPError File "/usr/lib/python2.7/site-packages/requests/packages/__init__.py", line 95, in load_module raise ImportError("No module named '%s'" % (name,)) ImportError: No module named 'requests.packages.urllib3' |
根据所缺少的依赖包的不同,提示内容可能不同,使用下面的命令尝试修复。
1 2 3 4 5 | #升级pip pip install -U pip #升级相关包,指定 requests 包的版本为 2.6.0, 使用其它版本可能无法解决依赖问题。 pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3 pyOpenSSL |
再次运行 certbot ,程序正常。
一. 申请固定域名证书
下面讲一下如何从头配置 CentOS 生产环境, 以 DigitalOcean 云主机为例, 有关此云主机的介绍 请点这里
CentOS 7 为64位系统, 最低内存要求512M.
Remi 主打 php 及相关扩展, 所以安装 php 这个源是不二选择.
首先进入 Remi 网站, 在 Maintained Enterprise Linux (RHEL / CentOS / Other clones) 列表中找到 Enterprise Linux 7 项后面的 remi-release-7.rpm , 执行下面的命令安装.
1 | yum install http://mirror.innosol.asia/remi/enterprise/remi-release-7.rpm |
编辑 remi.repo 文件
1 | nano /etc/yum.repos.d/remi.repo |
修改 [remi]
1 | enabled=1 |
保存退出
编辑 remi-php72.repo
1 | nano /etc/yum.repos.d/remi-php72.repo |
修改 [remi-php72]
1 | enabled=1 |
保存退出
一般来说, 安装 Remi 源的时候会附带安装 EPEL 源, 如果没有安装, 则按以下方法安装.
EPEL 包含丰富的软件, 进入网站往下拉, 找到 Quickstart 项, 根据系统架构与版本选 RHEL/CentOS 7, 点击, 系统会根据来访ip查找最快的源镜像, 国内访问通常会转到搜狐与中科大的源.如果想用美国源, 最好是用代理访问或者在服务器中输入如下命令
1 | yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm |
进入 Nginx 官网 , 点右侧的 download 链接, 拉到最下面找到 Pre-Built Packages 项. 点 mainline version(主线版本; stable version 为稳定版本) 版本的链接. 根据提示编辑 repo 文件的内容, 具体操作如下.
在 yum repo 目录创建新的 nginx.repo 文件
1 | nano /etc/yum.repos.d/nginx.repo |
输入以下内容
1 2 3 4 5 | [nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck=0 enabled=1 |
保存退出
MariaDB 是 MySql 分支出来的项目, 因为 MySql 被 Oracle 收购且闭源, 所以有了 MariaDB, 并逐步添加新的功能. 最新版 10.x 已不再从 MySql 合并代码.
打开页面
https://downloads.mariadb.org/mariadb/repositories/#mirror=qiming&distro=CentOS
1 2 3 4 5 | #从IE导入代理设置 netsh winhttp import proxy ie #独立设置代理 netsh winhttp set proxy 127.0.0.1:1088 "<local>;localhost;127.*;10.*;172.16.*;192.168.*" |
在命令行设置代理服务器。
1 2 3 4 5 6 | #获取 http 请求的代理设置 npm config get proxy npm config set proxy="http://127.0.0.1:1080" #获取 https 请求的代理设置 npm config get https-proxy npm config set https-proxy="http://127.0.0.1:1080" |
代理服务器可以使用小灰机(小灰机同时支持 socks 代理和 http 代理)
因为众所周知的原因,在国内使用 NuGet 时会经常无法下载包,严重影响工作效率,下面说一下如何给 NuGet 设置代理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?xml version="1.0" encoding="utf-8"?> <configuration> <config> <add key="http_proxy" value="http://127.0.0.1:1080" /> </config> <packageSources> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" /> </packageSources> <packageRestore> <add key="enabled" value="True" /> <add key="automatic" value="True" /> </packageRestore> <bindingRedirects> <add key="skip" value="False" /> </bindingRedirects> <packageManagement> <add key="format" value="0" /> <add key="disabled" value="False" /> </packageManagement> <disabledPackageSources> <add key="Microsoft Visual Studio Offline Packages" value="true" /> </disabledPackageSources> </configuration> |
代理服务器可以是任何 http 代理,也可以用小灰机代理(小灰机本身即是 socks 代理也是 http 代理)。
Win10 的 Hyper-V 虚拟机默认安装有一个虚拟交换机,这个交换机不能修改,也不能删除,它默认启用NAT, 可以连接外网。但有一个缺点,每次重启主机系统后它的ip都会随机改变,当有多个虚拟机想组内网时无法使用这个交换机。
1 2 3 4 5 6 7 8 | #下载安装脚本 wget https://raw.githubusercontent.com/Angristan/OpenVPN-install/master/openvpn-install.sh #给脚本增加可执行权限 chmod +x openvpn-install.sh #运行脚本安装 ./openvpn-install.sh |
想要更多?没了!就是这么简单。
安装过程需要用户选择多次选择设置项。按提示输入序号就可以。 具体说明请看 这里
如果你同我一样需要在路由器上连接 OpenVPN 服务端,可能会遇到一些问题,最典型的就是客户端的版本过低,服务端不支持。例如我的华硕路由器的 OpenVPN 客户端是 2.3.2 版本的,上面安装的服务端加密方式要求最低版本为2.4。
要解决这个问题,只需修改几项配置即可。
修改服务端配置
1 | nano /etc/openvpn/server.conf |
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 | port 1194 proto udp dev tun user nobody group nobody persist-key persist-tun keepalive 10 120 topology subnet server 10.8.0.0 255.255.255.0 ifconfig-pool-persist ipp.txt push "dhcp-option DNS 8.8.8.8" push "dhcp-option DNS 8.8.4.4" push "redirect-gateway def1 bypass-dhcp" crl-verify crl.pem ca ca.crt cert server_7nXHxN25RjHzpvzv.crt key server_7nXHxN25RjHzpvzv.key dh dh.pem compress lz4-v2 push "compress lz4-v2" tls-server tls-auth tls-auth.key 0 cipher AES-256-CBC status /var/log/openvpn/status.log verb 3 |
修改客户端配置
在脚本的同一目录下会生成一个 .ovpn 文件,这是给客户端的配置文件。编辑它。
1 | nano client.ovpn |
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 27 28 29 30 31 32 | client proto udp remote 你的ip或域名 1194 dev tun resolv-retry infinite nobind persist-key persist-tun remote-cert-tls server verify-x509-name server_TP2kfJ5lwhmkDCEc name auth SHA256 auth-nocache cipher AES-128-CBC tls-client #注释掉下面两行 #tls-version-min 1.2 #tls-cipher TLS-DHE-RSA-WITH-AES-128-GCM-SHA256 setenv opt block-outside-dns verb 3 <ca> #此处省略 </ca> <cert> #此处省略 </cert> <key> #此处省略 </key> key-direction 1 <tls-auth> #此处省略 </tls-auth> |
保存之后将此文件上传到路由器,即可成功连接vpn。
查看服务端运行状态
1 | systemctl status openvpn@server |
CentOS 8 安装请移步 《CentOS 8 使用 Strongswan IPsec IKEv2 搭梯》
本文介绍使用 StrongSwan 搭建 VPN 的过程,适合有一定 linux 基础的用户。
本文使用的服务器
StrongSwan 是基于 OpenSource IPsec 的 VPN 解决方案,官方网站:https://www.strongswan.org/ ,如果无法访问请使用科学上网,原因你懂的。
StrongSwan 运行于 Linux 系统上,本文使用 CentOS 7 系统。
由于基于源码自行编译安装过于繁琐,这里只介绍使用 yum 的安装方式。
首先,配置 StrongSwan 的源,此软件已包含在 EPEL 源中,关于配置 yum 源,请参考 《CentOS 7 配置 LNMP + FTP 环境》文中的 “添加常用软件源” 部分。
安装 StrongSwan
1 | yum install strongswan |
启用开机启动
1 | systemctl enable strongswan |
StrongSwan IPsec IKEv2 连接需要用到服务器证书,用于验证服务器身份。由于自签发证书不受操作系统信任,我们需要申请 Let’s Encrypt 免费证书。
申请方式参考《CentOS 7 Nginx Let’ s Encrypt SSL 证书安装配置》, 申请的域名必须是明确的,不能用通配符证书代替。例如,vpn.xxx.com, 申请证书时,必须带上 -d vpn.xxx.com 参数。
安装证书
假设上一步我们申请的证书保存在 /etc/letsencrypt/live/xxx.com 中,xxx.com 是你申请证书使用的域名。
我们使用创建软连接的方式使用证书
1 2 3 4 5 6 | cd /etc/strongswan/ipsec.d ln -s /etc/letsencrypt/live/xxx.com/fullchain.pem ./certs/fullchain.pem ln -s /etc/letsencrypt/live/xxx.com/privkey.pem ./private/privkey.pem #下载 letsencrype 中间证书, 此步对于 windows 客户端连接至关重要。 wget https://letsencrypt.org/certs/lets-encrypt-r3-cross-signed.pem -O /etc/strongswan/ipsec.d/cacerts/lets-encrypt-x3-cross-signed.pem |
证书安装完成
修改 ipsec.conf 主配置文件
1 | nano /etc/strongswan/ipsec.conf |
删除原有内容,写入已下内容
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 27 28 29 30 31 32 33 | conn setup uniqueids = no conn %default compress = yes keyexchange=ike ike=aes256-sha256-sha1-modp2048-modp1024,3des-sha256-sha1-modp2048-modp1024! esp=aes256-sha256-sha1,3des-sha256-sha1! leftdns=8.8.8.8,8.8.4.4 rightdns=8.8.8.8,8.8.4.4 conn ikev2 dpdaction=clear dpddelay=60s rekey=no fragmentation=yes eap_identity=%identity left=%any leftid=这里用你申请证书时使用的域名,如“xxx.com” leftsubnet=0.0.0.0/0 leftauth=pubkey leftcert=fullchain.pem leftsendcert=always leftfirewall=yes right=%any rightid=%any rightsourceip=10.1.0.0/24 rightauth=eap-mschapv2 rightsendcert=never auto=add |
修改 charon.conf, 配置日志输出文件。
1 | nano /etc/strongswan/strongswan.d/charon.conf |
日志不是必要项,建议只用在调试配置时使用,配置正常后注释掉 filelog 节部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | charon { filelog { charon-debug-log { path = /var/log/charon_debug.log time_format = %b %e %T default = 2 mgr = 0 net = 1 enc = 1 asn = 1 job = 1 ike_name = yes append = no flush_line = yes } } #这里是其它设置...... } |
修改 ipsec.secrets , 增加 vpn 账户。
1 | nano /etc/strongswan/ipsec.secrets |
添加服务端证书私钥,新建用户账号。
1 2 3 4 5 | #这是申请的证书的私钥 : RSA privkey.pem #这是用户账号,每行一个账号,格式为 [账号 %any : EAP "密码"] user1 %any : EAP "password1" user2 %any : EAP "password2" |
1 | nano /etc/sysctl.conf |
增加下面的内容
1 2 3 4 5 | # VPN net.ipv4.ip_forward = 1 net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv6.conf.all.forwarding=1 |
重新加载规则
1 | sysctl -p |
CentOS 7 默认使用 firewalld 防火墙, 有关 firewalld 的介绍请看这里。
允许 ‘AH’ 和 ‘ESP’ 身份验证协议和加密协议通过防火墙
1 2 | firewall-cmd --zone=public --permanent --add-rich-rule='rule protocol value="esp" accept' firewall-cmd --zone=public --permanent --add-rich-rule='rule protocol value="ah" accept' |
开放 ipsec 和相关端口
1 2 3 | firewall-cmd --zone=public --permanent --add-port=500/udp firewall-cmd --zone=public --permanent --add-port=4500/udp firewall-cmd --zone=public --permanent --add-service="ipsec" |
允许 ip 伪装
1 | firewall-cmd --zone=public --permanent --add-masquerade |
然后重新加载防火墙
1 | firewall-cmd --reload |
首先禁用 firewalld
1 2 | systemctl stop firewalld systemctl disable firewalld |
然后安装 iptables
1 2 | yum install iptables-services systemctl enable iptables |
配置 iptables
1 | nano /etc/sysconfig/iptables |
在默认内容基础上,添加几条规则(下面高亮的部分)。
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 | *nat :PREROUTING ACCEPT [0:0] :INPUT ACCEPT [0:0] :OUTPUT ACCEPT [0:0] :POSTROUTING ACCEPT [0:0] -A POSTROUTING -m policy --pol ipsec --dir out -j ACCEPT -A POSTROUTING -s 10.1.0.0/24 -o eth0 -j MASQUERADE COMMIT *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -i eth0 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p ah -j ACCEPT -A INPUT -p esp -j ACCEPT -A INPUT -p udp -m udp --dport 500 -j ACCEPT -A INPUT -p udp -m udp --dport 4500 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -i eth0 -m policy --pol ipsec --dir in -j ACCEPT -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT |
重新加载 iptables 规则
1 | systemctl restart iptables |
1 | systemctl restart strongswan |
开始->设置->网络和Internet->vpn->添加vpn连接
添加完成后点击“更改适配器选项”
在vpn连接项点右键,属性->网络
双击 ipv4 ,点击“高级”
勾选“在远程网络上使用默认网关”, 取消“自动跃点”,手动填写跃点数为“10”(当自动跃点无法上网时才设置手动跃点)。如何确定跃点数,请看下面内容。
确定后连接 vpn 即可。
确定跃点数
因为 win10 系统的问题,经常会出现vpn连接能连上,就是打不开网页的情况,这是因为“本地连接”的优先级高于“vpn”的优先级,导致所有的请求优先使用“本地连接”的dns去解析域名,由于我们国家坚持推行使用社会主义特色的dns,所以某些域名解析会返回无效或错误的ip地址,这就导致我们无法正常访问网站!跃点数就是设定优先级用的。
我们需要做的就是让 “vpn 连接” 的跃点数小于 “本地连接” 的跃点数。
断开vpn, 打开 PowerShell 窗口,输入下面命令。
1 2 | #查看接口 Get-NetIPInterface -AddressFamily ipv4 |
第一列表示当前接口的索引号,第二列是名称,InterfaceMetric 列即接口的跃点,我的本地连接名是 WLAN, 跃点是35。
查看“本地连接”网卡的跃点数为 35,因此上一步中设置的跃点数只要小于35即可(跃点数越小优先级越高),所以我们设置为10。
“设置->VPN->添加配置”, 选 IKEv2
描述: 随便填
服务器: 填url或ip
远程ID: ipsec.conf 中的 leftid, 如 “xxx.com”
用户鉴定: 用户名
用户名: EAP 项用户名, 如 “user1”
密码: EAP 项密码, 如 “password1”
原理同 ios, 我没有 mac 做验证。
需下载安装客户端程序,前往官网下载
最新版 apk 程序
目前 8.0.12 版本有 bug, 无法在指定目录初始化数据文件, 请下载 8.0.11 版本.
MySql for Windows 5.7.7 之前的 zip 版中都会附带 Data 目录, 里面有 MySql 的初始数据库. 从 5.7.7 版本开始, 就没有这个目录了, 需要用命令初始化数据库.
当前最新 .net core 版本为 2.1
打开 .net core 下载页面,https://www.microsoft.com/net/download/linux ,页面包含 .net core sdk 及 .net core runtime 两个安装包。
如果需要在 linux 上进行开发,要安装 .net core sdk 版本,不需要开发推荐安装 .net core runtime 版本。
点击 install .NET Core 2.1 Runtime ,进入下载页面,在下拉框中选择 Centos。
按提示输入以下命令
1 2 3 4 5 6 7 | rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm #如果需要同时支持运行 asp.net core 和 .net core app ,则运行这条命令 yum install aspnetcore-runtime-2.1 #如果只要需运行 .net core app, 不需要 asp.net core ,则运行这条命令 yum install dotnet-runtime-2.1 |
安装完成后运行 dotnet –info 查看版本信息
1 2 3 4 5 6 7 8 9 | #从学生表中删除姓名重复的学生记录, 仅保留重复记录中id最大的一条 delete a from t_student as a join (select name, max(id) as maxid from t_student group by name having count(*) > 1 ) as b on a.name = b.name and a.id < b.maxid |