0%

Ubuntu 18.04搭建shadowsocks server

1. 系统安装

打开kvm配置界面,先stop系统,然后找到Install new OS入口

然后选择对应系统版本,此处选择的系统是 Ubuntu 18.04 x86_64 ,如下图

勾选许可协议,点击Reload即可开始安装,一般几分钟内完成

2. 搭建Shadowsocks服务器

2.1 安装 pip

以Python3为载体,安装pip3

1
sudo apt install python3-pip

2.2 安装Shadowsocks

因Shadowsocks作者不再维护pip中的Shadowsocks(定格在了2.8.2),我们使用下面的命令来安装最新版的Shadowsocks:

1
pip3 install https://github.com/shadowsocks/shadowsocks/archive/master.zip

安装完成后可以使用下面这个命令查看Shadowsocks版本:

1
sudo ssserver --version

目前会显示“Shadowsocks 3.0.0”

2.3 创建配置文件

创建Shadowsocks配置文件所在文件夹:

1
sudo mkdir /etc/shadowsocks

然后创建配置文件:

Bash

1
sudo vim /etc/shadowsocks/config.json

复制粘贴如下内容,password即为所需设置的密码,自行修改:

1
2
3
4
5
6
7
8
9
10
{
"server": "0.0.0.0",
"server_port": 你想设置的端口,
"local_address": "127.0.0.1",
"local_port": 1080,
"password": "password",
"timeout": 300,
"method": "rc4-md5",
"fast_open": true
}

保存后退出,然后测试Shadowsocks是否能正常工作:

1
ssserver -c /etc/shadowsocks/config.json

2.4 配置Systemd管理Shadowsocks

新建Shadowsocks管理文件

1
sudo vim /etc/systemd/system/shadowsocks-server.service

复制粘贴:

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=Shadowsocks Server
After=network.target

[Service]
ExecStart=/usr/local/bin/ssserver -c /etc/shadowsocks/config.json
Restart=on-abort

[Install]
WantedBy=multi-user.target

保存 退出。

重载shadowsocks-server.service:

1
sudo systemctl daemon-reload

设置shadowsocks-server.service开机自启

1
sudo systemctl enable /etc/systemd/system/shadowsocks.service

重启Shadowsocks-server.service:

1
sudo systemctl restart shadowsocks-server

2.5 开启BBR

BBR系Google最新开发的TCP拥塞控制算法,目前有着较好的带宽提升效果,甚至不比老牌的锐速差。

BBR在Linux kernel 4.9引入。首先检查服务器kernel版本:

1
uname -r

显示版本为: 4.15.0-20-generic,无需升级Linux内核。

运行lsmod | grep bbr,如果结果中没有tcp_bbr,则先运行:

1
2
modprobe tcp_bbr
echo "tcp_bbr" >> /etc/modules-load.d/modules.conf

运行:

1
2
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf

运行:

1
sysctl -p

保存生效。运行:

1
2
sysctl net.ipv4.tcp_available_congestion_control
sysctl net.ipv4.tcp_congestion_control

若均有bbr,则开启BBR成功。

2.6 优化吞吐量

新建配置文件:

1
sudo nano /etc/sysctl.d/local.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
34
35
36
37
38
39
40
# max open files
fs.file-max = 51200
# max read buffer
net.core.rmem_max = 67108864
# max write buffer
net.core.wmem_max = 67108864
# default read buffer
net.core.rmem_default = 65536
# default write buffer
net.core.wmem_default = 65536
# max processor input queue
net.core.netdev_max_backlog = 4096
# max backlog
net.core.somaxconn = 4096
# resist SYN flood attacks
net.ipv4.tcp_syncookies = 1
# reuse timewait sockets when safe
net.ipv4.tcp_tw_reuse = 1
# turn off fast timewait sockets recycling
net.ipv4.tcp_tw_recycle = 0
# short FIN timeout
net.ipv4.tcp_fin_timeout = 30
# short keepalive time
net.ipv4.tcp_keepalive_time = 1200
# outbound port range
net.ipv4.ip_local_port_range = 10000 65000
# max SYN backlog
net.ipv4.tcp_max_syn_backlog = 4096
# max timewait sockets held by system simultaneously
net.ipv4.tcp_max_tw_buckets = 5000
# turn on TCP Fast Open on both client and server side
net.ipv4.tcp_fastopen = 3
# TCP receive buffer
net.ipv4.tcp_rmem = 4096 87380 67108864
# TCP write buffer
net.ipv4.tcp_wmem = 4096 65536 67108864
# turn on path MTU discovery
net.ipv4.tcp_mtu_probing = 1

net.ipv4.tcp_congestion_control = bbr

运行:

1
sysctl --system

编辑之前的shadowsocks-server.service文件:

1
sudo nano /etc/systemd/system/shadowsocks-server.service

ExecStart前插入一行,内容为:

1
ExecStartPre=/bin/sh -c 'ulimit -n 51200'

即修改后的shadowsocks-server.service内容为:

1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=Shadowsocks Server
After=network.target

[Service]
ExecStartPre=/bin/sh -c 'ulimit -n 51200'
ExecStart=/usr/local/bin/ssserver -c /etc/shadowsocks/config.json
Restart=on-abort

[Install]
WantedBy=multi-user.target

重载shadowsocks-server.service:

1
sudo systemctl daemon-reload

重启Shadowsocks:

1
sudo systemctl restart shadowsocks-server

整个配置过程完成。

坚持原创技术分享,您的支持将鼓励我继续创作!