macOS终端通过SSH上传文件至Apache服务器遇文件不存在错误求助
Hey there! Let's break this down step by step—since you're new to Unix shells, it's totally normal to hit this kind of roadblock. Let's start with the most common mistake new users make, then work through other possible fixes.
The #1 Mistake: Trying to Access Local Files From the Remote Server Shell
If you first ran ssh your-server-username@server-address to log into the Apache server, your terminal is now operating on the remote server's file system, not your local macOS machine. That means when you try to reference /Users/your-mac-username/Desktop/test.html from the remote shell, it has no idea what that path is—it only sees the server's files.
Correct Approach 1: Use scp Directly From Your Local Terminal
You don't need to log into the server first. Open a fresh macOS terminal window and run this command (replace the placeholders with your actual info):
scp /Users/your-mac-username/Desktop/test.html your-server-username@server-ip-or-domain:/path/to/server/directory
For example, if your macOS username is carlos, server username is webadmin, server address is 192.168.0.20, and the Apache target directory is /var/www/html, the command becomes:
scp /Users/carlos/Desktop/test.html webadmin@192.168.0.20:/var/www/html/
Enter your server password when prompted, and the file should upload successfully.
Correct Approach 2: Use sftp (Interactive, Great for New Users)
If you prefer a more guided experience, use SFTP:
- Open your local terminal and connect to the server:
sftp your-server-username@server-ip-or-domain - Switch to your local desktop directory with
lcd(local cd):lcd /Users/your-mac-username/Desktop - Switch to the server's target directory with
cd:cd /path/to/server/directory - Upload the file:
put test.html - Type
exitwhen done to close the SFTP session.
Other Possible Fixes If the Above Doesn't Work
- Double-check your local file path: Run this in your local terminal to confirm the file exists:
If this throws a "file not found" error, make sure your macOS username is spelled correctly (case matters here—e.g.,ls /Users/your-mac-username/Desktop/test.htmlCarlosvscarlos). - Watch for filename case sensitivity: Unix/Linux servers are case-sensitive. If your local file is named
Test.htmlbut you typedtest.htmlin the command, the server won't recognize it. - Check server directory permissions: If you get a "permission denied" error, you might not have write access to the target directory. Try uploading to the server's temporary directory first:
Then log into the server and move the file withscp /Users/carlos/Desktop/test.html webadmin@192.168.0.20:/tmp/sudo(if you have admin rights):sudo mv /tmp/test.html /var/www/html/
内容的提问来源于stack exchange,提问作者Carlos Rodríguez

