Sometimes, you may be blocked by a firewall and cannot access GitHub / BitBucket. In this post, the steps to bypass the firewall using SSH tunnel is documented.
Step 1 - Setup the tunnel
----------------------------------------
Assuming you use SSH to perform git operations (git clone, fetch, pull, merge, etc.), you should find a SSH URL like: git@github.com:example/example.git or git@bitbucket.org:example/example.git. In order to access the blocked SSH hosts, we have to SSH tunnel to forward the requests. To do so, use the following commands to create a tunnel (assume you have a SSH host that can be accessed).
ssh -C -L 8022:github.com:22 example@example.com # Establish a tunnel to github.com, SSH requests to local port 8022 are forwarded to github.com:22.
ssh -C -L 8122:bitbucket.org:22 example@example.com # Establish a tunnel to bitbucket.org, SSH requests to local port 8122 are forwarded to bitbucket.org:22.
Of course, you can combine the two to have one SSH session only.
ssh -C -L 8022:github.com:22 -L 8122:bitbucket.org:22 example@example.com
Step 2 - Config SSH client
------------------------------------------
After you have your tunnels, you can then configure your SSH client to redirect SSH requests to your tunnels. Put the following lines in your ~/.ssh/config file.
Host github.com
HostName 127.0.0.1
Port 8022
Host bitbucket.org
HostName 127.0.0.1
Port 8122
Afterwards, feel free to use git clone git@github.com:example/example.git or git clone git@bitbucket.org:example/example.git (you can also use git fetch, pull, merge, etc.). All requests will be passing through your SSH tunnel.