よく忘れて、その度に調べる羽目になるので、記事にします。
ssh-keygen
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
保存する場所を聞かれます。
Enter file in which to save the key (/Users/user_name/.ssh/id_rsa):
適当にディレクトリ作ってサービスごと(githubとかbitbucketとか)に保存するのがベターな気がします。
$ mkdir -p ~/.ssh/github`をした後に、~/.ssh/github/id_rsaと入力しましょう。
パスワードを聞かれます。設定しておきましょう。
Enter passphrase (empty for no passphrase):
githubやbitbucketに登録
pbcopy < ~/.ssh/github/id_rsa.pub
公開鍵を登録しましょう。
vim config
以下のような感じでconfigファイルを書いておきましょう。役立ちます。
$ vi ~/.ssh/config
Host github.com
User git
Port 22
HostName github.com
identityFile ~/.ssh/github/id_rsa
TCPKeepAlive yes
IdentitiesOnly yes
ForwardAgent yes
どう役立つかというと、こんな風に書きたくないから設定しておこうって感じです。
$ ssh ユーザー名@ホスト名 -i ~/.ssh/鍵の名前 -p ポート番号
もう少し詳しく知りたい方はこちらの記事を読むといいです。
https://qiita.com/passol78/items/2ad123e39efeb1a5286b
ssh-add
いちいちパスワードを入力したくないので、ssh-agentに作った秘密鍵を登録しておきましょう。
$ ssh-add -K ~/.ssh/github/id_rsa
登録できたか確認。
$ ssh-add -l
最後に接続できるか試しましょう。
$ ssh github.com