scp Command in Linux: Secure File Transfer Examples

When you need to copy files to or from a remote server over SSH, scp does the job with a single command. It encrypts both the transferred data and the authentication credentials, so nothing extra is needed if SSH access is already in place.
This guide explains how to use the scp command with practical examples and detailed explanations of the most common options.
Before You Begin
Before using scp, keep the following in mind:
- SCP relies on SSH for data transfer. You need either an SSH key or a password to authenticate on the remote system.
- The colon (
:) is howscpdistinguishes between local and remote paths. A path without a colon is treated as local. - You must have read permission on the source and write permission on the destination.
- SCP overwrites files without warning when the source and destination share the same name.
- When transferring large files, run the
scpcommand inside ascreenortmuxsession to keep the transfer running if your terminal disconnects.
SCP Command Syntax
The general syntax of the scp command is:
scp [OPTIONS] [[user@]host:]source [[user@]host:]destination[[user@]host:]source— Source path. Include the username and hostname (or IP address) when the file is on a remote machine.[[user@]host:]destination— Destination path. Same format as the source.
Local paths can be absolute or relative. Remote paths must include the host and colon.
The most commonly used scp options are:
-P— Remote host SSH port (uppercase P)-p— Preserve modification time, access time, and mode-r— Copy directories recursively-C— Compress data during transfer-q— Suppress the progress meter and non-error messages-i— Path to the SSH private key (identity file)-l— Limit bandwidth in Kbit/s-o— Pass an SSH option (e.g.,-o StrictHostKeyChecking=no)-3— Route traffic between two remote hosts through the local machine-O— Force the legacy SCP protocol instead of SFTP
Copy a Local File to a Remote System
To copy a file from the local machine to a remote server, run:
scp file.txt remote_username@10.10.0.2:/remote/directoryIn this example, file.txt is the local file, remote_username is the user on the remote server, and 10.10.0.2 is the server IP address. The file is copied to /remote/directory on the remote host. If you omit the remote directory, the file is copied to the remote user’s home directory.
You will be prompted to enter the user password, and the transfer process will start:
remote_username@10.10.0.2's password:
file.txt 100% 14KB 82.1KB/s 00:00To save the file under a different name on the remote host, specify the new filename in the destination path:
scp file.txt remote_username@10.10.0.2:/remote/directory/newfilename.txtIf SSH on the remote host is listening on a port other than the default 22, use the -P option:
scp -P 2322 file.txt remote_username@10.10.0.2:/remote/directoryTo copy a directory and all its contents, use the -r flag for recursive copy:
scp -r /local/directory remote_username@10.10.0.2:/remote/directoryWhen you want to copy multiple local files that match a pattern, let the local shell expand the wildcard before scp runs. In the following example, all .txt files from the local Projects directory are copied to the remote Projects directory:
scp "$HOME"/Projects/*.txt remote_username@10.10.0.2:/home/user/Projects/To preserve file metadata (modification time, access time, and mode), use the -p option:
scp -p file.txt remote_username@10.10.0.2:/remote/directory/To use a specific SSH key for authentication, pass it with the -i option:
scp -i ~/.ssh/id_ed25519 file.txt remote_username@10.10.0.2:/remote/directory/Copy a Remote File to the Local System
To copy a file from a remote server to the local machine, use the remote location as the source and the local path as the destination:
scp remote_username@10.10.0.2:/remote/file.txt /local/directoryIf you have not set up passwordless SSH login , you will be prompted to enter the user password.
To copy an entire remote directory, add the -r flag:
scp -r remote_username@10.10.0.2:/remote/directory /local/directoryCopy Files Between Two Remote Systems
With scp, you do not need to log in to either server to transfer files between two remote machines. The following command copies /files/file.txt from host1.com to the /files directory on host2.com:
scp user1@host1.com:/files/file.txt user2@host2.com:/filesYou will be prompted to enter the passwords for both remote accounts. By default, the data flows directly between the two remote hosts.
To route the traffic through the local machine instead, use the -3 option:
scp -3 user1@host1.com:/files/file.txt user2@host2.com:/filesUsing an SSH Config File
If you regularly connect to the same hosts, defining them in the SSH config file
simplifies your scp commands. Create or edit the file at ~/.ssh/config:
Host myserver
HostName 10.10.0.2
User leah
Port 2222
IdentityFile ~/.ssh/id_ed25519With this configuration, you can use the alias instead of the full connection details:
scp file.txt myserver:/remote/directorySetting up SSH key-based authentication removes the password prompt entirely, making transfers faster and easier to script.
Troubleshooting
Permission denied (publickey)
The remote server does not accept your SSH key. Verify that the correct key is offered with -i, or check that the public key is in the remote user’s ~/.ssh/authorized_keys file.
Connection refused
The SSH daemon is not running or is listening on a different port. Confirm the port with -P and ensure the firewall allows SSH traffic.
Not a regular file
You are trying to copy a directory without the -r flag. Add -r to copy directories recursively.
Host key verification failed
The remote host key does not match the entry in ~/.ssh/known_hosts. If the server was reinstalled, remove the old key with ssh-keygen -R hostname and try again.
Transfer is slow
Enable compression with -C to speed up transfers over slow connections. You can also limit bandwidth with -l to avoid saturating the link on shared networks.
Quick Reference
For a printable quick reference, see the scp cheatsheet .
| Command | Description |
|---|---|
scp file.txt user@host:/path | Copy local file to remote |
scp user@host:/path/file.txt . | Copy remote file to local |
scp -r dir/ user@host:/path | Copy directory recursively |
scp -P 2222 file.txt user@host:/path | Use custom SSH port |
scp -i ~/.ssh/key file.txt user@host:/path | Use specific SSH key |
scp -p file.txt user@host:/path | Preserve timestamps and mode |
scp -C file.txt user@host:/path | Compress during transfer |
scp -l 5000 file.txt user@host:/path | Limit bandwidth to 5000 Kbit/s |
scp -3 user1@host1:/f user2@host2:/f | Copy between two remotes via local |
scp -O file.txt user@host:/path | Force legacy SCP protocol |
FAQ
Does scp overwrite existing files?
Yes. SCP overwrites destination files without prompting. There is no built-in confirmation flag, so verify the destination path before running the command.
What is the difference between scp and sftp?
Both use SSH for encryption. SFTP is an interactive file transfer protocol that supports resuming transfers, directory listings, and file removal. SCP is a simpler one-shot copy command. Modern OpenSSH versions run the SFTP protocol internally when you use scp.
Is scp still recommended?
The scp command is still widely available and works the same way from the user’s perspective. Starting with OpenSSH 9.0, it uses the SFTP protocol internally rather than the legacy SCP/RCP protocol. For new scripts or automation, sftp
or rsync
may be a better long-term choice.
Can I resume an interrupted scp transfer?
No. SCP does not support resuming partial transfers. If the connection drops, you must start the transfer over. For resumable transfers, use rsync
with the --partial flag.
How do I copy files without a password prompt?
Set up SSH key-based authentication
between the local and remote machines. Once the public key is installed on the remote host, SCP authenticates automatically.
What does the -O flag do?
The -O flag forces scp to use the legacy SCP/RCP protocol instead of the SFTP protocol. This is sometimes needed when connecting to very old servers that do not support SFTP.
Conclusion
SCP is a straightforward tool for copying files between local and remote systems over SSH. While modern OpenSSH versions now use the SFTP protocol internally, the scp command remains widely available and works the same way from the user’s perspective.
For advanced workflows such as resumable transfers, incremental backups, or syncing large directory trees, consider using rsync
or sftp
instead.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page