Creating advanced bandwidth rules via Cron jobs in Linux
This guide will help you set up a cron job in Linux to set periods of time when Tor is allowed to have more or less bandwidth than others. An ideal use of this would be setting the bandwidthrate and bandwidthburst to 90% of your total allowable bandwidth during the wee hours of the morning, but throttling back to about 20% when you need more bandwidth for yourself during the rest of the day.
This will require NetCat for sending data to the control port for Tor.
The system this documentation is tested and based on is Fedora Core 4, but should work on all distros, with only minor modifications to NetCat's syntax.
Where to start
Make sure you have NetCat installed, and also make sure your torrc file has a controlport specified. In this guide, the default port 9051 will be used.
Utilizing the Cron file
If you already have a cron file for your user, you will need to manually add this information to it. If you don't have one, you need to create a Cron file that will control your scheduled processes throughout your account. Your Cron file can run a lot more than just Tor's bandwidth controls!
_Important: We are only making a new cron file if a cron file doesn't exist for your username yet. Use crontab -l to check if there's already a cronfile. If there is, add your desired configuration information to that cronfile with crontab -e. If there's no cronfile, you are free to give cron our new cron file._
Create a file in your user's home directory, by default /home/username/, named 'cron'. You will need to add relevant information to it based on your policies you choose in the following section.
Cron Bandwidth Policies
You can specify a limitless amount of rules for telling Tor how to manage its bandwidth. Note that every connection to the control port must end with a quit message. An example entry is below:
#[min] [hour] [day of month] [month] [day of week] [program to be run]
30 8 * * * echo -en "authenticate\r\nsetconf relaybandwidthrate=20000 relaybandwidthburst=20000\r\nquit\r\n" | nc localhost 9051
This entry will run at the 30th minute of the 8th hour, any day of the month, any month, and any day of the week. Notice that relaybandwidthrate and burst are both in bytes (20kb == 20000 bytes). By using relaybandwidth* instead of bandwidth*, you only limit the speed of relayed traffic on your node. Any traffic that you originate won't be slowed down by the bandwidth limiting.
We can specify two policies to control deadtime, and time when we need bandwidth for personal uses. The following policies control deadtime at midnight to 8 am using a 300 kb rate and 350 kb burst, and from 8 am till midnight time that we restrict Tor's bandwidth to 20 kb for both.
#[min] [hour] [day of month] [month] [day of week] [program to be run]
0 0 * * * echo -en "authenticate\r\nsetconf relaybandwidthrate=300000 relaybandwidthburst=350000\r\nquit\r\n" | nc localhost 9051
0 8 * * * echo -en "authenticate\r\nsetconf relaybandwidthrate=20000 relaybandwidthburst=20000\r\nquit\r\n" | nc localhost 9051
If you have a job from 8 to 5 on weekdays, and like to have more bandwidth to yourself from 5 till your bedtime at 11:30 plus all the time on the weekends, we can specify the following policies:
#[min] [hour] [day of month] [month] [day of week] [program to be run]
30 23 * * 1,2,3,4,5 echo -en "authenticate\r\nsetconf relaybandwidthrate=300000 relaybandwidthburst=350000\r\nquit\r\n" | nc localhost 9051
0 17 * * 1,2,3,4,5 echo -en "authenticate\r\nsetconf relaybandwidthrate=20000 relaybandwidthburst=20000\r\nquit\r\n" | nc localhost 9051
* * * * 0,6 echo -en "authenticate\r\nsetconf relaybandwidthrate=20000 relaybandwidthburst=20000\r\nquit\r\n" | nc localhost 9051
Starting 11:30pm at night on Mon-Fri (you don't use your home internet for the daytime hours) you give Tor 300 Kb rate, and 350 Kb burst. Starting at 5 pm when you get home on Mon-Fri, you only give Tor 20 Kb on both. We also need to state that on all hours of the weekends, Tor will use only 20 Kb.
Saving the Cron file, and setting it to be used by crontab.
As mentioned above, save the cron file to /home/username/cron.
_Important: We are only making a new cron file if a cron file doesn't exist for your username yet. Use crontab -l to check if there's already a cronfile. If there is, add your desired configuration information to that cronfile with crontab -e. If there's no cronfile, you are free to give cron our new cron file._
Now, under terminal execute the following command to add the cron file to crontab for your username
crontab [-u username] /home/username/cron
Acknowledgements
Many thanks to tup and Rob Levin of Peer-Directed Projects Center on the freenode IRC for their support in creating this guide. Original guide written by Silivrenion.
See also: TheOnionRouter/BandwidthLimitChangeController