前言

  利用树莓派搭建一个低功耗、低成本的BT下载服务器是一个不错的选择,IO性能也不算特别差劲,而且有4个USB口可以用来挂载移动存储设备,还有一个千兆口来提供高速网络传输。
  Transmission是一个跨平台的BitTorrent客户端,采用C编写,内存占用也非常低,重要的是,它具有一个Web控制界面,可以利用Nginx反向代理来更安全的访问。




安装环境




开始安装

安装所需依赖库

sudo apt-get install build-essential automake autoconf libtool pkg-config intltool libcurl4-openssl-dev libglib2.0-dev libevent-dev libminiupnpc-dev libgtk-3-dev libappindicator3-dev

编译并安装transmission-daemon

cd ~
wget -c -O transmission-2.94.tar.xz  https://github.com/transmission/transmission-releases/raw/master/transmission-2.94.tar.xz -T 5
tar xvf transmission-2.94.tar.xz
cd transmission-2.94
./configure
make
sudo make install

配置Transmission
先切换到root用户,然后运行一次transmission-daemon来生成配置文件,等待5s左右再杀掉进程。

sudo -s
transmission-daemon
killall transmission-daemon

进入~/.config/transmission-daemon目录下,编辑settings.json配置文件,主要配置download-dirrpc-portrpc-whitelist这几个键的值。

{
    "download-dir": "/root/download",
    "rpc-port": 9091,
    "rpc-whitelist": "127.0.0.1,192.168.*.*"
}

download-dir为下载目录,一般设置为硬盘的挂载目录;rpc-port为Web界面的端口,一般默认即可;rpc-whitelist为Web界面的白名单,在白名单内的IP或IP段允许访问Web界面。配置好后再次运行即可生效和使用。

transmission-daemon

注意:编辑配置文件前需要结束该进程才能使其配置生效,其他键值的具体配置请查阅官方文档




进阶

  每次启动树莓派都需要手动运行transmission太麻烦了,可通过添加服务脚本到/etc/init.d/目录下来实现开机自启动。脚本如下:

#!/bin/bash

### BEGIN INIT INFO
# Provides:           transmission
# Required-Start:     $all
# Required-Stop:      $all
# Default-Start:      2 3 4 5
# Default-Stop:       0 1 6
# Short-Description:  Run transmission-daemon
# Description:        Run transmission-daemon
### END INIT INFO

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
PROC_NAME=transmission-daemon
ProcNumber=`ps -ef |grep -w $PROC_NAME|grep -v grep|wc -l`
export PATH

case "$1" in
    start)
        if [ $ProcNumber -le 0 ];then
            echo -e "\033[33mtransmission-daemon will start...\033[0m"
            transmission-daemon -e /var/transmission.log -g ~/.config/transmission-daemon
            sleep 1
            echo -e "\033[32mtransmission-daemon is started.\033[0m"
        else
            runningPid=$(ps aux | grep transmission-daemon| grep -v grep | awk '{print $2}')
            echo -e "\033[33mtransmission-daemon(pid $runningPid) is running.\033[0m"
        fi
        ;;
    stop)
        if [ $ProcNumber -le 0 ];then
            echo -e "\033[33mtransmission-daemon is not run.\033[0m"
        else
            echo -e "\033[33mtransmission-daemon will stop.\033[0m"
            killall transmission-daemon
            sleep 1
            echo -e "\033[31mtransmission-daemon is stoped.\033[0m"
        fi
        ;;
    restart)
        if [ $ProcNumber -le 0 ];then
            echo -e "\033[33mtransmission-daemon is not run.Will start...\033[0m"
            transmission-daemon -e /var/transmission.log -g ~/.config/transmission-daemon
            sleep 1
            echo -e "\033[32mtransmission-daemon is started.\033[0m"
        else
            echo -e "\033[33mtransmission-daemon will restart...\033[0m"
            killall transmission-daemon
            transmission-daemon -e /var/transmission.log -g ~/.config/transmission-daemon
            sleep 1
            echo -e "\033[32mtransmission-daemon is restarted.\033[0m"
        fi
        ;;
    status)
        if [ $ProcNumber -le 0 ];then
            echo -e "\033[33mtransmission-daemon is not run.\033[0m"
        else
            pid=$(ps aux | grep transmission-daemon| grep -v grep | awk '{print $2}')
            logFileIndex=$(ps aux | grep transmission-daemon| grep -v grep | awk '{print $13}')
            configDirIndex=$(ps aux | grep transmission-daemon| grep -v grep | awk '{print $15}')
            echo -e "\033[32mtransmission-daemon(pid $pid) is running.\033[0m"
            echo -e "=================================================================="
            echo -e "Log file: $logFileIndex"
            echo -e "Config dir: $configDirIndex"
            echo -e "=================================================================="
        fi
        ;;
    *)
        echo "Usage: /etc/init.d/transmission <start|stop|restart|status>"
        ;;
esac
exit 0

由于是root用户执行该脚本的,所以需要注意配置文件的目录实际为/root/.config/transmission-daemon。把该脚本放到/etc/init.d/目录下,并命名为transmission

最后把该脚本添加到开机启动服务中:

sudo cd /etc/init.d/
sudo update-rc.d transmission defaults 90

重启即可看到效果。