Git through SSH port forwarding (SSH tunneling)

Scenario:

A.  you are on a firewalled network, from where you can’t access github (remote port:9418 is not allowed)

or

B. you want to access a securized git server from home, but your IP is not on the git server’s whitelist.

and, of course, you have ssh access on a server that can do this.

 

A. Make a ssh tunnel to the git server

Me –> ServerWithSSHAccess –> GitServer

The comand to use is:

ssh ServerWithSSHAccess -L 2000:GitServer:22 -N ,

where 2000 is the local port you will connect to, and 22 is the remote port used to connect to the git server (if the connection uses git:// instead of default ssh port, change this to 9418). Then, instead of:

git clone GitServer/my_repo,

you must use:

git clone localhost:2000/my_repo

 

B. Automatically use tunnel to connect to git server

Furthermore, if the repository is already cloned on your computer, or you want to use the same addresses, you can config your ssh client to automatically pick the tunneled connection when connecting to the git server. In ~/.ssh/config, add:

Host GitServer

HostName 127.0.0.1

Port 2000

so now you can access your git repo as you were directly connected to it:

git clone GitServer/my_repo

git pull
git push

etc

About these ads

2 Responses

  1. Awesome thanks. Was very useful today ;)

  2. I also needed to specify git+ssh: “git clone git+ssh://localhost:2000/my_repo“.
    Just saying in case someone else ends up here ;)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: