keepcalculator

06 Numbers and Powers

SCP Command: Copy Files to a VPS

Card No. 06·16947 · Updated · Standard formula, shown below

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., root or 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

  1. Open a terminal on your local computer. On Windows, use PowerShell or Command Prompt; on macOS/Linux, use the built-in Terminal.
  2. Navigate to the directory containing the file you want to copy (optional, but makes the command shorter): cd /path/to/local/folder
  3. Run the basic scp command with this syntax:
    scp filename username@vps_ip:/remote/destination/path
    For example: scp report.pdf root@192.168.1.100:/var/www/
    This copies report.pdf to the /var/www/ directory on the VPS.
  4. Enter your password when prompted. If you’re using an SSH key, it will use that automatically (no password prompt).
  5. 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.
  6. To copy a directory (with all its contents), add the -r (recursive) flag:
    scp -r /local/folder username@vps_ip:/remote/path
  7. To use a custom SSH port (if your VPS uses a non-default port), add the -P flag (capital P):
    scp -P 2222 file.txt username@vps_ip:/remote/path
  8. To preserve file timestamps and permissions, use the -p flag (lowercase):
    scp -p file.txt username@vps_ip:/remote/path
  9. To copy multiple files at once, list them all before the destination:
    scp file1.txt file2.txt username@vps_ip:/remote/path
  10. 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 -p for port – scp uses -P (capital) for port, unlike ssh which uses -p (lowercase).
  • Forgetting the colon – The colon after the remote host is essential. scp file user@host/path will fail; it must be user@host:/path.
  • Missing the remote path – If you omit the destination path, scp will 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-keygen and copy the public key to the VPS with ssh-copy-id.
  • Use -C for 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_keys with 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 ssh first.
  • “Host key verification failed” – This happens when the VPS’s host key has changed. Remove the old key from your local ~/.ssh/known_hosts file (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 -C for compression or -c aes128-ctr to use a faster cipher. Also check your network connection.
  • Transfer stops mid-way – Use rsync with --partial to resume, or re-run scp – 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

The whole Numbers and Powers drawer →