Bitbucket: Port Forward SSH Port 22



If you attempt to clone a Bitbucket Server repository on SSH, you might stump into this problem:

D:\Documents\repos>git clone ssh://git@localhost/test/test-repo.git
Cloning into 'test-repo'...
ssh: connect to host localhost port 22: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

Cover Photo by Glenn Carstens-Peters on Unsplash


Bitbucket Server listens for SSH connections on port 7999 by default.

Your users will need to include the port in the URL they use to clone from Bitbucket Server, for example:

git clone ssh://git@localhost:7999/test/test-repo.git`

Rather than have the port number in the URL, you may wish to set up port forwarding so that connections to the default SSH port are forwarded to the port Bitbucket Server is listening on (e.g. you could forward port 22 to port 7999).

This would allow your users to use a URL without a port number in it, like this:

git clone ssh://git@localhost/test/test-repo.git`

This simple how-to will guide you on how to port forward the SSH port on a Linux or Windows environment.

Solution for Linux

Atlassian provided a document on how to set up SSH port forwarding in Linux using HAProxy.

Solution for Windows

  1. Open the command line prompt in Administrator’s mode and check if there are any existing routes that might interfere, using the command below:
    netsh interface portproxy show all
  2. If no results are found, you can start to port forward the SSH port using the command below (you can identify Bitbucket's SSH port from here):
    netsh interface portproxy add v4tov4 listenport=22 connectport=   <BITBUCKET_SERVER_SSH_PORT> connectaddress=<BITBUCKET_SERVER_IP>
  3. Run the same command as step 1 to check the results of your port forwarding:
    C:\WINDOWS\system32>netsh interface portproxy show all
    Listen on ipv4:             Connect to ipv4:
    Address         Port        Address         Port
    --------------- ----------  --------------- ----------
    *               22          localhost       7999
  4. Attempt to clone your repository once more:
    D:\Documents\repos>git clone ssh://git@localhost/test/test-repo.git
    Cloning into 'test-repo'...
    The authenticity of host 'localhost (' can't be established.
    RSA key fingerprint is SHA256:jtrT+G9H9TPaZzcknnMsR8yJiK7PF0+xnYuNIxjRZuc.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
    Enter passphrase for key '/c/Users/jalex/.ssh/id_rsa':
    > warning: You appear to have cloned an empty repository.
    > Checking connectivity... done.