Ubuntu 中通过 SSH 克隆 Windows 上的 Git 仓库

在 Ubuntu 中通过 SSH 克隆 Windows 上的 Git 仓库

根据您提供的信息(Windows 用户名:Administrator,仓库路径:E:\xuehy_share\xuehaiyang.git),以下是详细步骤:

1. 确保 Windows SSH 服务器配置正确

首先,确认 Windows 上的 OpenSSH 服务器已正确安装并运行:

powershell 复制代码
# 在 Windows PowerShell 中以管理员身份运行
Get-Service -Name sshd

如果服务未运行,启动它:

powershell 复制代码
Start-Service sshd
Set-Service -Name sshd -StartupType Automatic

2. 配置 Windows 防火墙

确保 Windows 防火墙允许 SSH 连接(默认端口 22):

powershell 复制代码
# 允许 SSH 通过防火墙
New-NetFirewallRule -Name "OpenSSH-Server" -DisplayName "OpenSSH Server (sshd)" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

3. 获取 Windows 机器的 IP 地址

您已经知道 IP 地址是 192.168.31.160,所以不需要额外步骤。

4. 在 Ubuntu 上测试 SSH 连接

在 Ubuntu 终端中测试到 Windows 的 SSH 连接:

bash 复制代码
ssh Administrator@192.168.31.160

系统会提示您输入 Administrator 用户的密码。如果能够成功登录,说明 SSH 连接正常。

5. 设置 SSH 密钥认证(推荐)

为了避免每次输入密码,设置 SSH 密钥认证:

在 Ubuntu 上生成 SSH 密钥:

bash 复制代码
ssh-keygen -t rsa -b 4096

将公钥复制到 Windows:

bash 复制代码
ssh-copy-id Administrator@192.168.31.160

6. 在 Ubuntu 上克隆仓库

使用正确的路径格式克隆仓库。Windows 路径 E:\xuehy_share\xuehaiyang.git 在 SSH 中需要转换为 /e/xuehy_share/xuehaiyang.git:

bash 复制代码
git clone ssh://Administrator@192.168.31.160/e/xuehy_share/xuehaiyang.git

7. 如果遇到权限问题

如果遇到权限问题,请检查:

  1. Windows 上仓库目录的权限:

    powershell 复制代码
    # 在 Windows PowerShell 中以管理员身份运行
    icacls "E:\xuehy_share\xuehaiyang.git" /grant "Administrator:(OI)(CI)(F)"
  2. 确保 Git 仓库已正确初始化(如果是裸仓库):

    bash 复制代码
    # 在 Windows 上使用 Git Bash
    cd /e/xuehy_share/xuehaiyang.git
    git init --bare

8. 替代方法:使用 SMB 挂载

如果 SSH 仍然有问题,可以考虑使用 SMB 挂载:

bash 复制代码
# 在 Ubuntu 上安装必要的包
sudo apt install cifs-utils

# 创建挂载点
sudo mkdir /mnt/xuehy_share

# 挂载 Windows 共享
sudo mount -t cifs //192.168.31.160/xuehy_share /mnt/xuehy_share -o username=Administrator,password=您的密码

# 然后克隆仓库
git clone /mnt/xuehy_share/xuehaiyang.git

故障排除提示

  1. 如果连接被拒绝,检查 Windows 上的 SSH 服务是否正在运行

  2. 如果认证失败,确保 Administrator 用户的密码正确

  3. 如果找不到仓库,确认路径是否正确

  4. 使用 -v 选项获取详细错误信息:

    bash 复制代码
    ssh -v Administrator@192.168.31.160
    git clone -v ssh://Administrator@192.168.31.160/e/xuehy_share/xuehaiyang.git

按照这些步骤,您应该能够在 Ubuntu 上成功克隆 Windows 上的 Git 仓库。如果仍然遇到问题,请提供具体的错误信息,我可以提供更针对性的解决方案。