Passo a Passo para Configurar Várias Chaves SSH em Seu Repositório

Renan Morais
Renan Morais

Uma chave por serviço evita misturar GitHub e Bitbucket na mesma identidade. O ~/.ssh/config escolhe qual arquivo usar em cada host.

Criar as chaves

Bitbucket:

ssh-keygen -t ed25519 -C "seu_email@exemplo.com" -f ~/.ssh/bitbucket_key

GitHub:

ssh-keygen -t ed25519 -C "seu_email@exemplo.com" -f ~/.ssh/github_key

O -f grava a chave dentro de ~/.ssh. Sem esse argumento, o ssh-keygen pode salvar no diretório em que o terminal está aberto.

Restrinja a leitura da chave privada:

chmod 600 ~/.ssh/bitbucket_key ~/.ssh/github_key

Registrar no ssh-agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/bitbucket_key
ssh-add ~/.ssh/github_key

Publicar a chave pública

O que entra no site é o arquivo .pub. A chave privada fica na máquina.

cat ~/.ssh/bitbucket_key.pub
cat ~/.ssh/github_key.pub

Copie cada saída e cole no painel de chaves SSH do serviço correspondente.

Escolher a chave por host

nano ~/.ssh/config
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github_key
  IdentitiesOnly yes

Host bitbucket.org
  HostName bitbucket.org
  User git
  IdentityFile ~/.ssh/bitbucket_key
  IdentitiesOnly yes

IdentitiesOnly yes faz o SSH oferecer só a chave desse host. Sem isso, o serviço pode recusar a conexão mesmo com a chave certa cadastrada.

Testar

ssh -T git@bitbucket.org
ssh -T git@github.com

O GitHub responde que a autenticação funcionou e que não existe shell. O Bitbucket confirma o usuário autenticado.

Voltar