linux - 外部端口/ip 上的 ssh(和 git)身份验证问题(本地 ip 工作正常)

标签 linux git networking ssh public-key-encryption

我正在尝试在可通过互联网远程访问的服务器计算机上设置一个 git 存储库。

我已经成功地让 git 在本地/内部 IP 地址上工作。在本地 LAN 中,我有基于私钥的身份验证用于 SSH(禁用密码登录),并且我可以使用 Git 和 SSH 成功克隆、推送和 pull ,例如;

ssh USER@192.168.1.xxx
[-> accepts public key, gives me a remote console prompt as "USER", etc]

git clone git+ssh://USER@192.168.1.xxx//gitdir/project.git
[-> creates a local clone as desired, commits and push work, no problems seemingly]

但是,我现在正尝试以同样的方式通过外部/互联网 IP 访问这台机器,但我不明白它给我的行为。

  • 我在我的路由器上启用了端口转发,将端口 22 连接到服务器机器。
  • 我已经在服务器计算机上的“UFW”上的软件中打开了端口 22。
  • 据我所知,我的路由器、SSH 配置或 UFW 上没有任何设置会阻止任何特定的网址或以其他方式导致我的本地计算机或服务器出现问题。服务器应该接受来自任何通过端口 22 访问的外部地址的连接,并且我的本地计算机和服务器通常都允许传出连接。

  • 如果重要的话,我在本地计算机上使用 Ubuntu 17.04。

  • 服务器和本地机器目前都在同一个局域网/连接到同一个路由器。

  • 我在我的“sshd_config”中禁用了“ChallengeResponseAuthentication”和“PasswordAuthentication”,并启用了“PubkeyAuthentication”。

  • 我已四重检查我使用的 IP 是否正确,并使用复制粘贴而不是手动输入。除非我真的遗漏了什么,否则我会尝试连接到正确的机器。

对于直接 SSH 登录,我得到这个(使用 -v 标志):

LOCALUSER@LOCALMACHINE:~$ ssh -v -i ~/.ssh/[PRIVATE_KEY] USER@[IP6_EXTERNAL_IP]
OpenSSH_7.4p1 Ubuntu-10, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /home/[HOME]/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to [IP6_EXTERNAL_IP] [[IP6_EXTERNAL_IP]] port 22.
debug1: Connection established.
debug1: identity file /home/[HOME]/.ssh/[PRIVATE_KEY] type 4
debug1: key_load_public: No such file or directory
debug1: identity file /home/[HOME]/.ssh/[PRIVATE_KEY]-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4p1 Ubuntu-10
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4p1 Ubuntu-10
debug1: match: OpenSSH_7.4p1 Ubuntu-10 pat OpenSSH* compat 0x04000000
debug1: Authenticating to [IP6_EXTERNAL_IP] as 'USER'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
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: ecdsa-sha2-nistp256 SHA256:[SHA_HASH]
debug1: Host '[IP6_EXTERNAL_IP]' is known and matches the ECDSA host key.
debug1: Found key in /home/[HOME]/.ssh/known_hosts:4
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,ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering ED25519 public key: /home/[HOME]/.ssh/[PRIVATE_KEY]
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: password
USER@[IP6_EXTRNAL_IP]'s password: 

...而且我提供的密码均无效。我不知道为什么服务器甚至要求输入密码,我在“sshd_config”中全局禁用了密码登录,并且我已经三重检查该用户也没有异常(exception)。

当我尝试通过外部 IP 从服务器克隆时,Git 同样会询问我的远程用户密码(因为它当然是通过 SSH 工作),如果我执行以下操作将其设置为使用外部 IP,而不是原本地IP:

git remote set-url origin ssh+git://USER@[REMOTE_IP]//gitdir/project.git

如果能帮助我理解这里遗漏的内容,我们将不胜感激;谢谢。

最佳答案

想通了:

我不清楚为什么,但是当我尝试从我自己的 LAN 内部通过外部公共(public) IP 访问我的服务器时,我的系统不喜欢它。我认为这是由于某种数据包混淆,因为本地机器和服务器机器通过路由器共享相同的公共(public) ip(对此确认将不胜感激)。

当我尝试从另一个网络连接时,一切正常并按预期进行。我被要求提供我的公钥,如果我没有提供正确的密码,我会立即被拒绝并且没有密码提示,并且在我更改我的 repo 源以使用公共(public) ip 之后,SSH 和通过 SSH 的 git 似乎都可以在互联网上按预期工作.编辑 ~/.ssh/config 以设置关键身份和主机,涵盖了任何进一步的细节。

标记为已解决。谢谢。

关于linux - 外部端口/ip 上的 ssh(和 git)身份验证问题(本地 ip 工作正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45587384/

相关文章:

linux - 在 X.Org 中控制对显卡的访问

git - 由于 ssh 和 https 连接的私有(private) git 存储库,npm 安装失败

linux - 如何让 Ubuntu 成功搜索许可证的环境变量?

linux - ifupdown 更改参数 dhclient

c - 尝试从内核空间将结构写入 proc 文件

linux - 使用 LInux 的 Bash 命令

c# - 防止 .Net Core 5 控制台应用程序在 Linux 作为服务运行时结束的推荐方法

git - 从旧提交创建新的存储库,保留当时的历史记录

git - 无论我做什么,如何使用错误的 .ssh 目录修复 Cygwin?

c++ - 计算 ICMP 数据包校验和