githubを使い始めた。サーバからコマンドでgithubへアップロードしたいと思い、作業したログ。

おおはまりした。(^_^;

サーバは、AWS EC2 の Amazon Linux AMI 64bit

#contents

*公開鍵と秘密鍵を作成 [#ub8e7ba2]

githubの[[マニュアル:https://help.github.com/articles/generating-ssh-keys#platform-linux]]を参考に作成

 $ ssh-keygen -t rsa -C "your_email@example.com"
 Generating public/private rsa key pair.
 Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa):  ←そのままEnter
 Enter passphrase (empty for no passphrase): ←パスワードを入力
 Enter same passphrase again: ←再度パスワードを入力

**ディレクトリ .ssh に公開鍵と秘密鍵が作成される。名前を分かりやすい名前に変更 [#y45cc840]

 $ mv ~/.ssh/id_rsa ~/.ssh/github_id_rsa
 $ mv ~/.ssh/id_rsa.pub ~/.ssh/github_id_rsa.pub

**パーミッション変更 [#v2626527]

 $ chmod 600 ~/.ssh/github*

*githubに公開鍵を登録 [#s3f2be18]

**サーバの公開鍵をコピー [#l58adfcb]

 $ cat ~/.ssh/github_id_rsa.pub
 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHpJxdA+ql5mLyaWLVYf7iYp21gSTRVsVNeHicypuG
 hogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehoge
 hogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehoge
 hogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehogehoge
 Jh3XqjVjnMnbTwKI+urASyjIFwiQOWxQRJv+lskF7TpBPkGShlCuPz+UThNd your_email@example.com

ssh-rsa から your_email@example.com までのテキストをクリップボードにコピー

**githubのサイトに公開鍵を登録 [#fc6232a3]

[[github:https://github.com/]]のサイトにブラウザでアクセス

***自分の「Account Settings」にアクセス [#yc586901]

#ref(1-1.png)

***左側のメニューの「SSH Keys」をクリック [#efc1ef48]

#ref(1-2.png)

***「Add SSH key」をクリック [#e9fd5149]

#ref(1-3.png)

***「Key」に公開鍵のテキストを貼り付け [#y2b439bf]

#ref(1-4.png)

***「Add key」をクリック [#e65aed7b]

#ref(1-5.png)

***パスワードを入力 [#s2fb6758]

#ref(1-6.png)

''このパスワードは、githubのパスワード。秘密鍵のパスワードでは無いことに注意。''

*接続テスト [#of4f66d5]

私はここではまった。

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

と表示されればOK!

私はここで間違った。@より前は、ユーザー名だと勘違いして以下の様にタイプ

hogehogeがユーザー名だとすると

 $ ssh -T hogehoge@github.com
 Permission denied (publickey).

とタイプして、Permission deniedというエラーがでた。

なぜPermission deniedになるのか、正月休みをまるまる1日つぶして試行錯誤した。

後日、githubのヘルプを良く読み返すと以下の様に書いてあった。

 To make sure everything is working you'll now SSH to GitHub. 
 When you do this, you will be asked to authenticate this action using your password, 
 which for this purpose is the passphrase you created earlier. 
 Don't change the git@github.com part. That's supposed to be there.

git@github.com を変更するなと書いてあるじゃない。

ああ、注意力散漫、さらに英語が苦手なおかげで痛い目にあった。
後から思うと、Permission deniedでGoogle検索しても的外れな情報しか出てこない。誰もここで間違う人はいないからだろう。

*SSHの設定を追加 [#a6ade23f]

サーバ毎にどの秘密鍵を使うか設定しておく

 $ vi ~/.ssh/config

 Host github.com
  HostName      github.com
  IdentityFile  ~/.ssh/github_id_rsa
  User          git

ここでユーザー名を自分のユーザー名に換えてはいけない。私はここでもはまった。

ユーザー名は git ままでよい。

*github から既存のリポジトリのクローンを作成 [#oe2798fd]

ユーザー名が hogehoge 、リポジトリの名前が ss_script の場合

ディレクトリ ~/tmp にクローンを作成

 $ cd ~/tmp
 $ git clone git@github.com:hogehoge/ss_script.git
 
ディレクトリ ~/tmp/ss_script が作成されて、その配下にソースがコピーされた。

*ソースの修正をして、push(更新) [#t59628f1]

ユーザー名が hogehoge 、リポジトリの名前が ss_script の場合

 $ cd ~/tmp
 $ cd ss_script
 $ vi README ← 例えばREADMEを変更した場合
 $ git add --all
 $ git commit -m "update" ← コメントは適宜
 $ git push origin master

パスワードを求められるので、秘密鍵のパスワードを入力

 Counting objects: 5, done. 
 Compressing objects: 100% (3/3), done.
 Writing objects: 100% (3/3), 370 bytes | 0 bytes/s, done.
 Total 3 (delta 1), reused 1 (delta 0)
 To git@github.com:hogehoge/ss_script.git
   8ce34e2..07d2a1d  master -> master

こんな表示がでれば更新成功!



SSHの設定を追加

[[その他]]
トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS