Procedure to register the public key in Deploy keys of github repository and pull it with ssh on AWS EC2 (Amazon Linux 2)
Ssh to the server (with ec2-user) and create a public and private key with the user you want to git pull
.
console
//This time I want to git pull with a user called tamorieeeen
$ sudo su - tamorieeeen
// .Create ssh directory(If not)
$ mkdir .ssh
$ ls -la | grep ssh
drwxrwxr-x 2 tamorieeeen tamorieeeen 6 Aug 31 14:38 .ssh
//I think the directory permissions are 775, so change it to 700
$ chmod 700 .ssh/
$ cd .ssh
// id_rsa_Create a key with the name github
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tamorieeeen/.ssh/id_rsa): id_rsa_github
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_github.
Your public key has been saved in id_rsa_github.pub.
$ ls -l
-rw------- 1 tamorieeeen tamorieeeen 1675 Aug 31 14:42 id_rsa_github
-rw-r--r-- 1 tamorieeeen tamorieeeen 433 Aug 31 14:42 id_rsa_github.pub
Register the public key created earlier from Settings> Deploy keys> Add deploy key of the repository you want to register.
You can see the public key with less and copy it.
console
$ less id_rsa_github.pub
[email protected]
Give the Title a descriptive name, paste the public key you just copied into the Key, and press ʻAdd key` to complete.
on ʻAdd key
.If the private key has a file name other than ʻid_rsa, set config. (In the case of ʻid_rsa
, it should be set because it goes to see by default ...)
console
$ pwd
/home/tamorieeeen/.ssh
//Add settings to config
$ vi config
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
User git
$ ls -l
-rw-rw-r-- 1 tamorieeeen tamorieeeen 81 Aug 31 15:16 config
-rw------- 1 tamorieeeen tamorieeeen 1675 Aug 31 14:42 id_rsa_github
-rw-r--r-- 1 tamorieeeen tamorieeeen 433 Aug 31 14:42 id_rsa_github.pub
//Change config permissions to 600
$ chmod 600 config
If the following is output, the ssh connection is complete
.ssh / config
with the name after@
.
(I was addicted to Permission denied (publickey) .
here)console
$ ssh -T [email protected]
Hi tamorieeeen/repository_name! You've successfully authenticated, but GitHub does not provide shell access.
If you want to clone a new one, you can just clone it with the URL of Clone with SSH
on github, but this time it is a repository that has already been cloned with https, so change the connection method from https to ssh.
console
//Move to cloned repository
$ pwd
/home/tamorieeeen/repository
//Check the current remote repository
$ git remote -v
origin https://github.com/tamorieeeen/repository.git (fetch)
origin https://github.com/tamorieeeen/repository.git (push)
//Changed remote repository URL to ssh
$ git remote set-url origin [email protected]:tamorieeeen/repository.git
//Check if it has changed
$ git remote -v
origin [email protected]:tamorieeeen/repository.git (fetch)
origin [email protected]:tamorieeeen/repository.git (push)
//Try to pull
$ git pull origin develop
From github.com:tamorieeeen/repository
* branch develop -> FETCH_HEAD
Already up to date.
I was able to pull it safely, so I'm done.
-Your way to make SSH Keys is wrong -Connect to git with ssh! → Why? Permission Denied ...
Recommended Posts