最近希望有一个私人的流量中转站,即使在办公室也能访问家里的计算和存储,低延迟低流量低配置的乞丐VPS就可以。
抠门的我挑了半天,还是选择了大厂,阿里云成都分云,这次不用红帽系了,因为最近一直在Debian、Ubuntu和Armbian。
初始化
首次登录好习惯改密码(为了方便,应该把zsh、git、oh-my-zsh、vim、tmux、aria2c装上。):
# VPS上改密码passwd# 修改主机名,阿里云默认的太丑了sudo hostnamectl set-hostname aliyun.mydomain.tech# 电脑上上传秘钥进行ssh免密登录,要指定秘钥则`ssh-copy-id -i ~/.ssh/id_rsa.pub root@4*.*.*.*`ssh-copy-id root@4*.*.*.*
sudo apt update && sudo apt dist-upgrade -ysudo apt install build-essential curl file git -ysudo apt install zsh git vim tmux aria2chsh -s $(which zsh)sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 安装oh-my-tmux,github不通时,可以用gitclone先顶一下cd ~git clone https://gitclone.com/github.com/gpakosz/.tmux.gitln -s -f .tmux/.tmux.confcp .tmux/.tmux.conf.local .将ssh设置为无法用密码登录(只接受key pair认证),顺便调整ssh超时时间:
# /etc/ssh/sshd_config 中:# 禁止密码认证PasswordAuthentication no# 调整ssh会话一次存活60秒,自动重连60次ClientAliveInterval 60ClientAliveCountMax 60
# 重启sshd服务:sudo systemctl restart ssh.service开防火墙策略:
sudo ufw default deny incomingsudo ufw default allow outgoingsudo ufw allow 22/tcp comment sshsudo ufw allow 6800/tcp comment aria2csudo ufw allow 2017/tcp comment v2rayasudo ufw enable**注意:**若端口已经打开,使用nc检测端口发现公网IP(即4x.x.x.x)的端口不通,而内网IP(即172.x.x.x)端口正常。则需要在云服务器ESC->网络与安全->安全组里添加端口。
开启虚拟内存:
# 检查虚拟内存十分开启:free -m# 分配虚拟内存文件的空间,大小为 bs*countsudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576# 设置文件权限chmod 600 /swapfile# 制作虚拟内存分区mkswap /swapfile# 激活虚拟内存swapon /swapfile# 此时检查就会有虚拟内存了free -m# 要一直使用的话,更改开机挂载虚拟内存vim /etc/fstab/swapfile swap swap defaults 0 0
# 检测swappiness值,即系统使用交换空间的优先级,默认为60即可,刚装好的阿里云为0cat /proc/sys/vm/swappiness# 临时修改sysctl vm.swappiness=60# 永久修改vim /etc/sysctl.confvm.swappiness=60安装科学冲浪
安装核心,如果install-release.sh不能下载,可以直接浏览器打开复制粘贴,然后chmod +x install-release.sh,运行即可:
# 安装或更新核心sudo bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install# 仅更新核心sudo bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install-geodata# 卸载核心sudo bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ remove
sudo systemctl status xray.service安装UI,直接在release中挑了一个arm64的包。如果实在下不下来,就scp ./installer_debian_amd64_1.5.9.1698.1.deb root@47.109.18.231:./传一个上去吧,或者传到哪个没被ban的公网服务器上下载也行。
wget https://github.com/v2rayA/v2rayA/releases/download/v1.5.9.1698.1/installer_debian_amd64_1.5.9.1698.1.debsudo apt install ./installer_debian_arm64_1.5.9.1698.1.deb# 可以添加为系统服务sudo systemctl enable v2raya.servicesudo systemctl start v2raya.servicesudo systemctl status v2raya.service配置aira2c
配置aria2c远程下载服务,本地使用AriaNg搭配风味更佳:
mkdir -p ~/.config/aria2touch ~/.config/aria2/aria2.conf #aria2的配置文件touch ~/.config/aria2/aria2.session #aria2保存会话的文件vim ~/.config/aria2/aria2.conf #修改配置文件配置如下:
## '#'开头为注释内容, 选项都有相应的注释说明, 根据需要修改 #### 被注释的选项填写的是默认值, 建议在需要修改时再取消注释 ##
## 文件保存相关 ##
# 文件的保存路径(可使用绝对路径或相对路径), 默认: 当前启动位置dir=/webroot/downloads# 启用磁盘缓存, 0为禁用缓存, 需1.16以上版本, 默认:16M#disk-cache=32M# 文件预分配方式, 能有效降低磁盘碎片, 默认:prealloc# 预分配所需时间: none < falloc ? trunc < prealloc# falloc和trunc则需要文件系统和内核支持# NTFS建议使用falloc, EXT3/4建议trunc, MAC 下需要注释此项#file-allocation=none# 断点续传continue=true
## 下载连接相关 ##
# 最大同时下载任务数, 运行时可修改, 默认:5#max-concurrent-downloads=5# 同一服务器连接数, 添加时可指定, 默认:1max-connection-per-server=16# 最小文件分片大小, 添加时可指定, 取值范围1M -1024M, 默认:20M# 假定size=10M, 文件为20MiB 则使用两个来源下载; 文件为15MiB 则使用一个来源下载min-split-size=4M# 单个任务最大线程数, 添加时可指定, 默认:5split=16# 整体下载速度限制, 运行时可修改, 默认:0max-overall-download-limit=3000000# 单个任务下载速度限制, 默认:0#max-download-limit=0# 整体上传速度限制, 运行时可修改, 默认:0max-overall-upload-limit=50000# 单个任务上传速度限制, 默认:0#max-upload-limit=0# 禁用IPv6, 默认:falsedisable-ipv6=true# 连接超时时间, 默认:60#timeout=60# 最大重试次数, 设置为0表示不限制重试次数, 默认:5#max-tries=5# 设置重试等待的秒数, 默认:0#retry-wait=0
## 进度保存相关 ##
# 从会话文件中读取下载任务input-file=/root/.config/aria2/aria2.session# 在Aria2退出时保存`错误/未完成`的下载任务到会话文件save-session=/root/.config/aria2/aria2.session# 定时保存会话, 0为退出时才保存, 需1.16.1以上版本, 默认:0save-session-interval=60
## RPC相关设置 ##
# 启用RPC, 默认:falseenable-rpc=true# 允许所有来源, 默认:falserpc-allow-origin-all=true# 允许非外部访问, 默认:falserpc-listen-all=true# 事件轮询方式, 取值:[epoll, kqueue, port, poll, select], 不同系统默认值不同#event-poll=select# RPC监听端口, 端口被占用时可以修改, 默认:6800rpc-listen-port=6800# 设置的RPC授权令牌, v1.18.4新增功能, 取代 --rpc-user 和 --rpc-passwd 选项rpc-secret=<your-rpc-secret># 设置的RPC访问用户名, 此选项新版已废弃, 建议改用 --rpc-secret 选项#rpc-user=<USER># 设置的RPC访问密码, 此选项新版已废弃, 建议改用 --rpc-secret 选项#rpc-passwd=<PASSWD># 是否启用 RPC 服务的 SSL/TLS 加密,# 启用加密后 RPC 服务需要使用 https 或者 wss 协议连接#rpc-secure=true# 在 RPC 服务中启用 SSL/TLS 加密时的证书文件,# 使用 PEM 格式时,您必须通过 --rpc-private-key 指定私钥#rpc-certificate=/path/to/certificate.pem# 在 RPC 服务中启用 SSL/TLS 加密时的私钥文件#rpc-private-key=/path/to/certificate.key
## BT/PT下载相关 ##
# 当下载的是一个种子(以.torrent结尾)时, 自动开始BT任务, 默认:truefollow-torrent=true# BT监听端口, 当端口被屏蔽时使用, 默认:6881-6999listen-port=51414# 单个种子最大连接数, 默认:55#bt-max-peers=55# 打开DHT功能, PT需要禁用, 默认:trueenable-dht=true# 打开IPv6 DHT功能, PT需要禁用#enable-dht6=false# DHT网络监听端口, 默认:6881-6999dht-listen-port=6881-6999# 本地节点查找, PT需要禁用, 默认:falsebt-enable-lpd=true# 种子交换, PT需要禁用, 默认:trueenable-peer-exchange=true# 每个种子限速, 对少种的PT很有用, 默认:50Kbt-request-peer-speed-limit=3M# 客户端伪装, PT需要peer-id-prefix=-TR2770-user-agent=Transmission/2.77peer-agent=Transmission/2.77# 当种子的分享率达到这个数时, 自动停止做种, 0为一直做种, 默认:1.0seed-ratio=1.0seed-time=0# 强制保存会话, 即使任务已经完成, 默认:false# 较新的版本开启后会在任务完成后依然保留.aria2文件#force-save=false# BT校验相关, 默认:true#bt-hash-check-seed=true# 继续之前的BT任务时, 无需再次校验, 默认:falsebt-seed-unverified=true# 保存磁力链接元数据为种子文件(.torrent文件), 默认:falsebt-save-metadata=false# dht-filedht-file-path=/root/.config/aria2/dht.dat# 删除未选择的文件bt-remove-unselected-file=true# 无速度时自动停止时间#bt-stop-timeout=1800启动aria2c:
aria2c --conf-path=$HOME/.config/aria2/aria2.conf -D安装nps内网穿透服务端
小可怜服务器没多少空间舍不得安装golang,直接从release列表中下载相应的二进制运行:
wget https://github.com/ehang-io/nps/releases/download/v0.26.10/linux_amd64_server.tar.gztar -zxvf linux_amd64_server.tar.gz -C linux_amd64_serversudo ./nps install# 安装完成后支持的命令:`nps start|stop|restart|uninstall|update or nps-update update`sudo nps start修改配置文件/etc/nps/conf/nps.conf的用户名和密码,以及默认的http和https监听,这台VPS我用打算用来中转流量,所以80和443打算暂时留给nginx:
# ...web_username=xxxweb_password=xxx# ...#http_proxy_port=80#https_proxy_port=443# ...查看日志cat /var/log/nps.log:
2022/12/03 19:28:15.920 [I] [nps.go:202] the version of server is 0.26.10 ,allow client core version to be 0.26.02022/12/03 19:28:16.496 [I] [server.go:200] tunnel task start mode:httpHostServer port 02022/12/03 19:28:16.496 [I] [connection.go:71] web management start, access port is 80802022/12/03 19:28:16.508 [I] [connection.go:36] server start, the bridge type is tcp, the bridge port is 8024在内网Rock-5B上下载客户端:
wget https://github.com/ehang-io/nps/releases/download/v0.26.10/linux_arm64_client.tar.gz或是在内网树莓派3B+上下载客户端:
wget https://github.com/ehang-io/nps/releases/download/v0.26.10/linux_arm_v7_client.tar.gz按照说明添加一个客户端,并在树莓派上尝试连接nps服务器:
./npc -server=4x.x.x.x:8024 -vkey=xxxxxxx -type=tcp此时刷新nps管理页面,客户端已显示线了。在管理页面的TCP隧道中加一条端口映射,就可以从电脑上连接ssh -p 10022 pi@4x.x.x.x了。
自动续期SSL证书
教程,我希望用4*服务器上的nps,将443映射给内网120服务器的443端口,并使得证书能够正常验证。
此时我使用apt update后,apt报错,很明显mirrors.cloud.aliyuncs.com这个地址指向我的Justhost服务器了,ping也是直接被解析成了ping mirrors.cloud.aliyuncs.com.mydomain.tech,此时dig、nslookup都不行,但resolvectl query mirrors.cloud.aliyuncs.com仍然正常。我查了ping命令,有文章说ping的解析靠/etc/nsswitch中host项目的配置,于是我改/etc/nsswitch、/etc/hosts都不行。然后搜索这个域名发现有的文章说,需要修改/etc/hosts:
# 这一组不行,v2rayA DNS hijack,这是v2rayA开启时注入的#nameserver 127.2.0.17#nameserver 119.29.29.29# 这一组不行,这是v2rayA关闭后改回来的#nameserver 223.6.6.6#nameserver 119.29.29.29# 这一组公共DNS也不行#nameserver 8.8.8.8#nameserver 114.114.114.114# 这一组使得`mirrors.cloud.aliyuncs.com`恢复# 即这个域名需要阿里云vpc下dns才能解析nameserver 100.100.2.136nameserver 100.100.2.138申请SSL证书,
# 安装acme.sh,末尾的`-s email=xxx@xxx.com是用来接受let's encrypt等更新邮件的地址curl https://get.acme.sh | sh -s email=xxx@xxx.com# 如果使用standalone模式,acme.sh会需要socat在80或443上建立服务sudo apt install socat
# 重载shell以生效source ~/.zshrc# 验证安装acme.sh -vhttps://github.com/acmesh-official/acme.shv3.0.5# 另外,acme.sh已经将默认CA设置为ZeroSSL,如果要用let's encrypt需要指定服务,# ZeroSSL只支持http-01 dns-01,不支持tls-alpn-01acme.sh --set-default-ca --server letsencrypt# 默认使用443端口,如果要修改需要指定`--httpport 8443`# 使用前记得停掉443上的进程,比如npsacme.sh --issue -d aliyun.mydomain.tech --alpn[Sat Dec 3 09:13:38 PM CST 2022] Your cert is in: /root/.acme.sh/aliyun.mydomain.tech/aliyun.mydomain.tech.cer[Sat Dec 3 09:13:38 PM CST 2022] Your cert key is in: /root/.acme.sh/aliyun.mydomain.tech/aliyun.mydomain.tech.key[Sat Dec 3 09:13:38 PM CST 2022] The intermediate CA cert is in: /root/.acme.sh/aliyun.mydomain.tech/ca.cer[Sat Dec 3 09:13:38 PM CST 2022] And the full chain certs is there: /root/.acme.sh/aliyun.mydomain.tech/fullchain.cer#申请成功后,查看已申请的证书信息acme.sh --info -d aliyun.mydomain.tech安装证书到nginx:
acme.sh --install-cert -d aliyun.mydomain.tech \--key-file /etc/nginx/aliyun.mydomain.tech/key.pem \--fullchain-file /etc/nginx/aliyun.mydomain.tech/cert.pem \--reloadcmd "service nginx force-reload"安装证书到syncthing
acme.sh --install-cert -d aliyun.mydomain.tech \--key-file /root/.config/syncthing/https-key.pem \--fullchain-file /root/.config/syncthing/https-cert.pem \