1
0

More than 5 years have passed since last update.

CentOSのAMIをPackerで作ろうとしたときに公開鍵がないと言われて困ってるなう

Posted at

解決できてないから助けてください記事です。助けてください(泣)。

症状

やむおえずCentOSを使っていて、packerでAMIを作ろうとしたら

こんなかんじで
packer build server.json
エラーがー
...
==> amazon-ebs: Error waiting for SSH: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
...

鍵がないよって言われてます。

AWS上のCentOSの鍵の設定は特殊

AWSで動いているCentOSは、/etc/rc.localに以下のような記述があります。

/etc/rc.local
# Get the root ssh key setup
ReTry=0
while [ ! -f /root/.ssh/authorized_keys ] && [ $ReTry -lt 10 ]; do
  sleep 2
  curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /root/.ssh/authorized_keys
  ReTry=$[Retry+1]
done
chmod 600 /root/.ssh/authorized_keys && restorecon /root/.ssh/authorized_keys

つまり、元から公開鍵を設定してない限り/etc/rc.localが実行されることで公開鍵が設定されます。

参考:http://dev.classmethod.jp/etc/centos-keypair/

/etc/rc.localが実行されるのはsshが起動した後

サービスが起動するのには順番があり、確認してみると

/etc/rc2.d
... S55sshd ... S99local

となっていてsshdが先に起動してます。
つまり、sshが起動した後に、公開鍵が設定されています。

つまりこうなってるんじゃない仮説

ここまで調べた結果、ここからは私の予想です。
packerはsshが有効になったかどうかしか見ていないのでは?

CentOS 「sshd起動!」
packer 「sshが使える!いけー!!!」
CentOS 「えっと鍵をですね...」
packer 「鍵ないじゃん none publickey」
CentOS 「」

ってなってるんじゃないかと。

sshをし始めるまでの時間を設定したりしたい

該当するようなパラメータは現状見つけられず...
"sshが使える状態になるまでどれだけ待つか"を設定していると思われるssh_timeoutはある
https://www.packer.io/docs/templates/communicator.html#ssh_timeout

誰か助けてください(号泣)

1
0
7

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