当使用SSH推送到Gitea时要求密码?

3

我刚刚在Ubuntu服务器上搭建了自己的gitea服务(服务器是以用户gitea运行的)。已完成以下步骤:

  1. 在我的Windows电脑上生成ssh密钥,存储在C:\ Users \ <user_name> \.ssh中,即id_rsa_giteaid_rsa_gitea.pub
  2. 复制并设置公钥到我的gitea帐户设置页面中

该目录中存在我的GitHub帐户的密钥文件,即id_rsaid_rsa.pub。我修改了同一目录中的config文件。

Host github.com
  HostName github.com
  User git
  IdentityFile C:/Users/<user_name>/.ssh/id_rsa
  IdentitiesOnly yes
 
Host 192.168.200.101
  HostName 192.168.200.101
  User gitea
  IdentityFile C:/Users/<user_name>/.ssh/id_rsa_gitea
  IdentitiesOnly yes

在GitHub上推送正常。但是当我尝试推送到gitea时,出现问题。

$ git push -u origin master
gitea@192.168.200.101's password:
Permission denied, please try again.
gitea@192.168.200.101's password:
Permission denied, please try again.
gitea@192.168.200.101's password:
\302\226gitea@192.168.200.101: Permission denied (publickey,password).
fatal: Could not read from remote repository.

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

我已在Git Bash控制台中使用ssh -v gitea@192.168.200.101测试了SSH连接,结果如下:

OpenSSH_7.6p1, OpenSSL 1.0.2m  2 Nov 2017
debug1: Reading configuration data /c/Users/admin/.ssh/config
debug1: /c/Users/admin/.ssh/config line 26: Applying options for 192.168.200.101
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 192.168.200.101 [192.168.200.101] port 22.
debug1: Connection established.
debug1: identity file C:/Users/admin/.ssh/id_rsa_gitea type 0
debug1: key_load_public: No such file or directory
debug1: identity file C:/Users/admin/.ssh/id_rsa_gitea-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.10
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.10 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.200.101:22 as 'gitea'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: aes128-ctr MAC: umac-64-etm@openssh.com compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: umac-64-etm@openssh.com compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:YqpRDueradBcei52m4ahex5DgTOwI3QvgJohoZSMzTs
debug1: Host '192.168.200.101' is known and matches the ECDSA host key.
debug1: Found key in /c/Users/admin/.ssh/known_hosts:23
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:DCCnO6UzUiXYhZiNxeaS4SV05fIUZhHK/ZGDPPI6cwc C:/Users/admin/.ssh/id_rsa_gitea
debug1: Server accepts key: pkalg rsa-sha2-512 blen 535
debug1: Authentication succeeded (publickey).
Authenticated to 192.168.200.101 ([192.168.200.101]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Remote: Forced command.
debug1: Remote: Port forwarding disabled.
debug1: Remote: X11 forwarding disabled.
debug1: Remote: Agent forwarding disabled.
debug1: Remote: PTY allocation disabled.
debug1: Remote: Forced command.
debug1: Remote: Port forwarding disabled.
debug1: Remote: X11 forwarding disabled.
debug1: Remote: Agent forwarding disabled.
debug1: Remote: PTY allocation disabled.
PTY allocation request failed on channel 0
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
debug1: channel 0: free: client-session, nchannels 1
Connection to 192.168.200.101 closed.
Transferred: sent 3720, received 3624 bytes, in 0.2 seconds
Bytes per second: sent 15126.0, received 14735.7
debug1: Exit status 1

我尝试了多种变化,但似乎都不起作用。有什么想法吗?

更新:使用http推送正常。我切换回ssh并尝试再次推送,现在出现:

Pushing to gitea@192.168.200.101:guanhuizhe/example-repo.git
fatal: Could not read from remote repository.

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

在这次推送期间,Ubuntu服务器的sshd日志如下:
Oct 16 18:08:29 DataStorage2 sshd[29981]: Accepted publickey for gitea from 192.168.200.141 port 14967 ssh2: RSA SHA256:DCCnO6UzUiXYhZiNxeaS4SV05fIUZhHK/ZGDPPI6cwc
Oct 16 18:08:29 DataStorage2 sshd[29981]: pam_unix(sshd:session): session opened for user gitea by (uid=0)
Oct 16 18:08:29 DataStorage2 sshd[30017]: Received disconnect from 192.168.200.141 port 14967:11: disconnected by user
Oct 16 18:08:29 DataStorage2 sshd[30017]: Disconnected from 192.168.200.141 port 14967
Oct 16 18:08:29 DataStorage2 sshd[29981]: pam_unix(sshd:session): session closed for user gitea

更新 v2:

问题是由我用于运行 gitea 二进制文件的 Ubuntu 用户引起的。该用户没有 shell。我删除了该用户并配置了一个正常的用户。一切正常运行。谢谢大家!!


一个更好的测试是 ssh -v 192.168.200.101,因为它还会检查 ~/.ssh/config 中的用户是否正确。 - Thomas
@AThaBlen 当您执行 ssh -Tv gitea 命令时,是否会看到 Hi there, xxx! You've successfully authenticated with the key named yyy 的提示信息?(假设您已按照答案中的建议将 Hoist 条目重命名) - VonC
@VonC 不是...但是当我尝试 ssh -Tv git@github.com 时,我可以看到这个。 - AThaBlen
只要您看不到这个消息,gitea身份验证就无法正常工作。这是需要解决的第一个问题。 - VonC
让我们在聊天中继续这个讨论 - VonC
显示剩余3条评论
1个回答

0

\302\226gitea@192.168.200.101 表示本地 git 配置文件 .git/config 中的 origin 并非为 gitea,而是 <START OF GUARDED AREA>gitea

你可以通过在 ~/.ssh/config 中更改你的 Host 条目来将其更改为 "gitea"(比 192.168.200.101 更易于输入)

可以使用以下命令进行测试:

ssh -Tv gitea

还要检查~gitea/.ssh/authorized_keys文件,确保公钥已由gitea添加,并在SSH强制命令行中。
您应该看到类似于以下内容:

command="/path/to/gitea --config='/path/to/app.ini' serv key-2",\
   no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty \
   ssh-rsa <yourPublicKey>

如果您想使用您的~/.ssh/config条目,那么您需要更改您的远程设置:
cd /path/to/repo
git remote set-url origin gitea:<me>/myrepo

谢谢您的回复!我检查了我的配置文件和authorized_keys文件中的空格,看起来是这样的:command="/home/gitea/gitea --config="/home/gitea/custom/conf/app.ini" serv key-4",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty <我的公钥>它仍然无法工作QAQ。我已经更新了问题。 - AThaBlen
@AThaBlen,你的远程代码有问题:我已经编辑了我的回答。 - VonC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接