Managing files between your local machine and a VPS is a common task, and SCP (Secure Copy Protocol) provides a simple and secure way to transfer files over SSH. This guide explains how to download files from a VPS to your local machine and upload files to a VPS using SCP.
Prerequisites
- A local machine with SCP installed (available by default on Linux and macOS; for Windows, use tools like PuTTY or WinSCP).
- SSH access to the VPS (username, password, or private key).
- The IP address of the VPS.
Download Files from a VPS to Local Machine
To download files or folders from a VPS, use the following SCP syntax:
scp [options] user@vps-ip:/path/to/remote/file /path/to/local/destination
Example 1: Download a Single File
If the file example.txt is located at /root/files/ on the VPS with IP 192.168.1.100, you can download it to the Downloads folder on your local machine with:
scp root@192.168.1.100:/root/files/example.txt ~/Downloads/
Example 2: Download an Entire Folder
To download a folder, add the -r flag for recursive transfer. For example, to download the folder /root/documents/:
scp -r root@192.168.1.100:/root/documents ~/Downloads/
After the transfer, the documents folder will be located in your Downloads directory.
Upload Files from Local Machine to VPS
To upload files or folders from your local machine to a VPS, use the following SCP syntax:
scp [options] /path/to/local/file user@vps-ip:/path/to/remote/destination
Example 1: Upload a Single File
If you want to upload the file myfile.txt from your Documents folder to the /home/user/ directory on the VPS:
scp ~/Documents/myfile.txt root@192.168.1.100:/home/user/
Example 2: Upload an Entire Folder
To upload a folder (e.g., project_files) from your local machine to the /var/www/ directory on the VPS:
scp -r ~/Documents/project_files root@192.168.1.100:/var/www/
Using SCP with SSH Keys
If your VPS uses SSH keys for authentication, include the -i option followed by the path to your private key:
scp -i /path/to/private-key [source] [destination]
For example:
scp -i ~/.ssh/id_rsa ~/Documents/myfile.txt root@192.168.1.100:/home/user/
Verify File Transfer
Once the transfer is complete, log in to your VPS via SSH and verify the files:
ssh root@192.168.1.100
ls /path/to/destination
This will list the files in the destination folder, ensuring the transfer was successful.
Troubleshooting
- Permission Denied Error: Ensure your user has the appropriate permissions on both local and remote paths.
- SSH Key Issues: Check the file permissions of your SSH key (use
chmod 600). - Connectivity Problems: Verify the VPS IP address, firewall rules, and SSH configuration.
By mastering SCP, you can easily manage files between your local machine and VPS without needing additional tools or software.