LoginSignup
9
15

More than 3 years have passed since last update.

公開鍵と秘密鍵を使ったSSH接続 (macOS Mojave)

Last updated at Posted at 2018-11-07

はじめに

公開鍵と秘密鍵を使ったSSH接続の設定方法を解説。

手順

1: クライアント側)鍵を入れるディレクトリを作成

$ cd
$ mkdir .ssh

2: クライアント側)公開鍵と秘密鍵のペアを作成
(-C '' を使うとデフォルトコメントを消せます)
(-f を使うとファイル名を指定できます)

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/motofumi/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 
Enter same passphrase again:
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.

3: サーバ側)ディレクトリとファイルを用意する

$ mkdir ~/.ssh
$ chmod 700 ~/.ssh
$ touch ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys

4: クライアント側)公開鍵をサーバにアップロード

scp ~/.ssh/id_rsa.pub username@example.com:~/.ssh/

5: サーバ側)公開鍵の内容をauthorized_keysに追記

$ cat ~/.ssh/id_rsa.pub >> authorized_keys
$ rm ~/.ssh/id_rsa.pub

6: クライアント側)ssh接続できればOK

$ ssh -i ~/.ssh/id_rsa username@example.com

!!注意!!
間違えて設定すると詰みます。
(二度とサーバ接続できなくなる可能性あり)
7: サーバ側)パスワード認証を切って鍵認証だけにする。

/etc/ssh/sshd_configファイルに追記
PasswordAuthentication no
ChallengeResponseAuthentication no

8: システム環境設定の共有のリモートログインをOFFにしてONする。

オプション

option: SSH鍵をssh-agentに登録と確認

$ ssh-add /Users/user_name/.ssh/id_rsa
$ ssh-add -l

リファレンス

9
15
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
9
15