Connecting to Bitbucket Server via SSH

By | July 9, 2016

It can seem daunting at first, but connecting to a git server via SSH can be performed in a few easy steps. The idea is that with SSH, you create a public/private key pair that does the authentication for you so there is no need to type your password each time you want run git push for example.

Step 1: Check if ssh is installed

Type ssh -v to confirm that ssh is installed. On OSX this is what you should see by default:

>> ssh -v
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port]
           [-Q cipher | cipher-auth | mac | kex | key]
           [-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] [user@]hostname [command]

Step 2: Check if ssh is enabled in OSX

This step is important to check if you are running bitbucket on localhost. Otherwise you can skip this step.

>> ssh -v localhost
OpenSSH_6.9p1, LibreSSL 2.1.8
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to localhost [::1] port 22.
debug1: connect to address ::1 port 22: Connection refused
debug1: Connecting to localhost [127.0.0.1] port 22.
debug1: connect to address 127.0.0.1 port 22: Connection refused
ssh: connect to host localhost port 22: Connection refused

If you get connection refused, it could mean that the SSH daemon is not enabled. ie you can’t login remotely until you enable it.

Go to System Preferences > Sharing and enable Remote Login.

Screen Shot 2016-07-09 at 5.39.19 AM

Step 3: Check ~/.ssh directory

Check if you have some keys already:

>> ls -a ~/.ssh
.      ..    id_rsa       id_rsa.pub      known_hosts

If you have the id_rsa files you can load this into your bitbucket account in step 5. If not, let’s create them in the next step.

Step 4: Create your default identity

Typ ssh-keygen and accept the default location and enter a passphrase when prompted. It is recommended to provide a passphrase unless you are scripting and need automatic login. What you should see is the following:

cloudnthings:~ cloudnthings$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/cloudnthings/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/cloudnthings/.ssh/id_rsa.
Your public key has been saved in /Users/cloudnthings/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:DRl+AST8sfHGi9IvEXw6u5Z7BoSya0P53+cqr5XftfE cloudnthings@cloudnthings
The key's randomart image is:
+---[RSA 2048]----+
|     ...+..      |
|      .ooo .     |
|       ++*.      |
|    . . *+=      |
|     + oS*..     |
|    + . B ..     |
|   . o . Bo    ..|
|    + . *o+... .+|
|   . . o=X++o ..E|
+----[SHA256]-----+

You should now have id_rsa and id_rsa.pub in your ssh folder.

If you are running OSX 10.6.7 or lower, you’ll want to start the ssh-agent and load your keys. See step 3 here for more details. If you have 10.6.8 or above, move on to the next step.

Step 5: Load your public key to your bitbucket account.

View your key and copy the text via:

>>  cat ~/.ssh/id_rsa.pub

In the top right, click on your avatar > Manage account

Screen Shot 2016-07-09 at 5.28.37 AM

SSH Keys > Add keys

Screen Shot 2016-07-09 at 5.29.34 AM

Paste your public key here.

Step 6: Change the existing bitbucket repo from HTTPS to SSH.

Screen Shot 2016-07-09 at 5.44.47 AM

Once you have the SSH url, you need to update the git config.

>> cd ~/<path to your repo>
>> cat .git/config
[core]
   repositoryformatversion = 0
   filemode = true
   bare = false
   logallrefupdates = true
   ignorecase = true
   precomposeunicode = true
[remote "origin"]
   url = git@localhost:7999/android/my-weather.git
   fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
   remote = origin
   merge = refs/heads/master

You’ll see in the url property, I’ve changed this to reflect the SSH url “git@localhost:7999/android/my-weather.git”

Step 7: Test it!

Make a change and test it.

>> touch testfile.txt
>> git add .
>> git commit -m "testing SSH"
>> git push
cloudnthings:my-weather cloudnthings$ git push
Saving password to keychain failed
Identity added: /Users/cloudnthings/.ssh/id_rsa ((null))
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 290 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To ssh:// git@localhost:7999/android/my-weather.git
   226895c..f8a24cd  master -> master

Sources:

Leave a Reply

Your email address will not be published. Required fields are marked *