如何通过终端将文件从Ubuntu复制到Windows Server?SSH传输失败求助
Hey there! Sorry to hear your SSH transfer attempt didn't go smoothly. Let's cover a few solid, built-in methods to get your files over without needing any extra software.
方法1:修复SCP/SSH传输(大概率是配置问题!)
First, let's rule out why your SSH transfer failed—Windows Server doesn't have OpenSSH Server enabled by default, which is a super common gotcha. Here's how to fix that from the Windows side first:
- On Windows Server, open PowerShell as admin and run this to check if OpenSSH Server is installed:
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*' - If it shows "NotPresent", install it with:
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 - Start the service and set it to auto-start so it works after reboots:
Start-Service sshd Set-Service -Name sshd -StartupType 'Automatic'
Once that's sorted, head back to your Ubuntu terminal and use scp like normal:
# 复制单个文件 scp /path/to/your/local/file ubuntu-user@windows-server-ip:C:/path/to/destination/ # 复制整个目录(记得加-r参数) scp -r /path/to/your/local/directory ubuntu-user@windows-server-ip:C:/path/to/destination/
Note: Make sure your Windows firewall allows incoming connections on port 22 (SSH's default port) if you're on a restricted network.
方法2:使用SMB共享(Windows原生支持,Ubuntu自带客户端)
Windows Server has SMB (Server Message Block) built-in, and Ubuntu comes with smbclient pre-installed (if not, you can grab it via sudo apt install smbclient—this is part of Ubuntu's official repos, not third-party software).
步骤1:在Windows Server上创建SMB共享
- Right-click the folder you want to send files to → Properties → Sharing tab → Advanced Sharing.
- Check "Share this folder", set a simple share name (e.g.,
UbuntuTransfer), then click Permissions to grant your Windows user read/write access to the share. - Note down the share path—it'll look like
\\WINDOWS-SERVER-IP\UbuntuTransfer.
步骤2:从Ubuntu终端上传文件
Use smbclient to connect and upload:
# 连接到SMB共享(替换成你的Windows用户名和服务器IP) smbclient //windows-server-ip/UbuntuTransfer -U windows-username # 连接成功后,用put命令上传单个文件 put /path/to/your/local/file # 上传整个目录用mput(记得加*匹配目录下所有内容) mput /path/to/your/local/directory/*
Tip: If you don't want to enter your password every time, you can add it to the command with -U windows-username%password, but be cautious—plaintext passwords show up in terminal history.
Alternatively, you can mount the SMB share directly on Ubuntu for drag-and-drop-like file management:
# 创建挂载点 sudo mkdir /mnt/windows-share # 挂载SMB共享 sudo mount -t cifs //windows-server-ip/UbuntuTransfer /mnt/windows-share -o username=windows-username,password=your-password # 现在复制文件就像操作本地磁盘一样 sudo cp /path/to/your/local/file /mnt/windows-share/ # 用完后卸载共享 sudo umount /mnt/windows-share
方法3:使用WinRM(Windows远程管理)
Windows Server has WinRM enabled by default (or you can turn it on easily), and Ubuntu's built-in curl can interact with it to upload files. This is a bit more technical, but it works if other methods aren't an option.
步骤1:配置Windows Server的WinRM(如果未启用)
Open PowerShell as admin:
# 启用WinRM Enable-PSRemoting -Force # 允许未加密流量(如果需要,加密模式更安全,但未加密更易配置) Set-Item WSMan:\localhost\Client\AllowUnencrypted -Value $true Set-Item WSMan:\localhost\Service\AllowUnencrypted -Value $true
步骤2:从Ubuntu用curl上传文件
# 上传单个文件到Windows的临时目录,再用PowerShell移动到目标路径 curl -u windows-username:your-password -T /path/to/your/local/file http://windows-server-ip:5985/wsman # 或者用curl发送PowerShell命令来完成文件移动(更可靠) curl -u windows-username:your-password -H "Content-Type: application/json" -X POST -d '{ "script": "Move-Item -Path \"C:\\Windows\\Temp\\your-file-name\" -Destination \"C:\\your\\final\\path\"" }' http://windows-server-ip:5985/wsman
Note: This method is trickier to get right, so stick with SCP or SMB if you can.
If none of these work, feel free to share the exact error messages you got from your failed SSH attempt—I can help troubleshoot that further!
内容的提问来源于stack exchange,提问作者arezoo




