When I first started using Termux for networking tools, I ran into a problem.
Most VPN apps on Android look nice, but they don’t work well in the background.
They get killed by battery optimization, or they don’t route traffic from Termux properly.
That becomes a problem when you're running scans, scripts, or anything that needs a stable connection.
What worked for me was switching to a lightweight setup using OpenVPN directly inside Termux.
No GUI. No heavy apps. Just a clean, always-on VPN running in the terminal.
In this guide, I’ll show you exactly how to do that using Surfshark.
Phase 1: What You Need
To follow this guide, you’ll need a Surfshark account.
I recommend it for this setup because it gives access to manual OpenVPN configuration files, which is exactly what we need for a terminal-based setup.
Phase 2: Setup in Termux
Step A: Update and Install Required Packages
Start by updating your Termux environment.
pkg update && pkg upgrade
Now install OpenVPN and curl:
pkg install openvpn curl
This gives you everything needed to connect to a VPN server from the terminal.
Step B: Get Your OpenVPN Credentials
This part is important.
Your normal Surfshark login (email and password) will NOT work here.
You need special OpenVPN credentials.
Here’s how to get them:
- Log in to your Surfshark dashboard
- Go to: VPN → Manual Setup → Desktop or Mobile → OpenVPN
- Copy your username and password
Keep them safe. You’ll use them in a moment.
Step C: Download a Server Configuration File
Next, download a server config file.
Surfshark provides .ovpn files for different countries.
In Termux, run something like this:
curl -O https://my.surfshark.com/vpn/api/v1/server/configurations/united-states-newyork.prod.surfshark.com_udp.ovpn
You can replace the location depending on the server you want.
Tip: Choose a server closer to you for better speed.
Step D: Save Your Login (Power Tip)
Typing your username and password every time gets annoying.
Instead, create a file to store them.
nano auth.txt
Inside the file, add:
your_openvpn_username
your_openvpn_password
Save and exit.
Now secure it:
chmod 600 auth.txt
This makes sure only you can read the file.
Phase 3: Connect to the VPN
Step E: Edit the Config File
Now we need to tell OpenVPN to use your saved credentials.
Open the .ovpn file:
nano united-states-newyork.prod.surfshark.com_udp.ovpn
Find this line:
auth-user-pass
Change it to:
auth-user-pass auth.txt
Save and exit.
Step F: Run the VPN
Now everything is ready.
Run:
openvpn --config united-states-newyork.prod.surfshark.com_udp.ovpn
If everything is correct, you’ll see logs showing the connection is established.
Leave this running.
Phase 4: Verify It’s Working
Now let’s confirm your traffic is actually going through the VPN.
Open a new Termux session.
Run:
curl ifconfig.me
If it shows a different IP (based on the server you chose), then you’re connected.
That means your Termux traffic is now encrypted and routed through Surfshark.
Why This Setup Works Better
After using this for a while, the difference is clear.
- No random disconnections
- Lower RAM usage
- Works directly with terminal tools
- More control over your connection
This is especially useful if you're learning networking, cybersecurity, or just want more privacy while working in Termux.
Final Thoughts
Running a VPN inside Termux might look complicated at first, but once you set it up, it just works.
You don’t need to rely on heavy apps anymore.
If you’re serious about using Termux for networking or cybersecurity practice, this setup makes a big difference.
If you haven’t already, you can get Surfshark here:
Get Surfshark VPN
Affiliate Disclosure
This post may contain affiliate links. If you choose to purchase through them, a small commission may be earned at no extra cost to you. This helps support the content and keep guides like this coming.
Top comments (0)