Share Files Over BitTorrent Using a Local Tracker and Client in Linux
written on Sunday, May 24, 2009
Everyone has heard of the BitTorrent protocol. It's widely used to quickly distribute large files with a low-bandwidth connection. There are also a lot of other features in BitTorrent that make it superior to either HTTP or FTP. For example, pseudo archives, built-in checksums, partial-download resumes, and send/receive bandwidth capping.
This post will show you how to share your personal files using BitTorrent tools that come standard with Ubuntu Linux (Jaunty 9.04). This article will cover the following steps:
- Configuring and running a local "tracker" server.
- Building torrent files from your personal file(s) you wish to share.
- Auto-seed and share your active torrent files.
Configure a Local BitTorrent Tracker Server
Before creating a torrent file, you should have an active tracker server configured and running. It is perfectly acceptable to use a public tracker, but running a personal tracker lets your clients connect over a private network (192.168.*). Also, if your torrent files are for commercial use, you likely do not want trust the reliability of your service to a third party.
The BitTorrent tracker server must be accessible to all your clients. Also, it must have either a static DNS or IP address so your clients can always contact it.
From the command line, install Bittornado, start the tracker, and verify it is running:
$ sudo apt-get install bittornado $ bttrack --port 6969 --dfile ~/.bttrack/dstate --logfile ~/.bttrack/tracker.log --nat_check 0 --scrape_allowed full $ firefox http://localhost:6969
The bttrack command will start a tracker that allows clients behind NAT connections. Connecting to the tracker port in FireFox will display a page with known torrents and status of each one.
Finally, be aware that with this configuration, the server will register all torrent files it is notified of. So, potentially someone could use this tracker for their own torrents. If you are on private network this may not be an issue, or you can use additional options to restrict the torrents allowed on the tracker:
--allowed_dir $HOME/btfiles --parse_dir_interval 10 --allow_get 1
These additional arguments to bttrack will force the tracker to only use files located the the ~HOME/btfiles directory. Remember, if you use these options, your torrents must be manually copied into this directory before the tracker will accept seeding requests.
Advanced Bttrack Config and Settings
Simplify the tracker startup with a simple shell script:
$ cat <<EOF > bin/bttrack.sh
#!/bin/bash
PORT=6969
DFILE="$HOME/.bttrack/dstate"
LFILE="$HOME/.bttrack/tracker.log"
# GET="--allowed_dir $HOME/btfiles --allow_get 1 --parse_dir_interval 10"
OPTS="--nat_check 0 --scrape_allowed full"
mkdir -p $HOME/.bttrack
bttrack --port $PORT --dfile $DFILE --logfile $LFILE $GET $OPTS
EOF
$ chmod 755 bin/bttrack.sh
You may want to add bttrack to your systems init.d startup scripts. Start with a copy of the default service skeleton file from /etc/init.d/skeleton:
$ sudo cp /etc/init.d/skeleton /etc/init.d/bttrack $ sudo vi /etc/init.d/bttrack
Replace the top section of the /etc/init.d/bttrack file using the example code below. Notice that the --dstate and --logfile values have been changed to a more standard location.
PATH=/usr/sbin:/usr/bin:/sbin:/bin
DESC="BitTorrent protocol tracker"
NAME=bttrack
DAEMON=/usr/bin/$NAME
DAEMON_ARGS="--dfile /var/run/$NAME/dstate --logfile /var/log/$NAME.log
--port 6969 --nat_check 0 --scrape_allowed full"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
do_start()
{
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --background --make-pidfile --quiet --pidfile $PIDFILE \
--exec $DAEMON -- $DAEMON_ARGS \
|| return 2
}
Create a Torrent File
The next step is to create one or more torrents files to provide the necessary information to begin seeding files inside of a BitTorrent Client. The following command is not the only method you can use. The other options for creating torrents is using a graphical client, such as Deluge or Transmission. I prefer the command line, since it is more concise:
$ sudo apt-get install bittornado $ btmakemetafile --piece_size_pow2 SIZE http://URL.COM:PORT/announce SOURCE --target OUTPUT.torrent
- SIZE [OPTIONAL]
- The torrent's "piece" size as a power of 2, where a "piece" is the smallest transferable unit of data. For example, 512KB (19) and 1MB (20). If not defined, this value is selected automatically. For fast connections, use a larger value such as 4MB (22).
- http://URL.COM:PORT/announce
- The address of the tracker and port number. The address can either be a DNS name or IP number.
- SOURCE
- The location of source directory or list of file(s) to add to the torrent.
- OUTPUT.torrent [OPTIONAL]
- The output name of the torrent file. Changing the name after creating the torrent file will break some BitTorrent clients, so set the name hear. If you leave this value empty, the torrent file will be named using the <strong>SOURCE</strong> name.
- --announce_list LIST [OPTIONAL]
- If your tracker has more than one DNS or IP, use this optional argument to set all tracker addresses. Make sure to duplicate the primary announce address, http://URL.COM:PORT/announce, in this list as well. The list can be "," separated for equal tiered trackers, or use "|" separator to indicate secondary trackers. See the man page for more details.
Seeding and Sharing Torrent Files
After you've created the torrent files for seeding, the final step is to start them from any BitTorrent client. If you need to seed files for an extended period of time, I recommend using rTorrent inside of a screen session. This allows you to run the client in the background, and pull-up the text-based GUI at any time.
The following steps will create an rTorrent configuration file that will automatically load torrents from a target directory and begin seeding:
$ cat <<EOF > ~/.rtorrent.rc
check_hash = no
session = ~/.rtorrent
directory = /data/torrents
schedule = watch_directory,5,5,load_start=/data/torrents/autostart/*.torrent
schedule = untied_directory,5,5,stop_untied=
EOF
$ sudo apt-get install rtorrent screen
$ screen rtorrent
rTorrent should be running in the current terminal. If you are not familiar with screen, here are a few keyboard shortcuts for navigation:
- <Ctrl-a> ?
- Show screen's help
- <Ctrl-a> d
- Detach the screen session
- <Ctrl-a> k
- Kill current screen session
- <Ctrl-a> c
- Create a new shell window in screen
- <Ctrl-a> n
- Show next window
- <Ctrl-a> p
- Show previous window
If you close the screen session, you can reconnect with the command "screen -r".
After launching rTorrent, it is now possible to start seeding the torrents.
- Move (or create a symbolic link to) the torrent data into rTorrent's "download" directory. The default download directory is defined in the rtorrent.rc file.
- Notice that in the rtorrent.rc file, it also specified the "watch" directory. Place the new torrent files in the "watch" directory, and they will be picked-up by rTorrent.
- Go back to the rTorrent GUI, and verify that the torrent is being re-hashed and seeded properly.
- Copy or e-mail the *.torrent files you have created to anyone that you want to share your files with.
Advanced Torrent File Sharing
Instead of manually sending *.torrent files to your family and friends, a better option is to run a public web server where the torrents can be made available 24/7:
$ sudo apt install lighttpd
$ cd /etc/lighttpd
$ sudo bash -c 'cat <<EOF > conf-available/40-site-torrent.conf
SERVER["socket"] == ":88" {
server.document-root = "/data/torrents/autostart"
}
EOF'
$ sudo /etc/init.d/lighttpd restart
This command creates a local web server running on port 88. The source directory can be mapped directly to the "watch" directory of your BitTorrent client. This is where all the active torrent files are stored while the torrents are active. Once enabled, the torrent files can be accessed from http://localhost:88/.