あそぶ技術

フィヨルドブートキャンプでRuby on railsの学習をしている僕の学習ブログです。

SSH公開鍵が送れない

公開鍵が送れない!

SSHでのログインが急に出来なくなったのでdebianを再インストールし再びSSHの設定をしています。

そして、sshの公開鍵を送るところが上手く出来ずハマりました。

つらみ

これはその時のログです。

最後に成功例も載ってます。

f:id:zeroringo:20180822170044p:plain

4回の失敗例

本当はもっと失敗しています。

f:id:zeroringo:20180822170140p:plain

イェー

失敗例 1

入力コマンド

ssh-copy-id -i ~/.ssh/client_rsa.pub

全文

hiyotatu8MBP:.ssh hiyoshitatsuya$ ssh-copy-id -i ~/.ssh/client_rsa.pub
/usr/local/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/hiyoshitatsuya/.ssh/id_rsa.pub"
/usr/local/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/local/bin/ssh-copy-id: ERROR: ssh: Could not resolve hostname /users/hiyoshitatsuya/.ssh/client_rsa.pub: nodename nor servname provided, or not known

f:id:zeroringo:20180821134021p:plain

原因

  • スペルミス 転送するキーはclient_rsa.pubではなくid_rsa.pubなのでシンプルに間違ってる。
  • 接続先サーバーのアドレスと公開鍵を送るユーザーの指定が無い
  • そりゃ送れんわ

失敗例 2

入力コマンド

ssh-copy-id -i~/.ssh/id_rsa.pub
hiyotatu8MBP:.ssh hiyoshitatsuya$ ssh-copy-id -i~/.ssh/id_rsa.pub

/usr/local/bin/ssh-copy-id: ERROR: failed to open ID file '~/.ssh/id_rsa.pub': No such file or directory

f:id:zeroringo:20180821133929p:plain

原因

  • id_rsa.pubにはなっているので鍵のスペルは合っている
  • -iはItentitiyFileの事らしい、良く解らないがsshの転送コマンドに良く書かれている
  • -iの後に半角スペースが無い
  • 接続先サーバーのアドレスと公開鍵を送るユーザーの指定が無い

ItentitiyFileについて

ItentitiyFileについては調べてみるも良く解らなかった。

一番最初に読み込ませるユーザー用にカスタマイズされた設定なのかな?

一応調べた資料を残しておきます。

man ssh-copy-idでマニュアルを見てみる

f:id:zeroringo:20180822160025p:plain

こんな感じ。

Identitiyについて

identify / identification / identifier / identity

そのユーザーが本人かどうか識別するのをidentify。動詞。

識別する行為そのものはidentification。identifyの名詞形。識別、ID、身分証。

そのユーザーを特定するために用い情報をidentifier。識別子。氏名、変数名など。

そのユーザーを特徴付けるあらゆる情報をidentity。アイデンティティ、同一性。

つまりどう言うことだってばよ!

失敗例 3

入力コマンド

ssh-copy-id ~/.ssh/id_rsa.pub

全文

hiyotatu8MBP:.ssh hiyoshitatsuya$ ssh-copy-id ~/.ssh/id_rsa.pub
/usr/local/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/local/bin/ssh-copy-id: ERROR: ssh: Could not resolve hostname /users/hiyoshitatsuya/.ssh/id_rsa.pub: nodename nor servname provided, or not known

f:id:zeroringo:20180821133536p:plain

原因

  • 1つ目の失敗例と違いスペルは合っている。
  • が、接続先サーバーのアドレスと公開鍵を送るユーザーの指定が無い
  • まあ送れないですよね。

失敗例 4s

入力コマンド

ssh-copy-id ~/.ssh/id_rsa.pub hiyotatu8@153.126.184.96:~/.ssh/authorized_keys

全文

hiyotatu8MBP:.ssh hiyoshitatsuya$ ssh-copy-id ~/.ssh/id_rsa.pub hiyotatu8@153.126.184.96:~/.ssh/authorized_keys
/usr/local/bin/ssh-copy-id: ERROR: Too many arguments.  Expecting a target hostname, got: '/Users/hiyoshitatsuya/.ssh/id_rsa.pub' 'hiyotatu8@153.126.184.96:~/.ssh/authorized_keys'

Usage: /usr/local/bin/ssh-copy-id [-h|-?|-f|-n] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname
    -f: force mode -- copy keys without trying to check if they are already installed
    -n: dry run    -- no keys are actually copied
    -h|-?: print this help

f:id:zeroringo:20180821133450p:plain

原因

  • 公開鍵の名前も合っているし、送り先の情報も書いてある。
  • 最後に付け足してある:~/.ssh/authorized_keysのせいで送れなかったのだと思う
  • どこかのサイトから拾ってきたのだと思う(記録は残していなかった)
  • 送った鍵の名前をauthorized_keysに変更したかったのだと思う
  • しかし、ssh-copy-idコマンドで鍵を転送した場合は指定しなくても転送された公開鍵の名前がauthorized_keysに変更されるので蛇足ですね。

成功例

入力コマンド

ssh-copy-id -i ~/.ssh/id_rsa.pub hiyotatu8@153.126.184.96

全文

hiyotatu8MBP:.ssh hiyoshitatsuya$ ssh-copy-id -i ~/.ssh/id_rsa.pub hiyotatu8@153.126.184.96
/usr/local/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/hiyoshitatsuya/.ssh/id_rsa.pub"
The authenticity of host '153.126.184.96 (153.126.184.96)' can't be established.
ECDSA key fingerprint is SHA256:sMz3HIDgnq5XxN+gKfFWYHalH8zsmoKC4ZJdXpNtmyU.
Are you sure you want to continue connecting (yes/no)? yes
/usr/local/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/local/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
hiyotatu8@153.126.184.96's password: 

Number of key(s) added:        1

Now try logging into the machine, with:   "ssh 'hiyotatu8@153.126.184.96'"
and check to make sure that only the key(s) you wanted were added.

f:id:zeroringo:20180821132823p:plain

こんな感じ

-iについて’は良く解らないけれど過不足無く必要な情報があるので上手く転送出来たんでしょう。

一応さくらVNCコンソールでSSH公開鍵がちゃんと転送されてるか確かめてみる。

f:id:zeroringo:20180821133215p:plain

ありました。

やったー

まとめ

なんか疲弊したけど良い復習になりました。

改めて見てみるとしょーもないミス集ですね。恥ずかしいわー

最近ハマっても新しいブログのネタだなこれと思える精神になってきています。嫌なものは嫌だけれどもな!!

後、こまめにログを取るの良いです。

自分のミスを後で振り返り原因を明確にする作業は爽快感があります。

sshに関するログが溜まってきたのでそれで3記事ぐらい作れそうです。

この後、sshd.configでもハマっています。何で設定効かないねん。

チックショー!!

f:id:zeroringo:20180822170333p:plain