How Can We Help?
Secure File Transfer Between Your Local Machine and VPS
Secure File Transfer Between Local Machine and VPS: A Comprehensive Guide
Transferring files between your local machine and your VPS is a frequent necessity in server management. This guide will walk you through the process using the secure copy (SCP) command with SSH keys on various operating systems, including Ubuntu, CentOS, Debian, FreeBSD, and OpenSUSE.
Prerequisites
Ensure you have SSH access to your VPS and know your VPS IP address. You’ll also need to have the SCP command and your SSH private key available on your local machine. Most Unix-based systems, including Linux and MacOS, have this command pre-installed.
Using SCP with SSH Key to Transfer Files
General Syntax
The general syntax for the SCP command with an SSH key is as follows:
scp -i /path/to/your/private/key source destination
“Source” and “destination” can be either a local file path or a remote file path. Remote file paths are written in the following format: user@ip:/path
.
Copying Files from Local Machine to VPS
To copy a file from your local machine to your VPS, use the following command:
scp -i /path/to/your/private/key /path/to/local/file username@your_VPS_ip:/path/to/remote/directory
Replace /path/to/your/private/key
with the local file path of your SSH private key, /path/to/local/file
with the local file path, username@your_VPS_ip
with your VPS username and IP, and /path/to/remote/directory
with the directory where you want to place the file on your VPS.
Copying Files from VPS to Local Machine
To copy a file from your VPS to your local machine, use the following command:
scp -i /path/to/your/private/key username@your_VPS_ip:/path/to/remote/file /path/to/local/directory
Replace /path/to/your/private/key
with the local file path of your SSH private key, username@your_VPS_ip:/path/to/remote/file
with your VPS username, IP, and the file you want to copy, and /path/to/local/directory
with the directory where you want to place the file on your local machine.
Examples of SCP Command with SSH Key on Different Operating Systems
While the SCP command itself does not change much between operating systems, the file paths and the way you access the terminal may vary. Here are a few examples:
Ubuntu, Debian, CentOS, and OpenSUSE
Suppose you want to copy a file named example.txt
from your home directory to the /var/www/
directory on your VPS. Open the terminal and type:
scp -i ~/.ssh/myvps.pem ~/example.txt username@your_VPS_ip:/var/www/
To copy the same file back to your local machine:
scp -i ~/.ssh/myvps.pem username@your_VPS_ip:/var/www/example.txt ~/
FreeBSD
In FreeBSD, you would use the same SCP commands, but file paths may differ depending on where your files are stored.
Protecting Your Private Key
Remember, keep your private key secure. Anyone who has access to it can connect to your VPS. Store it in a secure place and limit its permissions using chmod 400 /path/to/your/private/key
to ensure it is not publicly viewable. This is an essential step for maintaining the security of your VPS.
Conclusion
This guide should help you securely transfer files between your local machine and your VPS across different operating systems. If you encounter any issues or need further assistance, please contact our support team.