Skip to content

Instantly share code, notes, and snippets.

@rplaurindo
Last active November 5, 2024 17:40
Show Gist options
  • Save rplaurindo/73da83e26c0440003179 to your computer and use it in GitHub Desktop.
Save rplaurindo/73da83e26c0440003179 to your computer and use it in GitHub Desktop.

Woking With SSH Authentication

Dependencies

  • xclip

Creating folder

$ mkdir ~/.ssh

Entering inside folder

$ cd ~/.ssh

Generating a RSA key pair (public/private)

$ ssh-keygen -t rsa -b 4096 -C <encryption key>

Appoints the filename of the key and press ENTER two times.

Note: if no one name is informed to file, the tool will create with id_rsa to private key, and id_rsa.pub to public key.

Adding the key

Start the SSH agent

$ eval $(ssh-agent -s)

Add private key

$ ssh-add ~/.ssh/<filename>

Note: the $ ssh-add command with out a file will try to add the private key contained in id_rsa file by default.

Copying the public key and pasting on git repository web page

On Linux

$ xclip -sel clip < ~/.ssh/<filename>.pub

On Windows

$ cat ~/.ssh/<filename>.pub | clip

Testing remote connection

$ ssh -T git@<git-repository-domain>

Editing folder permissions

For security reasons, your should edit permissions in the folder ~/.ssh.

$ chmod 700 -R ~/.ssh

Working with multiple ssh key

SSH configuration

Create file config in ~/.ssh

$ touch ~/.ssh/config
$ subl ~/.ssh/config

Insert to each repository

Host domain[:repository-owner]
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/<filename references to private key>

Obs.: the Host key expects a pattern to differentiate from other user of the same domain. So, it cans be omitted.

Preparing the project

Run into project folder

$ git config --local remote.origin.url git@<repository-domain>:<repository-owner>/<project-path>.git
$ git config --local user.email <user-email>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment