How to download and manipulate torrents using only the command line in Ubuntu

This tutorial will allow you to download and manipulate torrents locally and remotely using command line tools and ssh.


You will need two computers running Linux, one acting as a torrent/ssh server the other as a client.

On Server

Install and configure ssh
CODE
apt-get install ssh openssh nano
sudo nano /etc/ssh/sshd_conf


Change or add these options, any line that has a # at the beginning is a comment, the options are the lines without the #

#This will disable ssh protocol 1, breaking compatibility with some clients and rendering version 1 options ineffective
#I have not included the v1 options here.
#Both of my clients support Protocol Version 2, we will disable version 1.
Protocol 2

# Replace 10.0.90.23 with your ip address
# Run sudo ifconfig -a
ListenAddress 10.0.90.23

# Replace 55222 with port number, one not in use up to 65535. This will help to stop automated attacks against ssh on port 22
# We can also specify -p on the command line which will override this and make sshd run on a specific port
#/usr/sbin/sshd -p 55229
Port 55222

#I am using IPv4 on 1 network adaptor, replace 10.0.90.23 with your ip address
#This binds sshd to just one ip address, by default sshd listens on all network addresses using both IP versions 4 & 6,
#we can restrict it to IPv4 by doing this. (This breaks IPv6 compatability)
ListenAddress 10.0.90.23
AddressFamily inet

#man syslog.conf for more information on syslog
#The defaults are fine for our needs
SyslogFacility AUTH
LogLevel INFO

#The server will give you 1 minute to enter your passphrase sucessfully before disconnecting the session.
LoginGraceTime 1m

#Disables root login
#Can enforce further by adding root to DenyUsers option, and by adding any account but root to the AllowUsers option
# (See Below) for using forced commands with keys for root
PermitRootLogin no

#This will make sshd check the users home directory for the correct file permissions and owners before allowing a log in.
#You can do this by running as a user "chmod -R o-w ~"
StrictModes yes

#This uses .rhosts/.shosts and /etc/host.equiv and trusts a file that can be manipulated by a user, .rhosts, we set this to no.
#See rhosts below

HostBasedAuthentication no

#Ignores users ~/.ssh/known_hosts
IgnoreUserKnownHosts yes

#Maximum login attempts per connection before disconnection
MaxAuthTries 4

#This will allow you to log into an account using a public-key
PubkeyAuthentication yes

#This will allow you to log into an account using a password, (we will leave this as yes for now).
#changing this to no will stop any account logging in with a password.
PasswordAuthentication yes

#When the PasswordAuthentication above is set to yes this option will deny account login with a blank password
PermitEmptyPasswords no

# This is the path to the authorized_keys file, containing your public-key.

# bob /home/bob/.ssh/authorized_keys
# root /root/.ssh/authorized_keys

AuthorizedKeysFile .ssh/authorized_keys

#This will disable tcp forwarding (not a solution but it removes it from this configuration)
AllowTcpForwarding no

#This disables X forwarding so ssh -X or -Y will not work.
X11Forwarding no

#This prints /etc/motd on connection
PrintMotd no

#This option will print the date and time and ip address of the last login
PrintLastLog no

#This is the default,
#TcpKeepAlive is to detect a dead connection.
TCPKeepAlive yes

#change USERNAME to your username and host combination.
AllowUsers USERNAME
DenyUsers root

UsePrivilegeSeparation yes


CTRL X to save and exit



Restart sshd
CODE
sudo /etc/init.d/ssh restart


On the client

On the client we now generate our key
CODE
ssh-keygen -t rsa


Press enter for defaut location then add passphrase and repeat passphrase. If left blank then no passphrase is required, which is great for scripting and ease of use but not as secure.


Copy our generated key to ssh sever

Replacing 55222 with sshd port you have decided to use in sshd_conf above, 10.0.90.23 with ip of your ssh server and USERNAME with your username on the ssh server
CODE
scp -P 55222 .ssh/id_rsa.pub USERNAME@10.0.90.23:/home/USERNAME/.ssh/authorized_keys

enter password



To Test
CODE
ssh -p 55222 USERNAME@10.0.90.23

enter passphrase this time and stay logged in, continued below


On Server


Setting permissions
CODE
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys*




Edit sshd.conf to turn off password login
CODE
sudo nano  /etc/ssh/sshd_conf


Change PasswordAuthentication yes
To PasswordAuthentication no

CTRL X to save and exit

restart sshd
CODE
sudo /etc/init.d/ssh restart


exit ssh session
CODE
exit



rtorrent and libtorrent install


Replace 55222 with sshd port you have decided to use above, 10.0.90.23 with ip of your ssh server and USERNAME with username of account on ssh server.
CODE
ssh -p 55222 USERNAME@10.0.90.23











Installing rTorrent



Install dependencies
CODE
sudo apt-get install build-essential libsigc++-2.0-dev pkg-config comerr-dev libcurl3-openssl-dev libidn11-dev libkadm55 libkrb5-dev libssl-dev zlib1g-dev libncurses5 libncurses5-dev



Preparing for install
CODE
mkdir rtorrent
cd rtorrent
wget http://libtorrent.rakshasa.no/downloads/rtorrent-0.7.4.tar.gz
wget http://libtorrent.rakshasa.no/downloads/libtorrent-0.11.4.tar.gz
tar xvzf rtorrent-0.7.4.tar.gz
tar xvzf libtorrent-0.11.4.tar.gz



Install libtorrent

CODE
cd libtorrent-0.11.4/
./configure
make
sudo make install; sudo ldconfig


rTorrent install

CODE
cd ../rtorrent-0.7.4
./configure --prefix=/usr/local/bin/
make
sudo make install; sudo ldconfig



Pick a port range for rtorrent and allow and forward ports on router/firewall, I have used 51000-51010


make appropriate directories
CODE
mkdir ~/Torrents/; mkdir ~/Torrents/session



I have used the example file from the wiki as a base, This will
1) Save Torrents in "~/Torrents"
2) Allow incoming and try outgoing encryption, retrying without if necessary
3) Using port range 51000-51010
4) I have added the performace boost from the rtorrent wiki, change values back if you don't want this.
5) Seed torrents until ratio = 2.0 with at least 200 MB uploaded or else with a ratio of 20.0
6) You can comment or adjust anything that is not wanted

CODE
nano ~/.rtorrent.rc


#Copy and Paste Below here

#Start of .rtorrent.rc

#Maximum and minimum number of peers to connect to per torrent.
#min_peers = 40
max_peers = 80

# Same as above but for seeding completed torrents (-1 = same as downloading)
#min_peers_seed = 10
max_peers_seed = -1

# Maximum number of simultanious uploads per torrent.
max_uploads = 15

# Global upload and download rate in KiB. "0" for unlimited.
download_rate = 0
upload_rate = 0

# Default directory to save the downloaded torrents.
directory = ~/Torrents

# Default session directory. Make sure you don't run multiple instance
# of rtorrent using the same session directory. Perhaps using a
# relative path?
session = ~/Torrents/session

# Watch a directory for new torrents, and stop those that have been
# deleted.
#schedule = watch_directory,5,5,load_start=~/Torrents/watch/*.torrent
#schedule = untied_directory,5,5,stop_untied=

# Close torrents when diskspace is low.
schedule = low_diskspace,5,60,close_low_diskspace=100M

# Stop torrents when reaching upload ratio in percent,
# when also reaching total upload in bytes, or when
# reaching final upload ratio in percent.
# example: stop at ratio 2.0 with at least 200 MB uploaded, or else ratio 20.0
schedule = ratio,60,60,"stop_on_ratio=200,200M,2000"


# The ip address reported to the tracker.
#ip = 127.0.0.1
#ip = rakshasa.no

# The ip address the listening socket and outgoing connections is
# bound to.
#bind = 127.0.0.1
#bind = rakshasa.no

# Port range to use for listening.
port_range = 51000-51010

# Start opening ports at a random position within the port range.
#port_random = no

# Check hash for finished torrents. Might be usefull until the bug is
# fixed that causes lack of diskspace not to be properly reported.
check_hash = yes

# Set whetever the client should try to connect to UDP trackers.
use_udp_trackers = no

# Alternative calls to bind and ip that should handle dynamic ip's.
#schedule = ip_tick,0,1800,ip=rakshasa
#schedule = bind_tick,0,1800,bind=rakshasa

# Encryption options, set to none (default) or any combination of the following:
# allow_incoming, try_outgoing, require, require_RC4, enable_retry, prefer_plaintext
#
# The example value allows incoming encrypted connections, starts unencrypted
# outgoing connections but retries with encryption if they fail, preferring
# plaintext to RC4 encryption after the encrypted handshake
#
encryption = allow_incoming,try_outgoing,enable_retry

#
# Do not modify the following parameters unless you know what you're doing.
#

# Hash read-ahead controls how many MB to request the kernel to read
# ahead. If the value is too low the disk may not be fully utilized,
# while if too high the kernel might not be able to keep the read
# pages in memory thus end up trashing.
hash_read_ahead = 8

# Interval between attempts to check the hash, in milliseconds.
hash_interval = 10

# Number of attempts to check the hash while using the mincore status,
# before forcing. Overworked systems might need lower values to get a
# decent hash checking rate.
hash_max_tries = 5

# Max number of files to keep open simultaniously.
#max_open_files = 128

# Number of sockets to simultaneously keep open.
#max_open_sockets = <no default>


#Finish of .rtorrent.rc



Downloading Torrents with elinks

On Server

CODE
cd
sudo apt-get install elinks
nano ~/.elinks/elinks.conf



Copy and paste below
############################################
# Automatically saved options
#

## document.download.directory <str>
# Default download directory.
set document.download.directory = "~/Torrents/"

## ui.language <language>
# Language of user interface. 'System' means that the language will
# be extracted from the environment dynamically.
set ui.language = "System"


## terminal.xterm.colors <num>
set terminal.xterm.colors = 1

# Automatically saved options
#

## terminal.xterm.type <num>
set terminal.xterm.type = 1
################################

CTRL X to save and exit


USAGE

On Client

To access over the internet forward port 55222 to your torrent box and change IPADDRESS to External IP Address of your router.

Log into torrent box and start elinks browser in a screen session
CODE
ssh -p 55222 USERNAME@IPADDRESS
screen
elinks http://darksiderg.com


Log in and download a torrent file, it will automatically be downloaded to the torrents directory then exit elinks


Change to torrent directory
CODE
cd ~/Torrents


Run rtorrent replace new.torrent with your torrent name
CODE
rtorrent new.torrent


Once started use the cursor keys to show details of the torrent.

This will detach the screen session allowing us to reattach later via ssh or locally

CODE
CTRL +a +d


to reattach the same session

CODE
screen -r








I have c&p the keys from the rtorrent user guide so the information is all in one place


^q
Initiate shutdown, press again to force the shutdown and skip sending the stop signal to trackers.

up | down | left | right arrow keys, ^P | ^N | ^B | ^F
Select entries or change windows. The right arrow key or ^F is often used for viewing details about the selected entry, while the left arrow key or ^B often returns to the previous screen.


a | s | d
Increase the upload throttle by 1/5/50 KB.

A | S | D
Increase the download throttle by 1/5/50 KB.

z | x | c
Decrease the upload throttle by 1/5/50 KB.

Z | X | C
Decrease the download throttle by 1/5/50 KB.

Main View Keys
->
View download.

1 - 7
Change view.

^S
Start download.

^D
Stop an active download, or remove a stopped download.

^K
Close a torrent and its files.

^R
Initiate hash check of torrent.

^O
Change the destination directory of the download. The torrent must be closed.

^X
Call commands or change settings.

+ | -
Change the priority of the download.

backspace
Add torrent using an URL or file path. Use tab to view directory content and do auto-complete.

l
View log. Exit by pressing the space-bar.

U
Delete the file the torrent is tied to, and clear the association.

I
Toggle whether torrent ignores ratio settings.



Download View Keys

->
View torrent file list. Use the space-bar to change the file priority and * to change the priority of all files. Use / to collapse the directories. OUTDATED

1 | 2
Adjust max uploads.

3 | 4
Adjust min peers.

5 | 6
Adjust max peers.

u
Display transfering blocks.

i
Display chunk rarity.

o
Display the tracker list. Cycle the trackers in a group with the space-bar.

p
View peer and torrent information.

t | T
Initiate tracker request. Use capital T to force the request, ignoring the "min interval" set by the tracker.

k
Disconnect peer.

*
Choke/Snub peer.

Have fun fellow darksiders drinks.gif