This guide shows you how to use the scp command to securely copy files from your local machine to a Virtual Private Server (VPS). You’ll learn the exact syntax, common options, and how to avoid typical pitfalls. By the end, you’ll be able to transfer files reliably over SSH.
What You’ll Need
- A VPS with SSH enabled (usually port 22).
- The VPS’s IP address or hostname.
- A username on the VPS (e.g.,
rootor a sudo user). - The password or SSH key for that user.
- OpenSSH client installed on your local machine (pre-installed on macOS and Linux; on Windows 10/11, use the built-in OpenSSH client or install it via “Optional Features”).
Step-by-Step
- Open a terminal on your local computer. On Windows, use PowerShell or Command Prompt; on macOS/Linux, use the built-in Terminal.
- Navigate to the directory containing the file you want to copy (optional, but makes the command shorter):
cd /path/to/local/folder - Run the basic
scpcommand with this syntax:scp filename username@vps_ip:/remote/destination/path
For example:scp report.pdf root@192.168.1.100:/var/www/
This copiesreport.pdfto the/var/www/directory on the VPS. - Enter your password when prompted. If you’re using an SSH key, it will use that automatically (no password prompt).
- Verify the transfer by checking the output. A successful transfer shows a progress bar and a confirmation like
report.pdf 100% 123KB 123.4KB/s 00:01. - To copy a directory (with all its contents), add the
-r(recursive) flag:scp -r /local/folder username@vps_ip:/remote/path - To use a custom SSH port (if your VPS uses a non-default port), add the
-Pflag (capital P):scp -P 2222 file.txt username@vps_ip:/remote/path - To preserve file timestamps and permissions, use the
-pflag (lowercase):scp -p file.txt username@vps_ip:/remote/path - To copy multiple files at once, list them all before the destination:
scp file1.txt file2.txt username@vps_ip:/remote/path - To copy from the VPS to your local machine (reverse direction), swap the order:
scp username@vps_ip:/remote/file.txt /local/destination/
Common Mistakes
- Using
-pfor port –scpuses-P(capital) for port, unlikesshwhich uses-p(lowercase). - Forgetting the colon – The colon after the remote host is essential.
scp file user@host/pathwill fail; it must beuser@host:/path. - Missing the remote path – If you omit the destination path,
scpwill copy to the user’s home directory, which may not be what you want. - Using relative paths on the remote – Remote paths are relative to the user’s home directory unless you use an absolute path (starting with
/). - Not escaping special characters – If your filename has spaces or special characters, wrap it in quotes:
scp "my file.txt" user@host:/path
Tips
- Use SSH keys for passwordless and more secure transfers. Generate a key pair with
ssh-keygenand copy the public key to the VPS withssh-copy-id. - Use
-Cfor compression – This can speed up transfers of large text files over slow connections:scp -C file.txt user@host:/path - Limit bandwidth with
-l(lowercase L) to avoid saturating your connection:scp -l 1000 file.txt user@host:/path(limits to 1000 Kbit/s). - For large transfers, consider
rsync– It’s more efficient for incremental transfers and can resume interrupted ones. - Test with a small file first to ensure your syntax and permissions are correct before transferring a large dataset.
Troubleshooting
- “Permission denied” error – Check that you’re using the correct username and password. If using an SSH key, ensure the key is added to the VPS’s
~/.ssh/authorized_keyswith proper permissions (chmod 600). - “Connection refused” – The SSH service may not be running on the VPS, or a firewall is blocking port 22. Check with your VPS provider’s console or try connecting via
sshfirst. - “Host key verification failed” – This happens when the VPS’s host key has changed. Remove the old key from your local
~/.ssh/known_hostsfile (the line with the VPS IP) and try again. - “No such file or directory” – The local file path is wrong, or the remote directory doesn’t exist. Double-check both paths. On the remote, you may need to create the directory first with
mkdir -p /remote/path. - Transfer is slow – Try adding
-Cfor compression or-c aes128-ctrto use a faster cipher. Also check your network connection. - Transfer stops mid-way – Use
rsyncwith--partialto resume, or re-runscp– it will overwrite the partial file.
Frequently Asked Questions
Can I use scp to copy a file from the VPS to my local machine?
Yes, just reverse the order: scp username@vps_ip:/remote/path/file.txt /local/destination/
How do I copy a file to a VPS using a different SSH port?
Use the -P flag with the port number, e.g., scp -P 2222 file.txt user@host:/path
Does scp work on Windows?
Yes, Windows 10 and 11 include OpenSSH client by default. You can use it in PowerShell or Command Prompt. If not, install it via Settings > Apps > Optional Features.
What’s the difference between scp and sftp?
Both use SSH for secure transfer. scp is a simple one-shot command for copying files, while sftp is an interactive session that allows browsing, uploading, downloading, and managing files on the remote server.
How do I copy a file with spaces in its name?
Wrap the filename in quotes, e.g., scp "my report.pdf" user@host:/path
Is scp encrypted?
Yes, scp uses SSH encryption, so your data and credentials are protected during transfer.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t