LoginSignup
2
1

More than 1 year has passed since last update.

EC2からgithubにssh接続

Last updated at Posted at 2022-03-16

概要

日常の仕事でec2からgithubのリポジトリをcloneしたり、pullしたりする事がありますが、
毎回ユーザー名とパスワードで接続することができますが。
現在、githubの仕様でユーザー名とパスワードで接続場合はアクセストークンで2回認証しないといけないです。
毎回githubにアクセストークン作成のは結構ややこしいですね。
もしsshでgithubに接続すれば、ユーザー名とパスワードの入力は要らなくなります。楽になります。

手順

ec2にec2にアクセスして、ユーザーを切り替え

$ sudo su --login ec2-user

以下のコマンドでssh鍵を作成

ssh-keygen -o
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ec2-user/.ssh/id_rsa.
Your public key has been saved in /home/ec2-user/.ssh/id_rsa.pub.
The key fingerprint is:

enterキーを押して、ssh全ての設定はディフォルトにします。

公開鍵とプライベートキーを確認

cd ~/.ssh
ls
uthorized_keys  id_rsa  id_rsa.pub

id_rsa.pubは公開鍵です。
id_rsaはプライベートキーです。

公開鍵をgithubに保存

cat id_rsa.pub

id_rsa.pubの内容をコピーして以下写真のkeyにペーストして保存します。

截屏2022-03-16 22.01.02.png

ec2にgithubにssh接続確認

ssh -T git@github.com
The authenticity of host 'github.com (xxx.xxx.xxx.xxx)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
ECDSA key fingerprint is MD5:7b:99:81:1e:4c:91:a5:0d:5a:2e:2e:80:13:3f:24:ca.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,xxx.xxx.xxx.xxx' (ECDSA) to the list of known hosts.
Hi xxxxxx! You've successfully authenticated, but GitHub does not provide shell access.

You've successfully authenticatedメッセージが出れば、成功!

2
1
1

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
2
1