CentOS 7 安装 shadowsocks-libev 教程

发布:elantion 日期:2018-07-11 阅读:12070 评论:3

shadowsocks-libev占用内存小,效率高,是个很不错的工具,但安装过程有点烦,特别是没有针对cent os的教程,好不容易研究通了,所以记录下来。以下是针对3.13版本,以后重装或更新,我也会在这儿更新一下。

编译安装

按照官方的教程一步一步写命令即可:官方教程(英文) ,其中最麻烦的是编译过程有很多依赖,要一个一个地检查确认,相当复杂,为了简化这个流程,我弄了个脚本,直接运行即可安装成功,有兴趣试试:

#!/bin/bash
# install dependencies
yum update -y
yum install -y pcre pcre-devel git gettext gcc autoconf libtool automake make asciidoc xmlto c-ares-devel libev-devel

export LIBSODIUM_VER=1.0.13
wget https://download.libsodium.org/libsodium/releases/libsodium-$LIBSODIUM_VER.tar.gz
tar xvf libsodium-$LIBSODIUM_VER.tar.gz
pushd libsodium-$LIBSODIUM_VER
./configure --prefix=/usr && make
make install
popd
ldconfig

export MBEDTLS_VER=2.6.0
wget https://tls.mbed.org/download/mbedtls-$MBEDTLS_VER-gpl.tgz
tar xvf mbedtls-$MBEDTLS_VER-gpl.tgz
pushd mbedtls-$MBEDTLS_VER
make SHARED=1 CFLAGS=-fPIC
make DESTDIR=/usr install
popd
ldconfig

# clean directory
rm -rf /tmp/shadowsocks-libev

# compile and install
cd /tmp
git clone https://github.com/shadowsocks/shadowsocks-libev.git
cd shadowsocks-libev
git submodule update --init --recursive
./autogen.sh
./configure
make
make install
cd ..
rm -rf /tmp/shadowsocks-libev

配置

配置文件放到/etc/shadowsocks-libev/config.json,参考下面样式即可:

{
    "server_host": "你的服务器ip或域名",
    "server_port": 8111,
    "password":"密码",
    "timeout":300,
    "method":"rc4-md5",
    "fast_open": false
}

直接启动

直接输入命令ss-server 即可,但并不能直接在后台启动,需要额外配置,请接着看下面。

配置服务

添加默认环境变量文件到/etc/default/shadowsocks-libev

https://github.com/shadowsocks/shadowsocks-libev/blob/master/debian/shadowsocks-libev.default

# Defaults for shadowsocks initscript
# sourced by /etc/init.d/shadowsocks-libev
# installed at /etc/default/shadowsocks-libev by the maintainer scripts

#
# This is a POSIX shell fragment
#
# Note: `START', `GROUP' and `MAXFD' options are not recognized by systemd.
# Please change those settings in the corresponding systemd unit file.

# Enable during startup?
START=yes

# Configuration file
CONFFILE="/etc/shadowsocks-libev/config.json"

# Extra command line arguments
DAEMON_ARGS="-u"

# User and group to run the server as
USER=nobody
GROUP=nobody

# Number of maximum file descriptors
MAXFD=32768

要注意的是,github链接上的代码GROUP的值是nodgroup,那是因为那个脚本是给debain用的,而不是centos,所以这里要改改,直接拷贝上面的代码即可。

然后添加服务脚本到/lib/systemd/system/shadowsocks-libev.service

https://github.com/shadowsocks/shadowsocks-libev/blob/master/debian/shadowsocks-libev.service

#  This file is part of shadowsocks-libev.
#
#  Shadowsocks-libev is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This file is default for Debian packaging. See also
#  /etc/default/shadowsocks-libev for environment variables.

[Unit]
Description=Shadowsocks-libev Default Server Service
Documentation=man:shadowsocks-libev(8)
After=network.target

[Service]
Type=simple
EnvironmentFile=/etc/default/shadowsocks-libev
User=nobody
Group=nobody
LimitNOFILE=32768
ExecStart=/usr/local/bin/ss-server -c $CONFFILE $DAEMON_ARGS

[Install]
WantedBy=multi-user.target

这里要注意两个点,一个跟上面的文件一样要把Group的值改为nobody,另外一个是ExecStart字段,它是执行文件的命令,所以要把ss-server的地址写对,如果你跟我一样是从源码编译的,这里可能要加上local,像上面那样。

这两个文件弄好之后,执行systemctl start shadowsocks-libev 即可启动服务。

debug

如果想知道当前服务的运行情况,执行systemctl status shadowsocks-libev -f,就可以看到当前服务的运行情况。如果想重启,则运行systemctl restart shadowsocks-libev,或者运行systemctl stop shadowsocks-libev来停止当前服务。
如果改变了服务文件/lib/systemd/system/shadowsocks-libev.service,则需要先执行 systemctl daemon-reload,然后再执行 systemctl restart shadowsocks-libev才能重启成功。如果需望开机启动,执行systemctl enable shadowsocks-libev 即可。

其它

1、windows版本ss下载地址:https://github.com/shadowsocks/shadowsocks-windows/releases
2、Mac版本ss下载地址:https://github.com/shadowsocks/ShadowsocksX-NG/releases

问题汇总

1、ppop拔号上网不能正常使用
答:把拔号名称改成英文或数字后,重启电脑即可。
2、需要改一下默认的端口吗?
答:最好改一下,现在国墙好像会自动扫描默认的端口了,为了不被墙,最好自已弄一个,最好别用我这个,免得国墙又发现了。。。

后语

这玩意安装配置并不简单,最主要原因是没有针对cent os的文档,踩了不少坑,但看着只有600k的内存占用,这点苦又算什么。好了,大家开心地写代码吧,有问题请留言。