To Nha Notes | Dec. 28, 2022, 9:03 p.m.
You can generate an SSH key on your local directory and then copy to your EC2 instance. You can also do it on your EC2 instance directly, but each time you generate an SSH key pair on your new instance, you need to register the new key in GitHub every time.
In your local terminal, create an SSH key, substituting your email address.
$ ssh-keygen -t rsa -b 4096 -C [your email address]
Save the key to the default directory, ~/.ssh
Enter a pass-phrase of your choice.
Check that the public and private key are in /.ssh by going to the directory and typing “ls -l id_rsa*”. You should see two files, the public key named “id_rsa.pub” and the private key named “id_rsa”
From the terminal, make sure this private key is not publicly viewable.
$ chmod 600 ~/.ssh/id_rsa
Add your SSH private key to the ssh-agent and store your passphrase in the keychain.
$ ssh-add -k ~/.ssh/id_rsa
Go to the settings under your GitHub account and then click SSH keys and New SSH key
In terminal copy your public key to the clipboard. Or show on the EC2 terminal:
$ pbcopy < ~/.ssh/id_rsa.pub # copy to clipboard $ cat ~/.ssh/id_rsa.pub # If you prefer appear on screen
Paste this into the key box on GitHub and click save. This key is available to ALL your Git repositories.
Sometimes you need to move the public key to “/.ssh/authorized_keys” to make the public key to work in LINUX.
$ mkdir ~/.ssh # if you don't have /.ssh/ folder $ chmod 700 ~/.ssh $ touch ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Follow this article https://help.github.com/articles/error-permission-denied-publickey/ to see if the key works and debug.
https://stackoverflow.com/questions/19596974/ec2-how-to-clone-git-repository
https://docs.github.com/en/authentication/connecting-to-github-with-ssh/about-ssh