37
27

More than 3 years have passed since last update.

【GitHub】Deploy keysを使ってsshでpullするまで

Last updated at Posted at 2020-09-02

概要

githubリポジトリのDeploy keysに公開鍵を登録してAWS EC2(Amazon Linux2)上でsshでpullしてくるまでの手順

手順

1. 公開鍵と秘密鍵を作成する

サーバーに(ec2-userで)ssh接続し、git pullしたいユーザーで公開鍵と秘密鍵を作成する。

console
// 今回はtamorieeeenというユーザーでgit pullしたい
$ sudo su - tamorieeeen

// .sshディレクトリを作成(無い場合)
$ mkdir .ssh
$ ls -la | grep ssh
drwxrwxr-x 2 tamorieeeen tamorieeeen        6 Aug 31 14:38 .ssh

// ディレクトリの権限が775になってると思うので700に変更
$ chmod 700 .ssh/
$ cd .ssh

// id_rsa_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. githubに公開鍵を登録する

登録したいリポジトリの Settings > Deploy keys > Add deploy key からさっき作った公開鍵を登録する
deploy-keys.png

公開鍵はlessで見てコピペすればOK

console
$ less id_rsa_github.pub
ssh-rsa...tamorieeeen@ip-X-X-X-X.ap-northeast-1.compute.internal

add-deploy-key.png

Titleにわかりやすい名前をつけて、Keyにさっきコピーした公開鍵を張り付けてAdd keyを押せば完了。
※pushも使う場合はAdd key上のAllow write accessにチェックを入れる

3. ssh_configを設定する

秘密鍵をid_rsa以外のファイル名にした場合はconfigを設定する。
(id_rsaの場合はデフォルトで見に行くから設定不要…なはず)

console
$ pwd
/home/tamorieeeen/.ssh

// 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

// configの権限を600に変更
$ chmod 600 config

4. 接続を確認する

下記が出力されればssh接続は完了
.ssh/configのHostに設定した名称と@以降は一致させること
(ここでPermission denied (publickey).でハマった)

console
$ ssh -T git@github.com
Hi tamorieeeen/repository_name! You've successfully authenticated, but GitHub does not provide shell access.

5. https接続からssh接続に変更する

新規にcloneする場合はそのままgithubのClone with SSHのURLでcloneしてくれば良いが、今回はすでにhttpsでclone済のリポジトリなので接続方式をhttpsからsshに変更する。

console
// clone済のリポジトリに移動
$ pwd
/home/tamorieeeen/repository

// 現在のremoteリポジトリをチェック
$ git remote -v
origin  https://github.com/tamorieeeen/repository.git (fetch)
origin  https://github.com/tamorieeeen/repository.git (push)

// remoteリポジトリのURLをsshのものに変更
$ git remote set-url origin git@github.com:tamorieeeen/repository.git

// 変更されてるかチェック
$ git remote -v
origin  git@github.com:tamorieeeen/repository.git (fetch)
origin  git@github.com:tamorieeeen/repository.git (push)

// pullしてみる
$ git pull origin develop
From github.com:tamorieeeen/repository
 * branch            develop    -> FETCH_HEAD
Already up to date.

無事にpullできたので完了。

参考

37
27
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
37
27