Just do IT

思うは招く

Capistrano で SSH 接続ができないときの対処

発生した問題1

Capistrano でデプロイしようとしたら、次のエラーが出た。

OpenSSH keys only supported if ED25519 is available (NotImplementedError)
net-ssh requires the following gems for ed25519 support:
 * ed25519 (>= 1.2, < 2.0)
 * bcrypt_pbkdf (>= 1.0, < 2.0)
See https://github.com/net-ssh/net-ssh/issues/565 for more information
Gem::LoadError : "ed25519 is not part of the bundle. Add it to your Gemfile."

解決

以下の2つのgemを追加する。

group :development do
  ~
  # 追加
  gem 'ed25519' 
  gem 'bcrypt_pbkdf'

そしてbundle。

発生した問題2

次にデプロイしようとしたら、次のエラーが出た。

git exit status: 128 (SSHKit::Command::Failed)
git stdout: Nothing written
git stderr: git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

原因

デプロイ先のリモートサーバで秘密鍵・公開鍵の作成管理が行われていないため、デプロイ先のサーバーからgithubに接続できない。

解決

リモートサーバから秘密鍵を使えるような設定にする。 そのため、まずは自分のssh-keyをssh-agentで使えるようにするため、ローカルで次のコマンドを打つ。

# ssh-agentに鍵を登録
ssh-add ~/.ssh/id_rsa

また、config/deploy/production.rbに次を記述することも忘れない。

server "133.167.121.197", user: "deploy", roles: %w{web db app}
set :ssh_options, {
    user: "deploy",
    port: 10022,
    keys: %w(~/.ssh/id_rsa),
    forward_agent: true,
    auth_methods: %w[publickey]
  }

ssh_optionsを書くことによって、Capistranoはコマンド実行者のssh-keyを使ってデプロイをしてくれる。

参考