2016-05-28

How to Configure SSH login for Bitbucket Authentication

Whenever I do a git push or git pull with bitbucket.org It asks for password which annoys me very badly. Hence I setup SSH based authentication. Here i have captured those steps that you can use.

First, You should have already created publickey, privatekey. if you don't have one, you can follow Creating SSH Keys Documentation by Bitbucket Team

Once you are done with creating publickey, privatekey pair. Let me walk you through the steps on how to setup SSH based authentication.

For Windows User

When you install Git on Windows, Git installs a few tiny program called git-bash.exe (Bash Shell for windows)

Local Developer Machine

1.Make sure you have both the publickey, privatekey inside your %userprofile%\.ssh folder

2.Open Explorer, Go To folder where git is installed. By default git is installed inside Program files\git

3.Launch git-bash.exe by double clicking

4.Do a cd ~/.ssh/ inside git-bash Shell.

5.You can open the file named config using your editor using the command vi config. Generally in git bash environment you would have vi editor.

6.Add the following lines in the config file.

Host bitbucket.org

# Add your private keyfile it should be something like this id_rsa
# It will not end with .pub
IdentityFile ~/.ssh/<Your_Private_key_Filename>

7.Create or open .bash_profile file using vi .bash_profile and add the following lines. This would load the key files automatically.

SSH_ENV=$HOME/.ssh/environment

# start the ssh-agent
function start_agent {
    echo "Initializing new SSH agent..."
    # spawn ssh-agent
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add
}

if [ -f "${SSH_ENV}" ]; then
     . "${SSH_ENV}" > /dev/null
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

Adding public key to your bitbucket account

  1. Logon to your bitbucket account

  2. Click on your picture on the top right corner to change Bitbucket Setting as show here [images/setup-ssh-login/bbsettingslink.png]

bbsettingslink.png

  1. Click on SSH Keys link

sshkeylink.png

or you can directly hit the link

https://bitbucket.org/account/user/<your-bitbucket-username>/ssh-keys/
  1. Click on Add Key button. Give a Label to your key like Bitbucket Projects and copy paste the publickey typically it would start with ssh-rsa

public-key-example.png

For Users who are already using Password based authentication

  1. If you are already using password based authentication, You just need to one more step

Go to your particular working directory, and check the protocol used git remote -v If you get output something like this

if you see your git repository url as https://meetneps@bitbucket.org/meetneps/myweb-app.git

First remove the repository git remote remove origin

Now add the repository git remote add origin git@bitbucket.org:meetneps/myweb-app.git.

You can find the exact url in your bitbucket account. ssh-url.png

You are done.

Now try doing a git push to your bitbucket repository. You will not be asked for the bitbucket password.

If you find this info useful, Feel free to share with your friends. Also You can drop any useful tip about using ssh in the comment section below.