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


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
Problem | Cause | Fix |
Error changing IP | Wrong password in script | Use the same one you hashed for torrc |
IP Fetch Error | Tor not running | Run: sudo systemctl start tor |
IP doesnβt change | Tor reused same exit node | Wait longer or try a different exit node |
Permission denied | Not root | No 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.
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:
- β 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
π 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 URL | Returns | Free? |
https://ipinfo.io/json | IP, country, city, ISP, etc. | β |
https://api.ipify.org?format=json | IP 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.
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.