LoginSignup
0
1

More than 3 years have passed since last update.

新しいmacでgitを設定(公開鍵作成〜SSH接続)

Last updated at Posted at 2019-11-09

gitのインストール、
秘密鍵・公開鍵のペア作成からSSH接続までをメモ
無駄なものを省いたシンプルな設定

Homebrewの導入(rudyを使う場合)

macOS用パッケージマネージャー
Homebrewの導入

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

macのOSを大文字・小文字で区切っている場合、Warningメッセージが表示される

#導入できたか確認
brew doctor

「Your system is ready to brew.」と表示されれば完了

#Ver確認
brew -v

スクリーンショット 2019-11-09 17.56.55.png

gitのインストール

brew install git
#Ver確認
git --version

スクリーンショット 2019-11-09 18.00.45.png

gitアカウントの登録

git config --global user.name
git config --global user.email
#確認
git config --list

sshディレクトリの作成

ホーム配下に権限を付けて.sshディレクトリを作成

#現在のユーザーで作成
mkdir ~/.ssh 
#所有者に読み・書き・実行権限を付与
chmod 700 ~/.ssh 
#ディレクトリ間の移動
cd ~/.ssh

SSH認証キーの作成

ssh-keygenで、RSA方式の暗号鍵を作成

ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa

-t「鍵の種類を指定」
-b「生成する鍵のビット数を指定」
   RSA 鍵の場合、最小のサイズは 1024 ビットであり、デフォルトは 2048 ビット
-C「コメントを追加」
-f「鍵を格納するファイル名を指定」

macのパスを2回も止められるので入力
randomartにより鍵が作成される
スクリーンショット 2019-11-09 18.18.45.png

#秘密鍵の確認はコレ
cat ~/.ssh/id_rsa

秘密鍵のパーミッションを600に変更

chmod 600 ~/.ssh/id_rsa

ls-laで鍵の作成を確認

#確認
ls -la ~/.ssh/id_rsa

-rw----- で始まる表示があれば正しく作成されている

#.sshの中身を確認する場合
ls ~/.ssh

~/.ssh/configに接続設定を追加

vi ~/.ssh/config

#以下を追加
Host *
  AddKeysToAgent yes
  UseKeychain yes

Host github
  HostName github.com
  IdentityFile ~/.ssh/id_rsa
  Port 22
  User git

Gitへの接続を確かめる

公開鍵をgitに設定し、gitへの接続を確認する

#以下でコピーしてgithubへ設定
pbcopy < ~/.ssh/id_rsa.pub
ssh -T git@github.com

実行後に、以下の文が返ってきたら接続完了

Hi (アカウント名)! You've successfully authenticated, but GitHub does not provide shell access.

以上

0
1
0

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