[GitHub] Until you pull with ssh using Deploy keys

Overview

Procedure to register the public key in Deploy keys of github repository and pull it with ssh on AWS EC2 (Amazon Linux 2)

procedure

1. Create public and private keys

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

2. Register your public key on github

Register the public key created earlier from Settings> Deploy keys> Add deploy key of the repository you want to register. deploy-keys.png

You can see the public key with less and copy it.

console


$ less id_rsa_github.pub
[email protected]

add-deploy-key.png

Give the Title a descriptive name, paste the public key you just copied into the Key, and press ʻAdd key` to complete.

3. Set ssh_config

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

4. Check the connection

If the following is output, the ssh connection is complete

console


$ ssh -T [email protected]
Hi tamorieeeen/repository_name! You've successfully authenticated, but GitHub does not provide shell access.

5. Change from https connection to ssh connection

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.

reference

-Your way to make SSH Keys is wrong -Connect to git with ssh! → Why? Permission Denied ...

Recommended Posts

[GitHub] Until you pull with ssh using Deploy keys
Until you start Jupyter with Docker
Until you run python with apache
Until you shake ruby with renpy
Until you can borrow VPS with Conoha and authenticate public key with SSH
# 1 Until you deploy Django's web application (instance construction with EC2 on AWS)
Until SSH connection with Fabric (MacOS-> CentOS7)
Note until you use emacs with WSL
Deploy flask app with mod_wsgi (using pipenv)
Until you confirm iOS communication with mitmproxy
Until you can use opencv with python
Until you CI what you made with Django with Jenkins
Until you use PhantomJS with Python on Heroku
What are you using when testing with Python?