Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Use the scp Command in Linux (Easy Guide)

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The scp (secure copy) command in Linux lets you copy files and directories between your local machine and a remote server, or between two remote servers, over an SSH connection. It’s essentially cp but across a network, and it encrypts everything in transit. You don’t need any special software on the remote machine—just SSH access. This guide walks you through the most common uses, with clear examples, common pitfalls, and fixes.

What You'll Need

  • A Linux (or macOS) local machine with scp installed (it comes with OpenSSH, usually preinstalled).
  • The remote server’s hostname or IP address.
  • A valid username and password or SSH key for the remote server.
  • SSH service running on the remote server (port 22 by default).

Step-by-Step

  1. Open a terminal. On most Linux distributions, press Ctrl+Alt+T or search for “Terminal” in your apps menu.
  2. Understand the basic syntax:
    scp [options] source destination
    The source and destination can be local paths (like /home/user/file.txt) or remote paths in the form username@host:/path/to/file.
  3. Copy a file from local to remote:
    scp /home/user/file.txt user@server.example.com:/home/user/
    This copies file.txt to the remote user’s home directory. You’ll be prompted for the remote password unless you have SSH keys set up.
  4. Copy a file from remote to local:
    scp user@server.example.com:/home/user/file.txt /home/user/
    This downloads the file to your current local directory.
  5. Copy an entire directory: Use the -r (recursive) option:
    scp -r /home/user/mydir user@server.example.com:/home/user/
    This copies the whole mydir folder and its contents.
  6. Use a specific SSH port: If the remote server uses a non-default port (e.g., 2222), add -P (capital P):
    scp -P 2222 file.txt user@server.example.com:/home/user/
    Note: For ssh it’s lowercase -p, but for scp it’s uppercase -P.
  7. Preserve file timestamps and permissions: Use -p (lowercase):
    scp -p file.txt user@server.example.com:/home/user/
  8. Copy between two remote servers: You can specify both source and destination as remote paths:
    scp user1@server1.com:/path/file.txt user2@server2.com:/path/
    This will transfer the file from server1 to server2 through your local machine (the data passes through your computer, so it’s not a direct server-to-server transfer).
  9. Use SSH keys for passwordless transfers: If you have an SSH key set up, scp will use it automatically. To generate a key, run ssh-keygen -t rsa and then copy the public key to the remote server with ssh-copy-id user@server.example.com. After that, scp won’t ask for a password.

Common Mistakes

  • Forgetting the colon in remote paths: scp file.txt user@server.com/home/user/ is wrong—you need a colon after the host: user@server.com:/home/user/.
  • Using lowercase -p for port: For port, it’s -P. Lowercase -p preserves file attributes.
  • Not using -r for directories: Trying to copy a folder without -r will give an error like “omitting directory”.
  • Permission issues on destination: If you don’t have write permission in the remote directory, you’ll get a “Permission denied” error. Use a directory you own, or use sudo on the remote side (which scp cannot do directly—you’d need to copy to a writable location and then move it).
  • Mixing up source and destination order: The first path is the source, the second is the destination. Swapping them can overwrite files unintentionally.

Tips

  • Use -v (verbose) to see detailed debugging info if something goes wrong.
  • Use -C to enable compression, which speeds up transfers of text files over slow connections.
  • For large transfers, consider using rsync instead—it resumes interrupted transfers and can be more efficient.
  • Always double-check the destination path—scp will happily overwrite an existing file without warning.
  • If you’re copying many files, consider tarring them first: tar czf - folder | ssh user@host 'tar xzf - -C /dest' can be faster than scp -r for many small files.

Troubleshooting

If scp doesn’t work as expected, try these in order:

  1. Check SSH connectivity: Run ssh user@server.example.com to see if you can log in. If SSH fails, scp will too. Fix SSH first.
  2. Check hostname and username: Typos are common. Verify the hostname resolves: ping server.example.com or nslookup server.example.com.
  3. Check port and firewall: If you’re using a non-standard port, make sure it’s open on the remote server’s firewall. Try telnet server 2222 to test.
  4. Check permissions on the source file: You need read permission on the local file and write permission on the remote directory. Use ls -l to check.
  5. Try verbose mode: Run scp -v file user@server:/dest and look for error messages like “Connection refused” or “Permission denied”.
  6. If you get “Host key verification failed”: The remote host’s key has changed. Remove the old key from ~/.ssh/known_hosts (find the line with the hostname) and try again.
  7. If the transfer stalls: Check your network connection, and try with -C for compression or reduce the window size with -l limit (in kbit/s).

Frequently Asked Questions

Can I use scp with a Windows machine?

Yes, Windows 10 and later include OpenSSH, so scp works in PowerShell or Command Prompt. You can also use tools like WinSCP or PuTTY’s pscp for a graphical or command-line alternative.

How do I copy a file to a remote server without typing a password?

Set up SSH key authentication: generate a key pair with ssh-keygen, then copy the public key to the server using ssh-copy-id user@server. After that, scp will use the key automatically.

What’s the difference between scp and rsync?

scp is simpler and copies everything every time. rsync can check for differences and only transfer changed parts, which is faster for large or repeated transfers. rsync also supports resuming interrupted transfers.

Can I copy a file between two remote servers directly?

Yes, but the data passes through your local machine, so it’s not a direct server-to-server transfer. For a true direct transfer, you’d need to log into one server and use scp from there.

Why do I get “Permission denied (publickey)”?

This usually means the server doesn’t accept your password or key. Check that you’re using the correct username, that the server allows password authentication, or that your public key is in the server’s ~/.ssh/authorized_keys file.

Is scp secure?

Yes, scp encrypts all data during transfer using SSH encryption. It’s as secure as SSH itself. However, note that scp is being deprecated in some environments in favor of rsync or sftp, but it still works fine on most systems.