Port Forward SSH Port 22 for Bitbucket Server

This is a guide on port forwarding port 22 (SSH) in Windows and Linux for Bitbucket Server. This is done so that your users would not need to enter any port number when interacting with your Bitbucket Server.

Problem

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

D:Documentsrepos>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.

Explanation

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:
    netsh interface portproxy add v4tov4 listenport=22 connectport=<BITBUCKET_SERVER_SSH_PORT> connectaddress=<BITBUCKET_SERVER_IP>
    • the <BITBUCKET_SERVER_SSH_PORT> is your Bitbucket Server’s SSH Port (from Admintration > Server settings > SSH port):
      port forward ssh port 22 for bitbucket server
      Location where you can grab the SSH port for Bitbucket Server
    • the <BITBUCKET_SERVER_IP> is the IP address of your Bitbucket Server.
  3. Run the same command as step 1 to check the results of your port forwarding:
    C:WINDOWSsystem32>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:Documentsrepos>git clone ssh://git@localhost/test/test-repo.git
    Cloning into 'test-repo'...
    The authenticity of host 'localhost (127.0.0.1)' 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. 

Technology Consultant at ServiceRocket. Great knowledge in Atlassian JIRA. Blogger. Programmer. Anime Vector Artist.