Secure Copy (scp) is a command-line tool in Linux that lets you transfer files and directories between computers over an SSH connection. It uses the same authentication and encryption as SSH, so your data stays secure while moving between your local machine and a remote server, or between two remote servers. This guide walks you through the most common scp usage patterns, from basic file copies to recursive directory transfers, with real examples you can adapt.
What You’ll Need
- A Linux or macOS machine with OpenSSH client installed (most come with it pre-installed; check with
scp -V). - The IP address or hostname of the remote server.
- A valid username on that server, or the ability to use root (if allowed).
- SSH access to the remote server (port 22 by default; your server may use a different port).
- If you’re transferring between two remote servers, you’ll need SSH access to both.
Step-by-Step
- Open a terminal on your local machine. On most Linux distributions, you can press
Ctrl+Alt+Tor search for “Terminal” in your applications menu. - Understand the basic syntax:
scp [options] source destination. The source and destination can be local paths or remote paths in the formuser@host:path. - Copy a file from local to remote:
scp /home/user/file.txt user@192.168.1.100:/home/user/. This copiesfile.txtto the remote user’s home directory. You’ll be prompted for the remote user’s password (or use SSH keys if set up). - Copy a file from remote to local: Reverse the order—
scp user@192.168.1.100:/home/user/file.txt /home/user/. The remote file is downloaded to your local directory. - Copy a directory recursively: Use the
-rflag to copy an entire directory and its contents. Example:scp -r /home/user/project user@192.168.1.100:/home/user/. Without-r, scp will error out on directories. - Specify a different SSH port: If the remote server uses a non-default port (e.g., 2222), use
-P(capital P) followed by the port number:scp -P 2222 file.txt user@192.168.1.100:/home/user/. Note: lowercase-pis for preserving file timestamps, not port. - Preserve file attributes: Use
-p(lowercase) to preserve modification times, access times, and modes from the original file. - Use SSH keys for passwordless transfer: If you have SSH keys set up (via
ssh-keygenandssh-copy-id), scp will automatically use them, so you won’t be prompted for a password. This is handy for scripting. - Copy between two remote servers:
scp user1@server1:/path/file user2@server2:/path/. This will connect to server1, then from there to server2. You’ll need to authenticate for both servers. - Use wildcards: You can use wildcards like
*or?to copy multiple files, but be careful with shell expansion. Example:scp /home/user/*.txt user@192.168.1.100:/home/user/copies all .txt files. - Verbose output for debugging: Add
-vto see detailed progress and connection information. This helps when things aren’t working. - Compress data during transfer: Use
-Cto enable compression, which can speed up transfers of large text files or logs over slow networks.
Common Mistakes
- Forgetting the colon: The remote path must include a colon (
:) after the host. Without it, scp treats the whole thing as a local path. - Using lowercase
-pfor port: Port is-P(uppercase). Lowercase-ppreserves file attributes, which is a different feature. - Missing the
-rflag for directories: Attempting to copy a directory without-rresults in an error like “Not a regular file”. - Wrong path syntax: On remote paths, use forward slashes (
/) even if the remote is Windows. Also, relative paths are relative to the remote user’s home directory, not your current local directory. - Permission issues: If you get “Permission denied”, ensure the remote user has write permission to the destination directory. You may need to use
sudoon the remote side (but scp doesn’t support sudo directly; you’d need to copy to a writable location first). - Overwriting files unintentionally: scp overwrites existing files without warning. Double-check the destination path to avoid losing data.
Tips
- Use SSH keys: Setting up key-based authentication not only avoids password prompts but also makes scp more secure and scriptable. Generate a key with
ssh-keygen -t ed25519and copy it withssh-copy-id user@host. - Limit bandwidth: Use
-l(lowercase L) to limit bandwidth in Kbit/s. Example:scp -l 800 file.txt user@host:/path/limits to 100 KB/s. Useful for not saturating your network. - Resume interrupted transfers: scp doesn’t support resume natively. For large transfers, consider using
rsyncinstead, which can resume and also compress and sync efficiently. - Test with
-n(dry run): scp doesn’t have a dry-run option, but you can usersync -nto preview what would be transferred, then run scp if you prefer it. - Use aliases: If you frequently scp to the same server, add an SSH config entry (
~/.ssh/config) with a short alias likeHost myserver, then usescp file.txt myserver:/path/. - Check file integrity: After transfer, compare checksums (e.g.,
md5sumon both sides) to ensure the file arrived intact, especially for critical data.
Troubleshooting
- “Connection refused”: The remote SSH service isn’t running or is on a different port. Check with
ssh user@hostto see if SSH works. If it does, ensure you’re using the correct-Pport. - “Host key verification failed”: The remote host’s key has changed (e.g., reinstall). Remove the old key from
~/.ssh/known_hostswithssh-keygen -R hostname, then try again. - “Permission denied (publickey,password)”: Authentication failed. Check your username, password, or key setup. If using keys, ensure the public key is in the remote user’s
~/.ssh/authorized_keyswith correct permissions (600 for the file, 700 for the .ssh directory). - “No such file or directory”: The source path doesn’t exist locally, or the remote path’s parent directory doesn’t exist. scp won’t create directories; you need to create them first with
ssh user@host 'mkdir -p /path/to/dir'. - Transfer is very slow: Try adding
-Cfor compression, or use-lto set a reasonable bandwidth limit. If the network is unstable, considerrsyncwith--partialto resume. - scp hangs or times out: Check firewalls and network connectivity. Try
ssh -v user@hostto see where it’s stuck. Also, ensure the remote server’s SSH is configured to allow SCP (some restrict SFTP but not SCP).
Frequently Asked Questions
Can I use scp to copy a file from a Windows machine to Linux?
Yes, but you need an scp client on Windows, such as the one included in Windows 10’s OpenSSH, or use tools like PuTTY’s pscp. The command syntax is similar, but paths use backslashes or forward slashes depending on the client.
What’s the difference between scp and sftp?
scp is a simple command for copying files, while sftp is an interactive file transfer protocol that supports browsing, resuming, and remote file management. scp is faster for one-off transfers, but sftp is more flexible for interactive sessions.
Is scp secure enough for sensitive data?
Yes, scp encrypts the data in transit using SSH’s encryption. However, it doesn’t provide end-to-end encryption if you copy between two remote servers via your local machine (the data is decrypted on your machine in between). For maximum security, consider using scp -3 to transfer directly between servers without routing through your local machine.
How do I copy a file to a remote server as root?
You can specify the root user: scp file.txt root@host:/root/. But direct root SSH is often disabled for security. A better practice is to copy to your user’s home directory, then use ssh user@host 'sudo mv file.txt /target/'.
Can I use scp to copy files between two remote servers without downloading to my local machine?
Yes, use the syntax scp user1@server1:/path/file user2@server2:/path/. This will connect from your local machine to server1, then from server1 to server2, so the data doesn’t pass through your local machine. However, you’ll need to authenticate to both servers.
What does “scp: /path: Not a directory” mean?
This usually means you forgot the colon after the host, so scp interpreted the entire argument as a local path. Double-check your syntax and ensure the colon is present after the hostname or IP.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Remove a Remote Access Trojan from Windows PC
- Fix Payday 2 Crash Issues on PC: A Step-by-Step Guide
- Google Pixel & Pixel XL: How to Turn Off Snooze
- Facebook as AIM Messenger: Step-by-Step Guide
- iPhone 17 SOS Service Issue: How to Fix It
- Install VLC Extensions on Linux