作为通信工程师,我经常需要访问国际学术资源和技术文档,科学上网工具成为必备的工作助手,本文将详细介绍如何从零开始搭建自己的Shadowsocks(SS)科学上网服务,让你拥有一个专属、稳定且安全的网络访问通道。
准备工作
服务器选购
首先你需要一台位于海外的VPS服务器,推荐选择以下服务商:
- Vultr:按小时计费,最低配置$5/月
- DigitalOcean:稳定可靠,$5/月起
- Linode:性能优秀,$5/月起
- AWS Lightsail:亚马逊云服务,$3.5/月起
建议选择日本、新加坡或美国西海岸的机房,这些地区到中国大陆的网络延迟相对较低。
操作系统选择
推荐使用以下Linux发行版:
- Ubuntu 20.04/22.04 LTS
- Debian 10/11
- CentOS 7/8
本文以Ubuntu 22.04为例进行演示。
服务器基础配置
连接服务器
使用SSH工具连接你的VPS:
ssh root@your_server_ip
系统更新
首次登录后执行系统更新:
apt update && apt upgrade -y
防火墙配置
配置UFW防火墙,开放必要端口:
apt install ufw -y ufw allow 22/tcp # SSH端口 ufw allow 你的SS端口/tcp # Shadowsocks端口 ufw enable
安装Shadowsocks服务端
安装Python和pip
apt install python3 python3-pip -y
安装Shadowsocks
pip3 install shadowsocks
创建配置文件
创建配置文件/etc/shadowsocks.json:
nano /etc/shadowsocks.json
根据实际情况修改):
{
"server":"0.0.0.0",
"server_port":8388,
"password":"your_password",
"timeout":300,
"method":"aes-256-gcm",
"fast_open":false
}
参数说明:
- server_port:SS服务端口(建议使用1024-65535之间的端口)
- password:连接密码(建议使用强密码)
- method:加密方式(推荐aes-256-gcm)
启动Shadowsocks服务
使用以下命令启动:
ssserver -c /etc/shadowsocks.json -d start
检查服务状态:
ssserver -c /etc/shadowsocks.json -d status
设置开机自启
创建systemd服务文件/etc/systemd/system/shadowsocks.service:
nano /etc/systemd/system/shadowsocks.service
[Unit]
Description=Shadowsocks Server
After=network.target
[Service]
ExecStart=/usr/local/bin/ssserver -c /etc/shadowsocks.json
Restart=on-failure
[Install]
WantedBy=multi-user.target
启用并启动服务:
systemctl enable shadowsocks systemctl start shadowsocks systemctl status shadowsocks
客户端配置
客户端下载
根据你的设备选择对应的客户端:
- Windows: Shadowsocks-Windows
- macOS: ShadowsocksX-NG
- Android: Shadowsocks-Android
- iOS: Shadowrocket或Potatso Lite
客户端配置
在客户端中添加服务器配置:
- 服务器IP:你的VPS IP地址
- 端口:配置文件中设置的server_port
- 密码:配置文件中设置的password
- 加密方式:配置文件中设置的method
保存后连接即可开始使用。
性能优化
开启TCP BBR加速
BBR是Google开发的TCP拥塞控制算法,可显著提升网络速度。
执行以下命令:
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf sysctl -p
验证是否生效:
sysctl net.ipv4.tcp_congestion_control
应返回net.ipv4.tcp_congestion_control = bbr
多用户配置
如果需要支持多用户,修改配置文件为:
{
"server":"0.0.0.0",
"port_password":{
"8388":"password1",
"8389":"password2",
"8390":"password3"
},
"timeout":300,
"method":"aes-256-gcm",
"fast_open":false
}
流量限制
使用iptables限制单个IP的连接数和带宽(可选):
# 限制单个IP最大连接数 iptables -A INPUT -p tcp --dport 8388 -m connlimit --connlimit-above 20 -j DROP # 限制单个IP的带宽 tc qdisc add dev eth0 root handle 1: htb default 20 tc class add dev eth0 parent 1: classid 1:1 htb rate 10mbit tc filter add dev eth0 parent 1: protocol ip prio 1 handle 1 fw classid 1:1 iptables -A OUTPUT -t mangle -p tcp --dport 8388 -j MARK --set-mark 1
安全加固
修改SSH端口
nano /etc/ssh/sshd_config
找到#Port 22,取消注释并修改为其他端口,如:
Port 2222
重启SSH服务:
systemctl restart sshd
禁用密码登录,使用SSH密钥
nano /etc/ssh/sshd_config
修改以下参数:
PasswordAuthentication no
PubkeyAuthentication yes
安装fail2ban防止暴力破解
apt install fail2ban -y systemctl enable fail2ban systemctl start fail2ban
常见问题解决
连接不上
检查步骤:
- 确认服务器防火墙开放了SS端口
- 确认Shadowsocks服务正在运行
- 测试端口是否可达:
telnet your_server_ip 8388 - 检查客户端配置是否正确
速度慢
可能原因:
- 服务器地理位置不佳
- 服务器带宽不足
- 网络线路问题
解决方案:
- 更换服务器机房
- 升级服务器带宽
- 尝试不同的加密方式
突然无法连接
可能原因:
- IP被封锁
- 端口被封锁
解决方案:
- 更换服务器端口
- 考虑使用SS + 简单混淆插件
进阶方案
使用Shadowsocks-libev
Shadowsocks-libev是原版SS的C语言实现,性能更好:
安装:
apt install shadowsocks-libev -y
安装obfs插件绕过检测
apt install simple-obfs -y
修改配置文件:
{
"server":"0.0.0.0",
"server_port":8388,
"password":"your_password",
"timeout":300,
"method":"aes-256-gcm",
"plugin":"obfs-server",
"plugin_opts":"obfs=http"
}
客户端也需要相应配置支持obfs插件。
通过本文的详细指导,你应该已经成功搭建了自己的Shadowsocks科学上网服务,作为通信工程师,我建议定期更新服务器系统和Shadowsocks软件,关注安全公告,确保服务稳定安全,自建SS服务不仅能提供更好的网络体验,还能让你深入了解网络通信原理,对技术成长大有裨益。









