"How I Built a Tor-Based IP Rotator in Python (Change Your IP Every Minute β€” or Every Second!)"

Omkar ShindeOmkar Shinde
5 min read

What This Does:

This Python script automatically changes your public IP address using the Tor network β€” every 1 minute by default (or even faster if you tweak it). It sends a signal to the Tor network to create a new circuit (new IP), then:

  • Fetches the new IP

  • Gets the country the IP belongs to

  • Prints the result to your terminal

  • Logs it into a file called ip_log.txt

Step 1: Install the Required Tools on Linux

sudo apt update
sudo apt install tor python3-pip -y
pip install stem requests

Step 2: Configure Tor for IP Changing

Edit Tor's configuration file:

sudo nano /etc/tor/torrc

Scroll to the bottom and add this:

ControlPort 9051
HashedControlPassword 16: (<paste_your_hash_here>)
CookieAuthentication 0

πŸ“ NOTE:
Always create your own hashed password for better security.
Replace the hash inside the brackets (<paste_your_hash_here>) with the one you generate.

How to Create Your Own Tor Password Hash

πŸ”Ή Step 1: Choose Your Password

Pick any password you want. Example: mynewpassword123

πŸ”Ή Step 2: Generate the Hash in Terminal

Run this command in your Linux terminal:

tor --hash-password mynewpassword123

Replace mynewpassword123 with your actual password.

πŸ”Ή Step 3: Copy the Output

You'll get output like this:

16:FA064B79853907A960053863B6A0AC60A7FEE4F2F701BA00DC4FA43963 (this is for example only)

This is your HashedControlPassword.

πŸ”Ή Step 4: Edit Tor Config File

Open Tor’s configuration file:

sudo nano /etc/tor/torrc

Scroll to the bottom and add (replace the hash with yours):

ControlPort 9051
HashedControlPassword 16:FA064B79853907A960053863B6A0AC60A7FEE4F2F701BA00DC4FA43963
CookieAuthentication 0

Step 3: Restart the Tor Service

After saving changes to the config file, apply them by restarting the Tor service:

sudo systemctl restart tor

βœ… Now Tor is ready to accept IP change commands via Python.

Full Python Script on GitHub

If you don’t want to copy-paste the entire code, no worries!
You can view and download the complete working script directly from my GitHub:

πŸ‘‰ Tor IP Rotator Script on GitHub

The repo includes the full Python script along with comments, logging, and setup instructions.

Step 4: Run the Python Script

Now that everything is set up, it's time to run your Tor IP Rotator script!

In your terminal, run:

python3 tor_ip_rotator.py

You should see output like:

πŸ•’ Rotating IP...
πŸ” Tor circuit changed!
🌐 IP: 185.xx.xx.xx
πŸ“ Country: Germany
🏒 ISP: N/A

πŸ” Customize the Rotation Speed (Optional)

Want to rotate IPs faster than every 60 seconds?

Locate these lines at the bottom of your script:

time.sleep(10)  # Let Tor build the circuit
...
time.sleep(50)  # Total wait time: 60 seconds

You can adjust them like this:

time.sleep(2)   # Give Tor some time (at least 2–3 sec)
time.sleep(8)   # Change every 10 seconds

⚠️ Note: Tor might not always give a new IP every second. It depends on available exit nodes and network conditions.

Common Issues and Fixes

ProblemCauseFix
Error changing IPWrong password in scriptUse the same one you hashed for torrc
IP Fetch ErrorTor not runningRun: sudo systemctl start tor
IP doesn’t changeTor reused same exit nodeWait longer or try a different exit node
Permission deniedNot rootNo sudo needed for the script, but torrc editing requires sudo

🎯 Final Thoughts

This script gives you control over your IP identity using Tor β€” useful for:

  • Safe web scraping

  • Anonymous testing

  • Learning how the Tor network works

You can extend it by:

  • Rotating through custom proxies

  • Adding a GUI

  • Running it as a background service

πŸ™Œ Feedback?

If you try this out, let me know how it worked for you!
Feel free to fork the GitHub repo or drop a comment with suggestions.

πŸ”— GitHub Repo: Tor IP Rotator

Want to See It in Action?

If you'd like to see how the script actually works β€” including live IP rotation and logging β€”
πŸ‘‰ Check out the video demo on my LinkedIn

🧠 Bonus: I explain how to run it, common errors, and show the changing IPs in real-time.

Final Usage Tip (Must-Read)

Before you start browsing or running your automation tasks:

  1. βœ… Start the Tor service (if it's not already running):
sudo systemctl start tor

2. Run the Python script to begin IP rotation:

python3 tor_ip_rotator.py
  1. 🌐 Using a regular browser (like Chrome/Firefox)?
    You must configure it to use the Tor proxy manually:

    • SOCKS5 Proxy: 127.0.0.1

      • Port: 9050

      • Proxy type: SOCKS5

      • (You can use browser extensions like FoxyProxy for easy setup.)

πŸ’‘ If you're using the Tor Browser, it's already configured β€” but this script won't affect the Tor Browser's internal IP; it's meant for custom Python-based traffic or external browsers/tools you set up to use the Tor proxy.

βœ… And that’s it!
You're now ready to browse, automate, or experiment safely through the Tor network with dynamic IP rotation.

🌍 Note on IP-Check API (optional)

The script currently uses the free API:

https://api.myip.com

This usually returns your IP address and country, but:

⚠️ Sometimes it may fail or rate-limit your requests β€” especially with frequent IP changes.

πŸ”„ What You Can Do:

Feel free to replace it with any other IP info API in the script. Here are a few alternatives:

API URLReturnsFree?
https://ipinfo.io/jsonIP, country, city, ISP, etc.βœ…
https://api.ipify.org?format=jsonIP onlyβœ…
https://ip-api.com/json/IP, location, ISPβœ… (with limits)

Update this line in your script:

res = requests.get("https://api.myip.com", proxies=proxies, timeout=10)

To:

res = requests.get("https://ipinfo.io/json", proxies=proxies, timeout=10)

And update how you extract values from the JSON response accordingly.

πŸ’‘ This flexibility lets you tailor the output for your use case β€” like showing ISP, region, or full geo-info.

πŸ’¬ Need Help?

Encountering an issue while setting things up?
I'm happy to help!

πŸ‘‰ Message me on LinkedIn and I’ll get back to you as soon as I can.

πŸ“© Whether it’s a Tor config error, IP not rotating, or proxy setup confusion β€” feel free to reach out.

20
Subscribe to my newsletter

Read articles from Omkar Shinde directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Omkar Shinde
Omkar Shinde

Hi! I'm Omkar, a B.Tech student specializing in Cybersecurity. I've been learning and exploring the world of cybersecurity since 2021, with a strong interest in ethical hacking, network defense, and cloud security. I'm also a cloud enthusiast, always curious about how secure and scalable systems work in the real world. Through this blog, I share my learning journey, hands-on experiences, and useful insights to help others grow with me. From Curiosity to Cybersecurity β€” Let’s Explore Together.