Skip to content

Setting up Linux server with SSH keygen for BitBucket

Michael Hulse edited this page Oct 6, 2021 · 16 revisions

June, 2017 update: Setup SSH key for local dev box and use agent forwarding for servers

October, 2021 update: Kinda related: https://blog.developer.atlassian.com/different-ssh-keys-multiple-bitbucket-accounts/

Create/setup the key on your server:

  1. Login to server using SSH/terminal.
  2. Depending on your setup, you may need to $ su - and enter the root user’s password (depending on the steps you take below, this will create files in that user’s home directory with root permissions).
  3. List the contents of .ssh directory: $ ls -a ~/.ssh and check for an existing id_rsa.pub; use that default identity in BitBucket (skip to next heading) or …
  4. At the command prompt, type: $ ssh-keygen.
  5. Follow the on-screen instructions (on a production machine, a password should be entered for security purposes).
  6. For comparison’s sake, list the contents of .ssh directory: $ ls -a ~/.ssh.
  7. Check to see if ssh-agent is running: $ ps -e | grep [s]sh-agent.
  8. If not (above command returns nothing) then run: ssh-agent /bin/bash.
  9. Add newly-created key to the ssh-agent: $ ssh-add ~/.ssh/id_rsa.
  10. View list of keys the ssh-agent is managing: $ ssh-add -l.
  11. Run $ cat ~/.ssh/id_rsa.pub and copy the output to your clipboard.

Reversing the above process is easy:

# Remove a specific identity:
$ ssh-add -d ~/.ssh/id_rsa
# Remove all identities:
$ ssh-add -D
$ rm ~/.ssh/id_rsa*

Add key to BitBucket

  1. On BitBucket, choose avatar > Manage Account.
  2. Switch to the account you want to manage and click SSH keys; add a new key which should be named to match the server name (e.g., dev.foodomain.com).
  3. Paste the key into the Key field and click Add key.

Clone BitBucket repo

  1. Go to your repo in BitBucket and copy the SSH URI (e.g., [email protected]:user-name/repo-name.git).
  2. SSH to your server and navigate to the location you want to clone the repo to.
  3. From the command line, run: $ git clone [email protected]:user-name/repo-name.git. Note: If you want to specify the folder name that the repo clones into, add that to the end of the command (e.g., git clone [email protected]:user-name/repo-name.git target-folder-name).

Done!

Pulling updates

  1. SSH to your server.
  2. Navigate to your repo: $ cd target-folder-name/.
  3. Run $ git remote update && git status $ git fetch.
  4. If there are changes to pull, then run $ git pull.
  5. Optionally re-run $ git remote update && git status $ git fetch && git pull to see if everything is clean.

Links

Clone this wiki locally