動機

 ご存知の通り、sshには接続先とパスワードが必要。
 しかし、パスワード入力は結構手間だし、人間が毎回見ることでパスワードが流出する危険などがある。
 そこで証明書(実際は公開鍵と秘密鍵)を使うと、パスワードを使わずにサーバに接続ができるようになる。

証明書の作成を実施

 ~ » ssh-keygen -t rsa -f ~/.ssh/donotfree -b 4096 -C "yutaka@wclab.jp"
 Generating public/private rsa key pair.
 Enter passphrase (empty for no passphrase):
 Enter same passphrase again:
 Your identification has been saved in /Users/yutakaf/.ssh/donotfree.
 Your public key has been saved in /Users/yutakaf/.ssh/donotfree.pub.

作成された内容が確認できる。

作成された確認

 ~ » ls ~/.ssh/
 config donotfree donotfree.pub id_ed25519 id_ed25519.pub known_hosts
 ~ »

donotfree のもとサーバにアクセスする。

先にフィンガープリントを取得しておく(接続先の確認)

 ~ » ssh yutaka@xenon.donotfree.com
 The authenticity of host 'xenon.donotfree.com (3.114.24.142)' can't be established.
 ECDSA key fingerprint is SHA256:lnQhwqNoUChqmL7/3AZDgTdWgGRcycOpxnRwYmmp7/8.
 Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
 Warning: Permanently added 'xenon.donotfree.com' (ECDSA) to the list of known hosts.
 yutaka@xenon.donotfree.com's password:
 Last login: Sat Jul 31 23:29:12 2021 from p971012-ipoe.ipoe.ocn.ne.jp
 -bash-4.2$ exit
 ログアウト
 Connection to xenon.donotfree.com closed.

証明書のコピー

接続先サーバの自分のホームディレクトリに公開証明書をコピーする。

 ~ » ssh-copy-id -i ~/.ssh/donotfree yutaka@xenon.donotfree.com
 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/yutakaf/.ssh/donotfree.pub"
 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
 /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

ログインテスト

 ~ » ssh -i ~/.ssh/donotfree yutaka@xenon.donotfree.com
 -bash-4.2$ exit
 ログアウト
 Connection to xenon.donotfree.com closed.
 ~ » ssh -i ~/.ssh/donotfree yutaka@rm.donotfree.com
 Last login: Sun Aug  1 00:07:48 2021 from p971012-ipoe.ipoe.ocn.ne.jp
 -bash-4.2$ exit
 ログアウト
 ~ » ssh -i ~/.ssh/donotfree yutaka@calico.donotfree.com
 Last login: Sun Aug  1 00:08:05 2021 from p971012-ipoe.ipoe.ocn.ne.jp
 -bash-4.2$ exit
 ログアウト

上記接続が証明書(パスワードを使わない)でできるようになった。

ssh/config の設定

ちなみに、いちいち色々な条件を指定すると面倒なので、下記で省略ができる。

/.ssh/config

 Host xenon.donotfree.com
   HostName xenon.donotfree.com
   User yutaka
   Port 22
   IdentityFile ~/.ssh/donotfree
 ~ » ssh yutaka@xenon.donotfree.com
 -bash-4.2$ exit
 ログアウト

楽にログインできるようになった。やったね。

参考

 https://www.ssh.com/academy/ssh/keygen
 https://www.attachmate.com/ja-jp/documentation/reflection-desktop-v16/rdesktop-guide/data/ssh-keygen_command_rf.htm
 https://orumin.blogspot.com/2020/05/ssh-rsa.html
 https://qiita.com/kazokmr/items/754169cfa996b24fcbf5
 https://qiita.com/suthio/items/2760e4cff0e185fe2db9

筆者

藤井 穣 yutaka.fujii@worldcraft.co.jp


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS