Capistrano Rails应用程序部署失败。服务器无法向Github标识自己。权限被拒绝(公钥)

我正在尝试使用Capistrano将Rails应用程序部署到服务器。

在我的本地环境中,我运行了“ 生产上限部署”,但出现错误。

    12: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:31:in `run'
    11: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:31:in `instance_exec'
    10: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/capistrano-3.11.2/lib/capistrano/scm/tasks/git.rake:18:in `block (3 levels) in eval_rakefile'
     9: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:100:in `with'
     8: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/capistrano-3.11.2/lib/capistrano/scm/tasks/git.rake:19:in `block (4 levels) in eval_rakefile'
     7: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/capistrano-3.11.2/lib/capistrano/scm/git.rb:38:in `check_repo_is_reachable'
     6: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/capistrano-3.11.2/lib/capistrano/scm/git.rb:77:in `git'
     5: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:80:in `execute'
     4: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:148:in `create_command_and_execute'
     3: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:148:in `tap'
     2: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/sshkit-1.20.0/lib/sshkit/backends/abstract.rb:148:in `block in create_command_and_execute'
     1: from /Users/rainbowtape/.rvm/gems/ruby-2.6.5/gems/sshkit-1.20.0/lib/sshkit/backends/netssh.rb:170:in `execute_command'
/Users/-------------/.rvm/gems/ruby-2.6.5/gems/sshkit-1.20.0/lib/sshkit/command.rb:97:in `exit_status=': git exit status: 128 (SSHKit::Command::Failed)
git stdout: Nothing written
git stderr: git@github.com: Permission denied (publickey).

所以我去服务器端检查服务器是否可以与Github通信。它调出了没有。

deploy@xx.xx.xx.xx:~$ ssh -vT git@github.com
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3,OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: /etc/ssh/ssh_config line 52: Deprecated option "useroaming"
debug1: Connecting to github.com [140.82.118.4] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /home/deploy/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/deploy/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/deploy/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/deploy/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/deploy/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/deploy/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/deploy/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/deploy/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
debug1: Remote protocol version 2.0,remote software version babeld-2e9d163d
debug1: no match: babeld-2e9d163d
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: rsa-sha2-512
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /home/deploy/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-dss>
debug1: SSH2_MSG_SERVICE_accEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/deploy/.ssh/id_rsa
debug1: Trying private key: /home/deploy/.ssh/id_dsa
debug1: Trying private key: /home/deploy/.ssh/id_ecdsa
debug1: Trying private key: /home/deploy/.ssh/id_ed25519
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).

请注意,我已经使用'ssh-copy-id'将本地公用密钥复制到服务器了

deploy@xx.xx.xx.xx:~/.ssh$ ls -la
total 16
drwx------  2 deploy deploy 4096 Nov 27 01:55 .
drwxr-xr-x 10 deploy deploy 4096 Nov 27 05:18 ..
-rw-------  1 deploy deploy 3724 Nov 27 12:37 authorized_keys
-rw-r--r--  1 deploy deploy 1326 Nov 27 01:57 known_hosts

另外,我尝试将密钥添加到ssh-agent。但失败了。

deploy@xx.xx.xx.xx:~/.ssh$ eval "$(ssh-agent -s)"
Agent pid 4417
deploy@xx.xx.xx.xx:~/.ssh$ ssh-add ~/.ssh/authorized_keys
Error loading key "/home/deploy/.ssh/authorized_keys": invalid format

服务器端是否只有我的公共密钥。对?不是私钥。我在正确的轨道上吗?

更新1

建议@ZedTuX

我在服务器端生成了另一个ssh密钥集。并将公共密钥添加到github repo,以便它可以读取/写入。所以我在服务器端测试了git pull and push。有用。仍然出现此错误。当我尝试在本地部署时。

 DEBUG [1b7b3349]   error: could not lock config file config: Permission denied
 DEBUG [1b7b3349]   fatal: could not set 'remote.origin.url' to 'git@github.com:maskeduser1/repo1.git'

这对我来说没有意义,因为我什至可以--set-upstream到Github。为什么我会收到此错误?

fengruoyang 回答:Capistrano Rails应用程序部署失败。服务器无法向Github标识自己。权限被拒绝(公钥)

  

我已经复制了本地公用密钥

那不是它的工作方式。

首先,不要使用您的dev SSH密钥来部署到您的服务器。该密钥具有读写权限,这很危险。 如果有人介绍您的服务器,那么他将对您的Github存储库源代码具有完全的读/写访问权限。

请改用部署密钥(在Github上,“设置>部署密钥”)。 因此,您可以使用ssh-keygen在服务器上生成新的SSH密钥,然后在“部署密钥”页面中从服务器复制/粘贴公共SSH密钥。

之后,您应该可以进行部署。

更新

所以现在您遇到此错误:

DEBUG [1b7b3349]   error: could not lock config file config: Permission denied
DEBUG [1b7b3349]   fatal: could not set 'remote.origin.url' to 'git@github.com:maskeduser1/repo1.git'

能否请您显示以下内容的输出?

  • whoami
  • ls -al ~/.ssh
  • ls -al /<Capistrano :deploy_to>/repo

对于capistrano repo文件夹,请查看:deploy_to设置。如果您没有更改它,它应该在/var/www/文件夹中。

本文链接:https://www.f2er.com/3021930.html

大家都在问