How to change the Tor exit node programmatically to get a new IP?

Tor

Tor Problem Overview


I have Tor running on my computer, and I need to change the Tor exit node every five minutes. For example, if I start using Tor via some exit node, then in 5 minutes I want Tor to change to an exit node with a different IP address. How can I do this?

Tor, as far as I know, is listening to port 8051 on localhost.

What commands can I send to this port to make Tor build a new chain, so that I can get another IP address?

Tor Solutions


Solution 1 - Tor

Method 1: HUP

sudo killall -HUP tor

Then check that your IP has changed with:

curl --socks5 127.0.0.1:9050 http://checkip.amazonaws.com/

Tested in Ubuntu 17.10 with sudo apt-get install tor version 1.6.0-5.

sudo is needed since the process is started by root by default.

What an HUP signal does exactly to the Tor daemon is documented at: https://gitweb.torproject.org/torspec.git/tree/control-spec.txt?id=03aaace9bd9459b0d4bf22a75012acf39d07bcec#n394 and is equivalent to sending some command through the command port.

Browser Bundle 5.0.5 is not affected by this, only daemon ports like the default 9050, which is not used by the TBB. For that use case see: https://tor.stackexchange.com/questions/1071/how-can-a-new-circuit-happen-without-closing-all-tabs

If you are deploying an army of Tor IPs as mentioned here you can selectively send:

kill -HUP $PID

Method 2: control port

Mentioned by kat:

(echo authenticate '""'; echo signal newnym; echo quit) | nc localhost 9051

but for that to work on Ubuntu 17.10 you must first:

  • enable the control port by uncommenting:

    ControlPort 9051
    

    from /etc/tor/torrc

  • Set the empty password, otherwise it gives 515 Authentication failed: Wrong length on authentication cookie.. First run:

    tor --hash-password ''
    

    This outputs something like:

    16:D14CC89AD7848B8C60093105E8284A2D3AB2CF3C20D95FECA0848CFAD2
    

    Now on /etc/tor/torrc update the line:

    HashedControlPassword 16:D14CC89AD7848B8C60093105E8284A2D3AB2CF3C20D95FECA0848CFAD2
    
  • Restart Tor:

    sudo service tor restart
    

Bonus: how to check that your IP changed

curl --socks5 127.0.0.1:9050 http://checkip.amazonaws.com/

See also: https://askubuntu.com/questions/95910/command-for-determining-my-public-ip

Related threads

Solution 2 - Tor

(echo authenticate '""'; echo signal newnym; echo quit) | nc localhost 9051

Solution 3 - Tor

This question seems to come up pretty frequently (1, 2) so I just added a FAQ entry for it. As mentioned earlier you can do this via a NEWNYM signal. Here's an example for doing it via stem...

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9051) as controller:
  controller.authenticate()
  controller.signal(Signal.NEWNYM)

Solution 4 - Tor

yeah, that's 1 (i mean =true =))) that tor does change ip every 10 minutes but! if i restart tor - i'll get a new ip even in this 10minutes interval. so i was thinking about making tor to send this "change_ip" request manually. see this code (written according to [http://en.linuxreviews.org/HOWTO_use_the_Internet_anonymously_using_Tor_and_Privoxy][1])

procedure ChangeIp;
var
  sck:TIdTCPClient;
begin
  sck:=TIdTCPClient.Create(nil);
  try
    sck.Host:='127.0.0.1';
    sck.Port:=10051;
    sck.Connect;
    sck.SendCmd('authenticate','');
    if sck.LastCmdResult.Code='250' then
    begin
      sck.SendCmd('signal newnym','');
    end;
  finally
    sck.Free;
  end;
end;

and accornig to [https://tor-svn.freehaven.net/svn/torctl/trunk/doc/howto.txt] i can write a controler that will change tor's conf on the fly. by default it is not enebled (i mean this ability), but i can make tor client listen to some port for accepting commands using torrc...if i'm not mistaken...again=)

!!! where the hell torrc is on my pc?

In C:\Users\geekman\AppData\Roaming\Tor i could,n fing it i got vista. [1]: http://en.linuxreviews.org/HOWTO_use_the_Internet_anonymously_using_Tor_and_Privoxy

Solution 5 - Tor

I've made a shell script for myself that also lets you do this remotely (if Tor server is running on another machine).

It's available here: https://gist.github.com/kirelagin/9667900.

Solution 6 - Tor

You can simply type or insert in your bash script:

service tor reload

Solution 7 - Tor

If you don't have access to the control port, you could use a different circuit which changes your IP*. This can be done by specifying a different socks username:

$ curl -x "socks5://[email protected]:9050" "https://ifconfig.io/ip"
109.70.100.34

$ curl -x "socks5://[email protected]:9050" "https://ifconfig.io/ip"
209.222.101.251

$ curl -x "socks5://[email protected]:9050" "https://ifconfig.io/ip"
54.37.19.83

If you want to reuse a specific IP at some point you can use the same username you used before to get that IP again.

* The exit node will be random every time and there is a slight chance you'll get the same IP twice in a row.

Correct me if I'm wrong, I'm not a Tor expert, but this works for me.

Solution 8 - Tor

You have no control over the routing in the tor network (if you had, someone could abuse this feature). But tor already switches the route roughly every 10 minutes (at least according to the German Wikipedia article).

Solution 9 - Tor

I have done something different here... i wrote a PHP program that can communicate with linux shell. The program would restart tor in regular intervals.

So when tor is restarted it gets a new IP.... Yeah.....!!

exec("/etc/init.d/tor restart",$ioOut);
print_r($ioOut); //output from shell after executing the command
sleep(25);

You can also write a shell script to do this.

I am now in search of a windows option to do this. The problem is .. in windows Tor is a service which cannot be restarted.

Solution 10 - Tor

Advtor gives you access over almost all advanced settings which works over tor network.

Solution 11 - Tor

I've wrote a library to control Tor with PHP. It is installable with Composer and allows to change the exit node.

Of course it's free software: http://dunglas.fr/2013/02/php-torcontrol-a-library-to-control-tor/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlazybobView Question on Stackoverflow
Solution 1 - TorCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 2 - TorkatView Answer on Stackoverflow
Solution 3 - TorDamianView Answer on Stackoverflow
Solution 4 - TorlazybobView Answer on Stackoverflow
Solution 5 - TorkirelaginView Answer on Stackoverflow
Solution 6 - TorLiamView Answer on Stackoverflow
Solution 7 - TorForivinView Answer on Stackoverflow
Solution 8 - TorAaron DigullaView Answer on Stackoverflow
Solution 9 - TorClain DsilvaView Answer on Stackoverflow
Solution 10 - TorCyrilView Answer on Stackoverflow
Solution 11 - TorKévin DunglasView Answer on Stackoverflow