terça-feira, 14 de setembro de 2021

Como acessar github usando SSH Keys



Desde meados de 2021, o github não aceita mais acessar os repositórios usando login/senha. Para automatizar o acesso ao código fonte com propósito de CI/CD, pode ser util configurar o acesso via SSH Keys. Dessa forma, será necessário criar um par de chaves, e configurar a chave pública no github.

How to Generate SSH key for Git

SSH key is considered an access credential for the secure shell (SSH) network protocol. It is an encrypted and authenticated secure network protocol, applied for remote communication between machines operating on an unsecured network. This snippet will help you to find the kbd class="highlighted">SSH key on your PC if it exists, generate a new SSH key and learn how to add it to your ssh-agent or your GitHub/Bitbucket account.

Checking PC's SSH Keys

You can do that by following the steps below. Type ls -al ~/.ssh so as to see your ssh keys:

ls -al ~/.ssh

By default, the filename of the public key is one of the following:

  id_ecdsa.pub
  id_ed25519.pub
  id_rsa.pub

Generate a new SSH key

If you don't have an SSH key, you should create it. Now let’s generate a new SSH key step by step.

Type the command below, using your GitHub's account email:

ssh-keygen -t rsa -b 4096 -C "[your github's email]"

After running this command, you will be offered to set the SSH key path, but we recommend you to use its default value by pressing the "Enter" button.

> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

After that it will offer you to insert a passphrase to protect your SSH key.

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

Adding an SSH key to the ssh-agent

Now, when you already have an SSH key, you can add it into the ssh-agent.

  1. Be sure ssh-agent is enabled:
    eval "$(ssh-agent -s)"
  2. Add your SSH key to the ssh-agent.
    ssh-add ~/.ssh/id_rsa
    If you changed the default path while generating an SSH key, now you need to mention that path instead of ~/.ssh/id_rsa.

How To Add SSH Key To GitHub Account

Usually, repositories are private, and the developers are required to write a username and a password to take any actions related to the remote repository. Adding your SSH key to the GitHub/Bitbucket account helps the system to identify you and your are not asked to write your username and password each time.

Open your terminal and type the command below:

cat ~/.ssh/id_rsa.pub

Now, it will print the SSH key in the terminal, so you can copy it.

How to Add SSH Key to the GitHub Account

  1. Log into your GitHub's account. In the top right corner of any page, click your profile photo, then click Settings.
  2. In the user settings sidebar, click on SSH and GPG keys.
  3. Click on the New SSH key button.
  4. Type the title and your SSH key, and press the Add SSH key button

To check everything is correct, use ssh -T git@github.com

Now, we can do git clone:

git clone git@github.com:username/your-repository.git


Or, if the clone already exists, don't forget to modify the remote point as follows:
git remote set-url origin git@github.com:username/your-repository.git



Nenhum comentário:

Postar um comentário

O que significa o erro "Execução de scripts foi desabilitada neste sistema"?

  O que significa o erro "Execução de scripts foi desabilitada neste sistema"? Execute Set-ExecutionPolicy -ExecutionPolicy Remote...