DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • 7 Techniques That Supercharged My Claude-Assisted Development
  • Beyond Request-Response: Architecting Stateful Agentic Chatbots with the Command and State Patterns
  • A Beginner's Guide to Docker Compose
  • A Beginner's Guide to Essential Commands to Fix Container Setup Issues

Trending

  • Agentic Testing: Moving Quality From Checkpoint to Control Layer
  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  • The Agent Protocol Stack: MCP vs. A2A vs. AG-UI
  • S3 Vectors: How to Build a RAG Without a Vector Database
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Command Line Tor

Command Line Tor

So Tor's useful for anonymous internet access, for sure. But most of us use it from the browser bundle - how can you use it from the command line or a program?

By 
Christopher Lamb user avatar
Christopher Lamb
·
Updated Apr. 03, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
24.4K Views

Join the DZone community and get the full member experience.

Join For Free

Let's take a look at the command line first. So Tor, in the bundle we all know and love, consists of two things - the browser and the router. The browser is preconfigured to use the Tor router in the installed bundle. When you use Tor from the command line, we don't have that option. We end up needing to, somehow, force the commands we would generally use from the command line to dispatch requests through the Tor router.

To do this, you use Tor, Torify, and, preferentially, Torsocks. So first, you still need to install Tor. You don't need to use the bundle - you can use either brew or apt, or likely any other package manager. You'll want to install Tor and Torsocks. On Ubuntu or a similar Debian system, you get all this for free by installing Tor. If you're on OS X, use brew to install Tor and Torsocks separately - Torsocks isn't included. If you're not using either a Linux or OS X, well, you have other issues you might want to address, but you can fix that temporarily at least by downloading VirtualBox and getting a VM running you can use to do real work.

Now, you do need to configure Tor in order to use it - and this is slightly different depending on the platform you use. On a Mac, copy the torrc.sample file to the root of the Tor distribution - /usr/local/etc/tor, and name it torrc. Then uncomment the controlport line and the Cookieauthentication line. Change the cookieauthentication value to 0 and start Tor (the tor command on the command line is sufficient for this).

Do the same thing on a Linux system, but you don't need to copy the torrc file first - it's in /etc/tor. After you've made these changes, you can restart the service or the system - up to you. On Linuxes, Tor runs as a service.

Now Torify requires that you manually create a new session in order to use Tor. You do this by sending a command to the Tor control port, like this:

$ echo -e 'AUTHENTICATE ""\r\nsignal NEWNYM\r\nQUIT' | nc 127.0.0.1 9051 

Then, you can use Torify to execute a request over Tor:

$ torify curl -v https://slashdot.org

To start a new session, re-echo the authentication command to the local control port.

Now Torify is a bit complex, and it's easy to mess up. Torsocks is harder to use incorrectly, as you don't need to manually create a new session, or manually control the router. With Tor running, you use Torsocks just like you do Torify (e.g. torsocks <your command here>), and it will create the virtual circuit for you prior to executing your command.

So overall, not that bad! Enjoy your anonymous command line.

Tor (network) Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • 7 Techniques That Supercharged My Claude-Assisted Development
  • Beyond Request-Response: Architecting Stateful Agentic Chatbots with the Command and State Patterns
  • A Beginner's Guide to Docker Compose
  • A Beginner's Guide to Essential Commands to Fix Container Setup Issues

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook