How to Securely Copy Files from Remote Server Using scp with an SSH Key

How to Securely Copy Files from Remote Server Using scp with an SSH Key
Photo by Yash Raut / Unsplash

When working with remote servers, transferring files back and forth is a common task. One of the simplest ways to securely download files from a remote machine is using the scp command — a tool that comes with the OpenSSH suite and works on Windows, Linux, and macOS.


What is scp?

scp stands for secure copy. It’s a command-line tool that lets you transfer files over SSH, meaning your data stays encrypted and secure during transmission.


🔑 Using scp with an Identity File

If your server uses key-based authentication (instead of a password), you’ll need to specify the path to your private key with the -i option.

Basic Syntax:

scp -i /path/to/private_key username@remote_host:/path/to/remote/file /path/to/local/destination

Example:

scp -i ~/.ssh/id_rsa [email protected]:/home/user/file.txt .
This command will download file.txt to your current directory (.).

📁 Copying a Whole Directory

To copy an entire folder (including its contents), add the -r flag:

scp -i ~/.ssh/id_rsa -r [email protected]:/home/user/myfolder .

This recursively downloads everything inside myfolder.


🧭 Save to a Specific Path or Rename File

You’re not limited to saving in the current folder. For example:

scp -i ~/.ssh/id_rsa [email protected]:/home/user/file.txt C:/Users/YourName/Documents/newname.txt
This renames file.txt to newname.txt and saves it into your Documents folder on Windows.

🔁 Custom SSH Port

If the server uses a non-standard SSH port (not 22), add -P (uppercase):

scp -i ~/.ssh/id_rsa -P 2222 [email protected]:/home/user/file.txt .

💡 Helpful Tips and Considerations

  • Use absolute paths for remote files to avoid confusion.
  • Make sure the private key file is not publicly readable (on Linux/macOS: chmod 600).
  • On Windows, use scp either via:
    • Command Prompt or PowerShell (after installing the OpenSSH Client).
    • Or via WSL (Windows Subsystem for Linux) for a more UNIX-like experience.
  • If you're using WSL, remember that file paths follow Linux format (e.g., /mnt/c/Users/...).

🚀 Speeding Things Up with SSH Config (Optional)

If you connect to the same server frequently, you can create a config entry in ~/.ssh/config (Linux/macOS/WSL):

Host myserver
    HostName 192.168.1.10
    User user
    IdentityFile ~/.ssh/id_rsa
    Port 2222

Then, instead of the full command, just use:

scp myserver:/home/user/file.txt .

Much faster.


🧱 Why scp Over Other Tools?

  • No need for GUI.
  • Works from any shell.
  • Easier to script or automate.
  • Uses standard SSH ports and credentials.

If you prefer a GUI tool, you might also look at WinSCP (Windows) or Cyberduck (macOS), which offer similar functionality but with drag-and-drop convenience.


📌 Final Thoughts

The scp command is a fast, secure, and reliable way to copy files over SSH. Whether you’re on Windows, Linux, or macOS, the command works the same — especially powerful when using SSH key authentication.

Next time you need to pull logs, backups, or deployment assets, remember this simple one-liner:

scp -i /path/to/key user@host:/remote/file .

Fast, clean, secure.

Support Us