Bash script that creates a persistent SSH reverse tunnel from your local machine to a public server. It uses a systemd service to keep the tunnel active and auto-restarts if it fails, making sure your connection is always up.
There are 2 types of usage: Manual Mode
and SSH Config Mode
Just run
sudo ./make_tun.sh
The script asks for the following parameters:
-
Public server IP (required)
The IP address or hostname of the server with a public IP. -
Public server User (optional, default:
user
)
The username for SSH login to the public server. -
Public server SSH port (optional, default:
22
)
The port number for the SSH service on the public server. -
Public server SSH key path (optional, default:
~/.ssh/id_rsa
)
The path to the private SSH key used for authentication. -
Public server forwarding port (required)
The port number on the public server to forward the local service. -
Local receiving port (optional, default:
22
)
The port number on the local machine that receives the forwarded traffic. -
Local Service name (optional, default:
backtun-{SERVER_IP}.service
)
Thesystemd
service name used to manage the tunnel.
Specify a host from the SSH config as a command-line argument, the make_tun reads the SSH configuration to populate the necessary parameters.
Example usage:
sudo ./make_tun.sh your_ssh_config_host
The make_tun.sh
uses the following SSH configuration parameters:
-
Hostname
The IP address or hostname of the server. -
User
The username for SSH login. -
Port
The port number for the SSH service. -
IdentityFile
The path to the private SSH key used for authentication.
Ensure your ~/.ssh/config
file contains entries similar to this:
Host exampleHost
Hostname 192.168.1.1
User yourUser
Port 2222
IdentityFile ~/.ssh/id_rsa
The make_tun.sh
creates a systemd
service file, configures it to start on boot, and starts the service immediately. The service is designed to keep the SSH reverse tunnel alive and reconnect automatically if the connection drops.
You can manage the tunnel service using systemctl
with the service name you provided (or the default name).
Start the service:
sudo systemctl start {service_name}
Stop the service:
sudo systemctl stop {service_name}
Enable the service to start on boot:
sudo systemctl enable {service_name}
Disable the service:
sudo systemctl disable {service_name}
Check the status of the service:
sudo systemctl status {service_name}