安装rTorrent心得

什么是rTorrent

rTorrent是一个非常简洁、优秀、非常轻量的命令行BT下载客户端,和Aria2一样,既可以使用命令行下载,也可以配合自己的Web前端进行控制操作。很多都喜欢用rTorrent配合前端ruTorrent一起使用,毕竟有个图形界面方便些,不过配置起来挺麻烦的,博主觉得单独使用rtorrent下载BT文件速度还可以,操作也不难,关键安装很简单,很适合临时下载BT文件的人。

rTorrent的优缺点

优点:

  • 性能极高,资源占用少(相当于transmission资源消耗,却拥有不不弱于qbittorrent的速度)
  • 包含丰富的命令,对于保种、做种有极大的自由度

缺点:

  • 安装配置极其复杂

版本选择

有两版本分别是官方版jesec,后者提供了更加友好的面向用户的其他功能、优化以及与 RPC 接口的现代用户的更好集成;这里主要讨论jesec的版本。

安装rTorrent

安装完全静态的二进制文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Install rTorrent to /usr/local/bin/rtorrent
# rtorrent-linux-amd64 and rtorrent-linux-arm64 are available
sudo wget https://github.com/jesec/rtorrent/releases/latest/download/rtorrent-linux-amd64 -O /usr/local/bin/rtorrent

# Make it executable
sudo chmod +x /usr/local/bin/rtorrent

# Default configuration

sudo mkdir -p /etc/rtorrent
sudo wget https://github.com/jesec/rtorrent/releases/latest/download/rtorrent.rc -O /etc/rtorrent/rtorrent.rc

# Install as a systemd service (optional)
# This example uses "download" user. Replace it with the an existing user that rTorrent should run with.
sudo wget https://github.com/jesec/rtorrent/releases/latest/download/rtorrent@.service -O /etc/systemd/system/rtorrent@.service
sudo systemctl daemon-reload
sudo systemctl enable rtorrent@download
sudo systemctl start rtorrent@download

其它安装方式,请自行搜索。

配置

rTorrent 尝试从多个位置加载配置文件:

  • $XDG_CONFIG_HOME/rtorrent/rtorrent.rc (最高优先级)
  • $HOME/.config/rtorrent/rtorrent.rc
  • $HOME/.rtorrent.rc
  • /etc/rtorrent/rtorrent.rc (最低优先级)

配置模板

内核调整

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
# /etc/security/limits.conf
* - nofile 51200

# /etc/sysctl.conf (XanMod Kernel)
vm.swappiness=10

net.core.default_qdisc=fq_pie
net.ipv4.tcp_congestion_control=bbr2

# Maximum Socket Receive Buffer. 16MB per socket - which sounds like a lot, but will virtually never consume that much. Default: 212992
net.core.rmem_max = 16777216
# Maximum Socket Send Buffer. 16MB per socket - which sounds like a lot, but will virtually never consume that much. Default: 212992
net.core.wmem_max = 16777216
# Increase the write-buffer-space allocatable: min 4KB, def 12MB, max 16MB. Default: 4096 16384 4194304
net.ipv4.tcp_wmem = 4096 12582912 16777216
# Increase the read-buffer-space allocatable: min 4KB, def 12MB, max 16MB. Default: 4096 16384 4194304
net.ipv4.tcp_rmem = 4096 12582912 16777216

# Tells the system whether it should start at the default window size only for new TCP connections or also for existing TCP connections that have been idle for too long. Default: 1
net.ipv4.tcp_slow_start_after_idle = 0
# Allow reuse of sockets in TIME_WAIT state for new connections only when it is safe from the network stack’s perspective. Default: 0
net.ipv4.tcp_tw_reuse = 1
# Do not last the complete time_wait cycle. Default: 0
# only works from Linux 2.4 to 4.11
net.ipv4.tcp_tw_recycle = 1
# Minimum time a socket will stay in TIME_WAIT state (unusable after being used once). Default: 60
net.ipv4.tcp_fin_timeout = 30

我的配置

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#############################################################################
# A minimal rTorrent configuration that provides the basic features
#############################################################################

# Some default configs are commented out by #, you can override them to fit your needs
# Lines commented out by ## are merely examples (NOT default)

# It is recommended to extend upon this default config file. For example:
# override only some configs via command line: -o network.port_range.set=6881-6881
# or, on top of custom config: import = /etc/rtorrent/rtorrent.rc

# Some additional values and commands
method.insert = system.startup_time, value|const, (system.time)

# rTorrent runtime directory (cfg.basedir) [default: "$HOME/.local/share/rtorrent"]
method.insert = cfg.basedir, private|const|string, (cat,(fs.homedir),"/rtorrent/")

# Default download directory (cfg.download) [default: "$(cfg.basedir)/download"]
#method.insert = cfg.download, private|const|string, (cat,(cfg.basedir),"download/")
method.insert = cfg.download, private|const|string, (cat,"/home/data/下载/")

# Log directory (cfg.logs) [default: "$(cfg.basedir)/log"]
method.insert = cfg.logs, private|const|string, (cat,(cfg.basedir),"log/")
method.insert = cfg.logfile, private|const|string, (cat,(cfg.logs),"rtorrent-",(system.time),".log")

# Torrent session directory (cfg.session) [default: "$(cfg.basedir)/.session"]
method.insert = cfg.session, private|const|string, (cat,(cfg.basedir),".session/")

# Watch (drop to add) directories (cfg.watch) [default: "$(cfg.basedir)/watch"]
method.insert = cfg.watch, private|const|string, (cat,(cfg.basedir),"watch/")

# Create directories
fs.mkdir.recursive = (cat,(cfg.basedir))

fs.mkdir = (cat,(cfg.download))
fs.mkdir = (cat,(cfg.logs))
fs.mkdir = (cat,(cfg.session))

fs.mkdir = (cat,(cfg.watch))
fs.mkdir = (cat,(cfg.watch),"/load")
fs.mkdir = (cat,(cfg.watch),"/start")

# Drop to "$(cfg.watch)/load" to add torrent
schedule2 = watch_load, 11, 10, ((load.verbose, (cat, (cfg.watch), "load/*.torrent")))

# Drop to "$(cfg.watch)/start" to add torrent and start downloading
schedule2 = watch_start, 10, 10, ((load.start_verbose, (cat, (cfg.watch), "start/*.torrent")))

# Listening port for incoming peer traffic
network.port_range.set = 51418-51418
network.port_random.set = no

# Distributed Hash Table and Peer EXchange
# Enable tracker-less torrents but vulnerable to passive sniffing
# DHT and PEX are always disabled for private torrents
#dht.mode.set = auto
#dht.port.set = 51418
#protocol.pex.set = yes
dht.mode.set = disable
protocol.pex.set = no

# DHT nodes for bootstrapping
dht.add_bootstrap = dht.transmissionbt.com:6881
dht.add_bootstrap = dht.libtorrent.org:25401

# UDP tracker support
#trackers.use_udp.set = yes
trackers.use_udp.set = no

# Peer settings
throttle.max_downloads.set = 100
throttle.max_uploads.set = 100
throttle.max_downloads.global.set = 300
throttle.max_uploads.global.set = 300
throttle.min_peers.normal.set = 100
throttle.max_peers.normal.set = 200
throttle.min_peers.seed.set = 100
throttle.max_peers.seed.set = 0
trackers.numwant.set = 100

#protocol.encryption.set = allow_incoming,try_outgoing,enable_retry

# Limits for file handle resources, this is optimized for
# an `ulimit` of 1024 (a common default). You MUST leave
# a ceiling of handles reserved for rTorrent's internal needs!
network.max_open_files.set = 10240
network.max_open_sockets.set = 500

# Send and receive buffer size for socket. Disabled by default (`0`), this means the default is used by OS
# (you have to modify the system wide settings!) (`send_buffer_size`, `receive_buffer_size`)
# Increasing buffer sizes may help reduce disk seeking, connection polling as more data is buffered each time
# the socket is written to. It will result higher memory usage (not visible in rtorrent process!).
network.receive_buffer.size.set = 4M
network.send_buffer.size.set = 12M

# Memory resource usage (increase if you have a large number of items loaded,
# and/or the available resources to spend)
pieces.memory.max.set = 1800M
#network.xmlrpc.size_limit.set = 16M

# Preallocate disk space for contents of a torrent
#
# Useful for reducing fragmentation, improving the performance
# and I/O patterns of future read operations. However, with this
# enabled, preallocated files will occupy the full size even if
# they are not completed.
#
# If you choose to allocate space for the whole torrent at once,
# rTorrent will create all files and allocate the space when the
# torrent is started. rTorrent will NOT delete the file and free
# the allocated space, if you later mark a file as DO NOT DOWNLOAD.
#
# 0 = disabled
# 1 = enabled, allocate when a file is opened for write
# 2 = enabled, allocate the space for the whole torrent at once
system.file.allocate.set = 2

# Basic operational settings
session.path.set = (cat, (cfg.session))
directory.default.set = (cat, (cfg.download))
log.execute = (cat, (cfg.logs), "execute.log")
##log.xmlrpc = (cat, (cfg.logs), "xmlrpc.log")

# Other operational settings
encoding.add = utf8
system.umask.set = 0027
system.cwd.set = (directory.default)
#schedule2 = low_diskspace, 5, 60, ((close_low_diskspace, 500M))
#pieces.hash.on_completion.set = no
pieces.hash.on_completion.set = no
##view.sort_current = seeding, greater=d.ratio=
##keys.layout.set = qwerty

# HTTP and SSL
network.http.max_open.set = 50
network.http.dns_cache_timeout.set = 25

# Path to the CA bundle. By default, rTorrent tries to detect from:
# $RTORRENT_CA_BUNDLE (highest priority)
# $CURL_CA_BUNDLE
# $SSL_CERT_FILE
# /etc/ssl/certs/ca-certificates.crt
# /etc/pki/tls/certs/ca-bundle.crt
# /usr/share/ssl/certs/ca-bundle.crt
# /usr/local/share/certs/ca-root-nss.crt
# /etc/ssl/cert.pem (lowest priority)
##network.http.cacert.set = /etc/ssl/certs/ca-certificates.crt

# Path to the certificate directory to verify the peer. The certificates
# must be in PEM format, and the directory must have been processed using
# the c_rehash utility supplied with openssl.
#
# For advanced users only, generally you should use network.http.cacert.set
# to specify path to the bundle of certificates.
##network.http.capath.set = "/etc/ssl/certs"

#network.http.ssl_verify_peer.set = 1
#network.http.ssl_verify_host.set = 1

# Run the rTorrent process as a daemon in the background
#system.daemon.set = false

# XML-RPC interface
network.scgi.open_local = (cat,(cfg.basedir),rtorrent.sock)

# Logging:
# Levels = critical error warn notice info debug
# Groups = connection_* dht_* peer_* rpc_* storage_* thread_* tracker_* torrent_*
print = (cat, "Logging to ", (cfg.logfile))
log.open_file = "log", (cfg.logfile)
log.add_output = "info", "log"
##log.add_output = "tracker_debug", "log"

### END of rtorrent.rc ###

批量迁移种子文件

rtorrent_fast_resume.pl

rtorrent_fast_resume.pl 是一个Perl脚本,可以将快速恢复数据添加到种子文件,从而跳过rTorrent必须对所有文件进行哈希检查。

用法:

1
$ rtorrent_fast_resume.pl [文件所在目录] [种子路径.torrent] [生成种子路径.torrent]

安装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 安装Bencode_XS
wget http://search.cpan.org/CPAN/authors/id/I/IW/IWADE/Convert-Bencode_XS-0.06.tar.gz
wget https://rt.cpan.org/Ticket/Attachment/1433449/761974/patch-t_001_tests_t
tar zxf Convert-Bencode_XS-0.06.tar.gz
cd Convert-Bencode_XS-0.06
patch -uNp0 -i ../patch-t_001_tests_t
perl Makefile.PL
make
make test
sudo make install
## The patch-t_001_tests_t patch itself:
--- t/001_tests.t.orig<>Sat Nov 15 14:41:13 2014
+++ t/001_tests.t<----->Sat Nov 15 14:41:27 2014
@@ -109,6 +109,7 @@ SKIP: {
#we use Storable so we do not rely on bencode
eval q{use Storable qw(freeze)};.
skip "Storable not available", 12 if $@;
+ local $Storable::canonical = 1;
local $Convert::Bencode_XS::COERCE = 0;
is( freeze(bdecode('le')), freeze([]) );
is( freeze(bdecode('l0:0:0:e')), freeze(['', '', '']) );
# 安装rtorrent_fast_resume.pl脚本
sudo wget https://github.com/jesec/rtorrent/raw/master/doc/rtorrent_fast_resume.pl -O /usr/local/bin/rtorrent_fast_resume.pl

迁移脚本

move_torrent.sh

1
2
3
4
5
6
#!/bin/bash

for i in `cat $1`; do
echo ${i}
rtorrent_fast_resume.pl $3 "${2}/${i}.torrent" "${4}/${i}.torrent"
done

用法

1
bash move_torrent.sh [需要迁移种子列表] [需要迁移种子所在目录] [文件所在目录] [生成种子目录]
  • 需要迁移种子列表:不含后缀名
  • 需要迁移种子所在目录:略
  • 文件所在目录:略
  • 生成种子目录:略

BUG与处理方式

random_device

rTorrent 无法启动/循环启动。

systemd日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ sudo systemctl status rtorrent.service
● rtorrent.service - rTorrent service for Docker
Loaded: loaded (/etc/systemd/system/rtorrent.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2022-05-18 06:17:52 CST; 10s ago
Process: 140420 ExecStart=/usr/local/bin/rtorrent -o system.daemon.set=true (code=exited, status=255/EXCEPTION)
Main PID: 140420 (code=exited, status=255/EXCEPTION)
CPU: 912ms

May 18 06:17:51 nas systemd[1]: Started rTorrent service for Docker.
May 18 06:17:51 nas rtorrent[140420]: rTorrent: loading 939 entries from session directory
May 18 06:17:52 nas rtorrent[140420]: rTorrent: started, 939 torrents loaded
May 18 06:17:52 nas rtorrent[140420]: rtorrent: random_device::random_device(const std::string&): device not available
May 18 06:17:52 nas systemd[1]: rtorrent.service: Main process exited, code=exited, status=255/EXCEPTION
May 18 06:17:52 nas systemd[1]: rtorrent.service: Failed with result 'exit-code'.

处理方式一

1
2
3
screen -S rt
export XDG_CONFIG_HOME=/usr/local/etc
/usr/local/bin/rtorrent -o system.daemon.set=true

处理方式二

1
2
3
crontab -e
# 增加一行
*/5 * * * * /usr/bin/bash -c "export XDG_CONFIG_HOME=/usr/local/etc && test `pidof rtorrent` || /usr/local/bin/rtorrent -o system.daemon.set=true &"