Ubuntu CentOS 7 源代码构建 Shadowsocks libev Servers

说明:本文相关配置和说明仅用于示例和操作指引,本站(任我乐)不对相关操作结果及由此产生的问题负责。

介绍:

Shadowsocks libev是目前唯一一个持续更新的版本,其他版本由于某些原因作者已经停止开发。Shadowsocks是一个轻量级的SOCKS5网络代理工具。与其他代理软件不同的是,Shadowsocks的流量被设计为从其他流量到第三方监控工具都难以分辨,并且能够将自己伪装成正常的直接连接。通过Shadowsocks传递的数据被加密,以提高安全性和隐私。

由于目前UbuntuCentOS没有Shadowsocks软件包,因此本指南将介绍如何从源代码构建Shadowsocks服务器。

说明:下面章节操作写了UbuntuCentOS的配置方案。本文概要说明在 CentOS 系统下的 Shadowsocks 配置。

1.安装 Shadowsocks-libev 服务器

项目地址://github.com/shadowsocks

更新系统存储库,下载并安装依赖扩展包:

CentOS 7.*:

$ yum update && yum upgrade -y
$ yum install epel-release -y
$ yum install -y gcc gettext autoconf libtool automake make pcre-devel asciidoc xmlto udns-devel libev-devel libsodium-devel mbedtls-devel git m2crypto c-ares-devel

Ubuntu 17.04:

$ apt-get update && apt-get upgrade -yuf
$ apt-get install -y --no-install-recommends gettext build-essential autoconf libtool libpcre3-dev asciidoc xmlto libev-dev libudns-dev automake libmbedtls-dev libsodium-dev git python-m2crypto c-ares-devel

在/mnt目录下载Shadowsocks Git模块:

$ cd /mnt
$ git clone //github.com/shadowsocks/shadowsocks-libev.git

Cloning into 'shadowsocks-libev'...
remote: Counting objects: 11147, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 11147 (delta 0), reused 3 (delta 0), pack-reused 11142
Receiving objects: 100% (11147/11147), 9.22 MiB | 71.00 KiB/s, done.
Resolving deltas: 100% (7305/7305), done.

$ cd shadowsocks-libev
$ git submodule update --init --recursive

开始安装:

$ ./autogen.sh
$ ./configure
$ make -j4 && make install

2.配置 Shadowsocks-libev 服务端

为 Shadowsocks 创建一个新的系统用户:

CentOS 7.*:

$ groupadd shadowsocks
$ useradd -g shadowsocks -s /bin/false shadowsocks

Ubuntu 17.04:

$ adduser --system --no-create-home --group shadowsocks

为配置文件创建一个新目录:

$ mkdir -m 755 /etc/shadowsocks-libev

创建Shadowsocks配置文件,将下面列出的内容粘贴到文件中:

$ vim /etc/shadowsocks-libev/shadowsocks.json

{
    "server":"My Server IP",
    "server_port":8388,
    "local_address": "127.0.0.1",
    "local_port":1080,
    "password":"mypassword",
    "timeout":300,
    "method":"aes-256-gcm",
    "fast_open": true
}

核心参数说明:

server:服务端监听地址(IPv4或IPv6)。
server_port:服务端端口,一般为443。
local_address:本地监听地址,缺省为127.0.0.1 可用-b参数设置。
local_port:本地监听端口,一般为1080。
password:用以加密的密匙。
timeout:超时时间(秒)。
method:加密方法。推荐使用AEAD算法。
fast_open:打开可以减少延迟。适用于系统内核kernel versions 3.7.1或更高版本。

3.优化 Shadowsocks libev 服务器

创建系统优化文件并将下面显示的内容粘贴到文件中。这些设置为Shadowsocks提供了最佳的内核配置。如果以前由于某种原因配置了系统内核设置,请确保不存在冲突。

$ vim /etc/sysctl.d/local.conf

fs.file-max = 51200
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.core.rmem_default = 65536
net.core.wmem_default = 65536
net.core.netdev_max_backlog = 4096
net.core.somaxconn = 4096

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_rmem = 4096 87380 67108864
net.ipv4.tcp_wmem = 4096 65536 67108864
net.ipv4.tcp_mtu_probing = 1

net.ipv4.tcp_congestion_control = hybla

net.ipv4.tcp_congestion_control = cubic

可选:

建议开启BBR拥塞算法优化。请参阅《CentOS 7系统开启BBR拥塞算法优化TCP加速服务器网络》。

应用优化生效:

$ sysctl --system

4.创建一个Shadowsocks Systemd服务单元文件

Shadowsocks systemd服务允许守护进程在系统启动时自动启动并在后台运行。

$ vim /etc/systemd/system/shadowsocks.service

[Unit]
Description=Shadowsocks proxy server

[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/bin/ss-server -c /etc/shadowsocks-libev/shadowsocks.json -a shadowsocks -v start
ExecStop=/usr/local/bin/ss-server -c /etc/shadowsocks-libev/shadowsocks.json -a shadowsocks -v stop

[Install]
WantedBy=multi-user.target

加入开机自起并启动:

$ systemctl daemon-reload
$ systemctl enable shadowsocks
$ systemctl start shadowsocks

5.为 Shadowsocks 客户端打开防火墙端口

根据您的偏好,您可以使用iptablesUFWFirewallD命令来完成本节:

Iptables:

$ iptables -4 -A INPUT -p tcp --dport 8388 -m comment --comment "Shadowsocks server listen port" -j ACCEPT

UFW:

$ ufw allow proto tcp to 0.0.0.0/0 port 8388 comment "Shadowsocks server listen port"

FirewallD:

$ firewall-cmd --permanent --zone=public --add-rich-rule=' rule family="ipv4" port protocol="tcp" port="8388" accept'
$ firewall-cmd --reload

ShadowSocks-libev 的服务端到目前为止已经完成安装。

关于Shadowsocks客户端的使用这里不在叙述。

Ubuntu CentOS 7 源代码构建 Shadowsocks libev Servers》有10个想法

  1. guoliang

    你好,我配置完成后,能正常访问 google, 但是 VPS 的 磁盘占用非常快, 1个多小时 10G 就占满了。 我 VPS 上 配置为 Centos 7 x64 运行 ss bbr 加速,这是什么原因呢?

    回复
  2. mmmm

    你好,请问VPS主机新建的话,可以按照你的步骤搭建SS服务器吗?就是按照你的步骤做完,就完整搭建好了SS-LIBEV服务器了吗?只需要下载SS客户端就可以用了?

    回复
      1. mmmm

        你好,博主,我按照你的方法搭建完毕,CENTOS 7 64位的VPS主机,有一步报错。。是在加入开机自启动的第二步。。$ systemctl enable shadowsocks,这一步,提示 Bad message。。然后用SS客户端打开PAC代理模式,访问谷歌,报错。。
        500 Internal Privoxy Error
        Privoxy encountered an error while processing your request:

        Could not load template file no-server-data or one of its included components.

        Please contact your proxy administrator.

        If you are the proxy administrator, please put the required file(s)in the (confdir)/templates directory. The location of the (confdir) directory is specified in the main Privoxy config file. (It’s typically the Privoxy install directory).

        不知道是什么原因,因为我是第一次搭建SS,对LINUX也不是很熟悉。。所以请多指教我。。感谢。。

        回复
          1. mmmm

            这个已经搞定了,但是还有些问题想请教你,请问你能加一下我的QQ或者微信吗,微信号 mengx911,万分感谢

            回复

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code