由于喜迎国家会议的召开,方便使用 Google 和 GitHub,特此记录搭建及优化 Shadowsocks 服务、服务器简单加固的过程。
本教程仅适用于 Ubuntu 16.04 及更高版本,基于 Python 3 ,使用 Systemd 管理 Shadowsocks 服务,优化部分包括 BBR 、TCP Fast Open 以及系统吞吐量的配置。
0x01 安装
首先安装 Python3 以及 pip3 。
1
| apt install python3-pip vim
|
创建配置文件
1
2
| mkdir /etc/shadowsocks
vim /etc/shadowsocks/config.json
|
vim 的操作,按 a 键进入输入模式,复制粘贴如下内容后,按下 :x 回车即可。
配置文件内容如下,注意将 mypassword 换成自己的密码。
1
2
3
4
5
6
7
8
9
10
| {
"server":"::",
"server_port":443,
"local_address": "127.0.0.1",
"local_port":1080,
"password":"mypassword",
"timeout":300,
"method":"aes-256-cfb",
"fast_open": false
}
|
运行 SS
1
2
3
| curl ip.cn
// 当前 IP:8.8.8.8 来自:美国 Google
ssserver -c /etc/shadowsocks/config.json
|
在 Shadowsocks 客户端添加服务器,如果你使用的是我提供的那个配置文件的话,地址填写你的 IP 地址,端口号为 443,加密方法为 aes-256-cfb ,密码为你设置的密码。然后设置客户端使用全局模式,浏览器访问 www.youtube.com 可以直接打开了。
此时,访问 https://www.ipip.net/ ,可以看到自己的 ip 是服务器的 ip 了。
测试完毕,按 Ctrl + C 关闭 Shadowsocks。
配置 Systemd 管理 Shadowsocks
1
| vim /etc/systemd/system/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
|
保存完之后我们键入以下命令:
1
2
3
4
5
| # 开启 Shadowsocks 服务
systemctl start shadowsocks-server
# 开机自动启动 Shadowsocks 服务
systemctl enable shadowsocks-server
|
0x02 优化
开启 BBR
BBR 是 Google 最新开发的 TCP 堵塞控制算法,目前有着较好的带宽提升效果,甚至不比老牌的锐速差。
BBR 是在 Linux kernel 4.9 时引入的,首先我们需要检查服务器 kernel 版本:
如果其显示版本在 4.9.0 之下,则需要升级 Linux 内核,否则请跳过这一部分内容。
1
2
| apt update
apt-cache showpkg linux-image
|
找到一个你想要升级的 Linux 内核版本,如 linux-image-4.10.0-22-generic 。
1
2
| apt install linux-image-4.10.0-22-generic
reboot
|
重启之后我们再输入,删除旧内核。
升级完内核之后,我们开始设置 BBR 。
运行 lsmod | grep bbr,如果结果中没有 tcp_bbr,则先运行:
1
2
3
4
5
| modprobe tcp_bbr
echo "tcp_bbr" >> /etc/modules-load.d/modules.conf
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p
|
保存生效。运行:
1
2
| sysctl net.ipv4.tcp_available_congestion_control
sysctl net.ipv4.tcp_congestion_control
|
若均有 bbr,则开启 BBR 成功。
优化吞吐量
键入以下命令新建一个文件:
1
| vim /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
41
| # 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
|
最后键入以下命令刷新系统配置:
至此,我们所有的优化就做完了,最后开启对我们服务器的保护。
0x03 防火墙
运行如下命令,安装 iptables-persistent 帮助我们持久化设置:
1
2
| apt-get update
apt-get install iptables-persistent
|
编辑 IPv4 的 iptables 规则文件:
1
| vim /etc/iptables/rules.v4
|
按照以下提示插入规则文件即可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| # Generated by DaveX
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
// 添加以下内容
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
-A FORWARD -p tcp --syn -m limit --limit 1/s --limit-burst 5 -j ACCEPT
-A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
// --- [ End ] ---
COMMIT
# Completed in 2017
|
保存完文件之后我们重启服务器,使配置生效。
以上,就是我们搭建及优化一个安全的私人 Shadowsocks 服务的全过程,接下来就是享受探索外网知识的乐趣了。
0x04 Reference
Ubuntu 16.04 下 Shadowsocks 服务器端安装及优化
Mark as Complete How To Implement a Basic Firewall Template with Iptables on Ubuntu 14.04
使用 iptables 防止 DDos 攻击