🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 72 (from laksa091)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

đź“„
INDEXABLE
âś…
CRAWLED
8 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.3 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/
Last Crawled2026-04-16 10:04:41 (8 days ago)
First Indexed2018-09-20 10:18:48 (7 years ago)
HTTP Status Code200
Content
Meta TitleSCP Command in Linux: Secure File Transfer Examples | Linuxize
Meta DescriptionSCP copies files securely between local and remote hosts over SSH. This guide covers syntax, common options, and practical examples for everyday file transfers.
Meta Canonicalnull
Boilerpipe Text
SCP (Secure Copy) is a command-line utility that copies files and directories between hosts over an SSH connection. Both the transferred data and the authentication credentials are encrypted, which protects against interception and eavesdropping. OpenSSH 9.0 and newer versions use the SFTP protocol internally when you run scp by default. The legacy SCP protocol is considered outdated. For new workflows, consider using sftp or rsync directly. 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 how scp distinguishes 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 scp command inside a screen or tmux session to keep the transfer running if your terminal disconnects. SCP Command Syntax The general syntax of the scp command is: [[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: In 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: To save the file under a different name on the remote host, specify the new filename in the destination path: If SSH on the remote host is listening on a port other than the default 22, use the -P option: To copy a directory and all its contents, use the -r flag for recursive copy: When using wildcards such as * or ? , enclose the path in quotes to prevent shell expansion on the local machine. In the following example, we are copying all .txt files from the local Projects directory to the remote Projects directory: To preserve file metadata (modification time, access time, and mode), use the -p option: To use a specific SSH key for authentication, pass it with the -i option: 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: If 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: Copy 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 : You 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: Using 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 : With this configuration, you can use the alias instead of the full connection details: Setting 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 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 For a printable quick reference, see the SCP Cheatsheet . 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 . 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. If you have any questions, feel free to leave a comment below. Tags Linuxize Weekly Newsletter A quick weekly roundup of new tutorials, news, and tips. Unsubscribe anytime. We respect your inbox. 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 Part 18 of 26
Markdown
[Skip to main content](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#main-content) [Start Here](https://linuxize.com/start-here/) [Linux Commands](https://linuxize.com/tags/linux-commands/) [Bash](https://linuxize.com/tags/bash/) [Ubuntu](https://linuxize.com/tags/ubuntu/) [Python](https://linuxize.com/tags/python/) [Series](https://linuxize.com/series/) [Cheatsheets](https://linuxize.com/cheatsheet/) [About](https://linuxize.com/about/) Search `/` - [Start Here](https://linuxize.com/start-here/) - [Linux Commands](https://linuxize.com/tags/linux-commands/) - [Bash](https://linuxize.com/tags/bash/) - [Ubuntu](https://linuxize.com/tags/ubuntu/) - [Python](https://linuxize.com/tags/python/) - [Series](https://linuxize.com/series/) - [Cheatsheets](https://linuxize.com/cheatsheet/) - [About](https://linuxize.com/about/) 1. [Home](https://linuxize.com/) 2. / [ssh](https://linuxize.com/tags/ssh/) 3. / SCP Command in Linux: Secure File Transfer Examples # SCP Command in Linux: Secure File Transfer Examples By [Dejan Panovski](https://linuxize.com/authors/dejan-panovski/) • Updated on Feb 13, 2026 • 7 min read Series progress [SSH Essentials](https://linuxize.com/series/ssh-essentials/) Part 18 of 26 On this page - [Before You Begin](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#before-you-begin) - [SCP Command Syntax](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#scp-command-syntax) - [Copy a Local File to a Remote System](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#copy-a-local-file-to-a-remote-system) - [Copy a Remote File to the Local System](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#copy-a-remote-file-to-the-local-system) - [Copy Files Between Two Remote Systems](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#copy-files-between-two-remote-systems) - [Using an SSH Config File](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#using-an-ssh-config-file) - [Troubleshooting](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#troubleshooting) - [Quick Reference](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#quick-reference) - [FAQ](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#faq) - [Conclusion](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#conclusion) Show full outline Share ![Use SCP Command to Securely Transfer Files](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/featured_hu_fb07d8559abc237e.jpg) SCP (Secure Copy) is a command-line utility that copies files and directories between hosts over an SSH connection. Both the transferred data and the authentication credentials are encrypted, which protects against interception and eavesdropping. Tip OpenSSH 9.0 and newer versions use the SFTP protocol internally when you run `scp` by default. The legacy SCP protocol is considered outdated. For new workflows, consider using [`sftp`](https://linuxize.com/post/how-to-use-linux-sftp-command-to-transfer-files/) or [`rsync`](https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/) directly. This guide explains how to use the `scp` command with practical examples and detailed explanations of the most common options. ## Before You Begin [\#](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#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 how `scp` distinguishes 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 `scp` command inside a [`screen`](https://linuxize.com/post/how-to-use-linux-screen/) or [`tmux`](https://linuxize.com/post/getting-started-with-tmux/) session to keep the transfer running if your terminal disconnects. ## SCP Command Syntax [\#](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#scp-command-syntax) The general syntax of the `scp` command is: txt ``` 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 [\#](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#copy-a-local-file-to-a-remote-system) To copy a file from the local machine to a remote server, run: Terminal ``` scp file.txt remote_username@10.10.0.2:/remote/directory ``` In 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: output ``` remote_username@10.10.0.2's password: file.txt 100% 14KB 82.1KB/s 00:00 ``` To save the file under a different name on the remote host, specify the new filename in the destination path: Terminal ``` scp file.txt remote_username@10.10.0.2:/remote/directory/newfilename.txt ``` If SSH on the remote host is listening on a port other than the default 22, use the `-P` option: Terminal ``` scp -P 2322 file.txt remote_username@10.10.0.2:/remote/directory ``` To copy a directory and all its contents, use the `-r` flag for recursive copy: Terminal ``` scp -r /local/directory remote_username@10.10.0.2:/remote/directory ``` When using wildcards such as `*` or `?`, enclose the path in quotes to prevent shell expansion on the local machine. In the following example, we are copying all `.txt` files from the local `Projects` directory to the remote `Projects` directory: Terminal ``` 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: Terminal ``` 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: Terminal ``` scp -i ~/.ssh/id_ed25519 file.txt remote_username@10.10.0.2:/remote/directory/ ``` ## Copy a Remote File to the Local System [\#](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#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: Terminal ``` scp remote_username@10.10.0.2:/remote/file.txt /local/directory ``` If you have not set up [passwordless SSH login](https://linuxize.com/post/how-to-setup-passwordless-ssh-login/) , you will be prompted to enter the user password. To copy an entire remote directory, add the `-r` flag: Terminal ``` scp -r remote_username@10.10.0.2:/remote/directory /local/directory ``` ## Copy Files Between Two Remote Systems [\#](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#copy-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`: Terminal ``` scp user1@host1.com:/files/file.txt user2@host2.com:/files ``` You 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: Terminal ``` scp -3 user1@host1.com:/files/file.txt user2@host2.com:/files ``` ## Using an SSH Config File [\#](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#using-an-ssh-config-file) If you regularly connect to the same hosts, defining them in the [SSH config file](https://linuxize.com/post/using-the-ssh-config-file/) simplifies your `scp` commands. Create or edit the file at `~/.ssh/config`: ~/.ssh/configssh ``` Host myserver HostName 10.10.0.2 User leah Port 2222 IdentityFile ~/.ssh/id_ed25519 ``` With this configuration, you can use the alias instead of the full connection details: Terminal ``` scp file.txt myserver:/remote/directory ``` Setting up [SSH key-based authentication](https://linuxize.com/post/how-to-setup-passwordless-ssh-login/) removes the password prompt entirely, making transfers faster and easier to script. ## Troubleshooting [\#](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#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 [\#](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#quick-reference) | 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 | For a printable quick reference, see the [SCP Cheatsheet](https://linuxize.com/cheatsheet/scp/) . ## FAQ [\#](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#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`. **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`](https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/) with the `--partial` flag. **How do I copy files without a password prompt?** Set up [SSH key-based authentication](https://linuxize.com/post/how-to-setup-passwordless-ssh-login/) 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 [\#](https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/#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`](https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/) or [`sftp`](https://linuxize.com/post/how-to-use-linux-sftp-command-to-transfer-files/) instead. If you have any questions, feel free to leave a comment below. ## Tags [ssh](https://linuxize.com/tags/ssh/) [linux commands](https://linuxize.com/tags/linux-commands/) ## Linuxize Weekly Newsletter A quick weekly roundup of new tutorials, news, and tips. ## About the authors ![Dejan Panovski](https://linuxize.com/images/authors/dejan-panovski.jpg) 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](https://linuxize.com/authors/dejan-panovski/) ## [SSH Essentials](https://linuxize.com/series/ssh-essentials/) [View all parts](https://linuxize.com/series/ssh-essentials/) Part 18 of 26 [Previous · Part 17 How to Set up SSH SOCKS Tunnel for Private Browsing](https://linuxize.com/post/how-to-setup-ssh-socks-tunnel-for-private-browsing/) [Next · Part 19 How to Use SFTP Command to Transfer Files](https://linuxize.com/post/how-to-use-linux-sftp-command-to-transfer-files/) ## More Like This - [How to Fix SSH "Permission Denied (publickey)" Error](https://linuxize.com/post/fix-ssh-permission-denied-publickey/) - [SSH Hardening: Best Practices for Securing Your Server](https://linuxize.com/post/ssh-hardening-best-practices/) - [How to Copy SSH Keys with ssh-copy-id](https://linuxize.com/post/ssh-copy-id-command/) - [SSH Essentials](https://linuxize.com/series/ssh-essentials/) - [How to Generate SSH Keys on Linux](https://linuxize.com/post/how-to-generate-ssh-keys-on-linux/) ## You May Also Like [Feb 6, 2026 • 9 min read How to Use SFTP Command to Transfer Files Learn how to use the sftp command to securely transfer files, navigate directories, and manage remote files over SSH.](https://linuxize.com/post/how-to-use-linux-sftp-command-to-transfer-files/) [Mar 3, 2026 • 8 min read ssh Command in Linux: Connect to Remote Servers Connect to remote servers securely using the ssh command. Covers basic connections, key-based authentication, SSH config files, running remote commands, port forwarding, and troubleshooting.](https://linuxize.com/post/ssh-command-in-linux/) [Apr 9, 2026 • 4 min read file Command in Linux: Determine File Types Identify file types from the command line using the Linux file command. Covers basic usage, MIME types, compressed files, symlinks, and the magic database.](https://linuxize.com/post/linux-file-command/) ## Comments © 2026 Linuxize.com \| A Raptive Partner Site [Write For Us](https://linuxize.com/write-for-us/) [Support Us](https://linuxize.com/donate/) [Privacy Policy](https://linuxize.com/privacy-policy/) [Editorial Policy](https://linuxize.com/editorial-policy/) [Terms](https://linuxize.com/terms-of-service/) [Contact](https://linuxize.com/contact/)
Readable Markdown
SCP (Secure Copy) is a command-line utility that copies files and directories between hosts over an SSH connection. Both the transferred data and the authentication credentials are encrypted, which protects against interception and eavesdropping. OpenSSH 9.0 and newer versions use the SFTP protocol internally when you run `scp` by default. The legacy SCP protocol is considered outdated. For new workflows, consider using [`sftp`](https://linuxize.com/post/how-to-use-linux-sftp-command-to-transfer-files/) or [`rsync`](https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/) directly. 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 how `scp` distinguishes 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 `scp` command inside a [`screen`](https://linuxize.com/post/how-to-use-linux-screen/) or [`tmux`](https://linuxize.com/post/getting-started-with-tmux/) session to keep the transfer running if your terminal disconnects. ## SCP Command Syntax The general syntax of the `scp` command is: - `[[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: In 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: To save the file under a different name on the remote host, specify the new filename in the destination path: If SSH on the remote host is listening on a port other than the default 22, use the `-P` option: To copy a directory and all its contents, use the `-r` flag for recursive copy: When using wildcards such as `*` or `?`, enclose the path in quotes to prevent shell expansion on the local machine. In the following example, we are copying all `.txt` files from the local `Projects` directory to the remote `Projects` directory: To preserve file metadata (modification time, access time, and mode), use the `-p` option: To use a specific SSH key for authentication, pass it with the `-i` option: ## 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: If you have not set up [passwordless SSH login](https://linuxize.com/post/how-to-setup-passwordless-ssh-login/) , you will be prompted to enter the user password. To copy an entire remote directory, add the `-r` flag: ## Copy 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`: You 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: ## Using an SSH Config File If you regularly connect to the same hosts, defining them in the [SSH config file](https://linuxize.com/post/using-the-ssh-config-file/) simplifies your `scp` commands. Create or edit the file at `~/.ssh/config`: With this configuration, you can use the alias instead of the full connection details: Setting up [SSH key-based authentication](https://linuxize.com/post/how-to-setup-passwordless-ssh-login/) 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 | 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 | For a printable quick reference, see the [SCP Cheatsheet](https://linuxize.com/cheatsheet/scp/) . ## 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`. **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`](https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/) with the `--partial` flag. **How do I copy files without a password prompt?** Set up [SSH key-based authentication](https://linuxize.com/post/how-to-setup-passwordless-ssh-login/) 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`](https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/) or [`sftp`](https://linuxize.com/post/how-to-use-linux-sftp-command-to-transfer-files/) instead. If you have any questions, feel free to leave a comment below. Tags Linuxize Weekly Newsletter A quick weekly roundup of new tutorials, news, and tips. About the authors ![Dejan Panovski](https://linuxize.com/images/authors/dejan-panovski.jpg) 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](https://linuxize.com/authors/dejan-panovski/) Part 18 of 26
ML Classification
ML Categories
/Computers_and_Electronics
88.1%
/Computers_and_Electronics/Networking
54.1%
/Computers_and_Electronics/Networking/Data_Formats_and_Protocols
43.6%
/Internet_and_Telecom
38.7%
/Internet_and_Telecom/Communications_Equipment
15.9%
/Internet_and_Telecom/Communications_Equipment/Other
13.8%
Raw JSON
{
    "/Computers_and_Electronics": 881,
    "/Computers_and_Electronics/Networking": 541,
    "/Computers_and_Electronics/Networking/Data_Formats_and_Protocols": 436,
    "/Internet_and_Telecom": 387,
    "/Internet_and_Telecom/Communications_Equipment": 159,
    "/Internet_and_Telecom/Communications_Equipment/Other": 138
}
ML Page Types
/Article
99.6%
/Article/Tutorial_or_Guide
97.4%
Raw JSON
{
    "/Article": 996,
    "/Article/Tutorial_or_Guide": 974
}
ML Intent Types
Informational
99.9%
Raw JSON
{
    "Informational": 999
}
Content Metadata
Languageen-us
AuthorDejan Panovski
Publish Time2018-09-18 23:31:47 (7 years ago)
Original Publish Time2018-09-18 23:31:47 (7 years ago)
RepublishedNo
Word Count (Total)1,741
Word Count (Content)1,319
Links
External Links5
Internal Links33
Technical SEO
Meta NofollowNo
Meta NoarchiveNo
JS RenderedNo
Redirect Targetnull
Performance
Download Time (ms)73
TTFB (ms)56
Download Size (bytes)20,218
Shard72 (laksa)
Root Hash253233583582590872
Unparsed URLcom,linuxize!/post/how-to-use-scp-command-to-securely-transfer-files/ s443