
Stephano KambetaWhen I first started using Termux for networking tools, I ran into a problem. Most VPN apps on...
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.
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.
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.
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:
Keep them safe. You’ll use them in a moment.
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.
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.
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.
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.
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.
After using this for a while, the difference is clear.
This is especially useful if you're learning networking, cybersecurity, or just want more privacy while working in Termux.
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
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.