An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
How to use different SSH Keys for different Bitbucket (git) Repositories
Bitbucket doesn't allow you to use the same SSH Key on more than one account. This means if you have more than 1 account but need to access the repositories on both (in my case a work account and a personal account) you will need to configure your SSH agent to allow you to choose which SSH Key to use when cloning over SSH.
Navigate to your SSH Directory
cd ~/.ssh
Ensure SSH Agent is running
eval $(ssh-agent)
Create your SSH Keys
# Run the following replacing EMAIL_ADDRESS with the email address associated with your bitbucket accountssh-keygen -t rsa -b 4096 -C "EMAIL_ADDRESS"# When prompted to enter a file name, enter a name to help you differentiate your two keysEnter file in which to save the key (/c/Users/shane/.ssh/id_rsa): key-1
(optional) Remove previous SSH Keys for your SSH Agent (this does not delete the actual key from your ~/.ssh directory)
ssh-add -D
Add your newly generated SSH Keys to your SSH Agent
ssh-add ~/.ssh/key-1ssh-add ~/.ssh/key-2
Check that your SSH Keys have been added
ssh-add -l# Sample output4096 SHA256:Mo+oiomJASfjlksc6Qj0KcQpUXRVpyvsEIHdXNYDL1c [email protected] (RSA)4096 SHA256:6Jt908009upoASFjjkljsflkj9iaGebl7pNr6vuezPI [email protected] (RSA)
Create your SSH Config file
touch config
The format of our SSH Config
Host bitbucket-key-1 # Friendly name HostName bitbucket.org # The server we opening the SSH connection with User git # The SSH user (which for bitbucket is git eg. [email protected]) IdentityFile ~/.ssh/id_rsa # The private key file IdentitiesOnly yes # Tells SSH to only use keys provided by IdentityFile above
Create your different SSH Configs
# Bitbucket - SSH Key 1Host bitbucket-key-1 HostName bitbucket.org User git IdentityFile ~/.ssh/key-1-id_rsa IdentitiesOnly yes# Bitbucket - SSH Key 2Host bitbucket-key-2 HostName bitbucket.org User git IdentityFile ~/.ssh/key-2-id_rsa IdentitiesOnly yes
Important At this point, reload your terminal
Now when cloning via SSH we replace the usual host name (bitbucket.org) with our new "friendly" name
# Usual formatgit clone USER@HOST:ACCOUNT_NAME/REPOSITORY_NAME.git# Examplegit clone [email protected]:username/repository.git# Format using our new SSH Host config (replace bitbucket.org with the user friendly host name from our .config file)git clone git@bitbucket-key-1:username/repository.git
Original Link: https://dev.to/shane/how-to-use-different-ssh-keys-for-different-bitbucket-git-repositories-4of0
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To