Linux scp Command: Syntax, Examples, and Common Use Cases
Linux scp command: Syntax, Examples, Common Use Cases
The scp command is a widely used Linux utility for securely transferring files between local and remote systems. Whether you need to upload files to a VPS, download files from a server, or copy configuration files securely, scp provides a fast and reliable way to move data over an encrypted SSH connection.
Included with OpenSSH on most Linux distributions, scp is available without installing additional software. In many cases, a single command is all you need to transfer files or entire directories securely.
This guide explains how the Linux scp command works, covers its basic syntax, walks through the most common usage examples, and shows how to troubleshoot common transfer issues.
What Is the Linux scp Command?
The Secure Copy Protocol (scp) is a command-line utility that securely transfers files and directories over SSH (Secure Shell). By using SSH for authentication and encryption, scp protects both transferred data and login credentials.
Unlike traditional file transfer protocols such as FTP, scp encrypts all communication between systems. This makes it a practical choice for Linux administrators, developers, and VPS users who regularly manage remote servers.
scp is included with the OpenSSH package and is available by default on most Linux distributions, including Ubuntu, Debian, AlmaLinux, Rocky Linux, Fedora, and CentOS.
Note: Since OpenSSH 9.0, scp uses the SFTP protocol internally by default instead of the legacy SCP protocol. For most users, this change is transparent, and the command syntax remains the same.
Linux scp Command Syntax
The basic scp syntax is:
scp [options] source destination
Where:
● Options – modify how the command behaves.
● Source – the file or directory to copy.
● Destination – where the file or directory will be copied.
When copying files to or from another system, specify the remote location in the following format:
username@hostname:/path/to/file
or
username@ip-address:/path/to/file
scp report.pdf admin@203.0.113.15:/home/admin/documents/
The local file report.pdf is uploaded to the /home/admin/documents/ directory on the remote server.
Most Common scp Commands and Examples
The examples below cover the most common scp tasks you'll encounter when managing Linux systems, from uploading files to transferring entire directories between servers.
If you're new to Linux administration, it's also worth becoming familiar with other Essential Linux Commands for managing files, users, and servers.
Upload a File to a Remote Server
Uploading files from your local computer to a remote server is one of the most common uses of scp, whether you're deploying a website, transferring backups, or uploading configuration files to a VPS.
scp backup.sql admin@203.0.113.15:/home/admin/backups/
The file backup.sql is uploaded to the /home/admin/backups/ directory on the remote server.
The destination directory must already exist, and your account needs permission to write to it.
Before transferring large backups or website archives, check that the destination has enough available disk space. Our guide on How to Check Disk Usage in Linux explains several ways to verify free disk space.
Download a File from a Remote Server
To download a file, simply reverse the source and destination.
scp admin@203.0.113.15:/var/log/nginx/error.log
The log file is copied to the current local directory.
To save the file elsewhere, specify a different local destination:
scp admin@203.0.113.15:/var/log/nginx/error.log ~/Downloads/
The log file is downloaded directly to the ~/Downloads/ directory.
Copy an Entire Directory
To copy an entire directory, use the recursive -r option.
scp -r website/ admin@203.0.113.15:/var/www/html/
The entire website directory, including all subdirectories, is copied to the destination.
Without the -r option, scp copies only individual files and returns an error if the source is a directory.
If you're preparing large website archives or backups before transferring them, it can also be helpful to find large files in Linux to identify unnecessary files and reduce transfer time.
Authenticate with an SSH Key
Many production servers disable password authentication and require SSH keys.
scp -i ~/.ssh/id_ed25519 backup.sql admin@203.0.113.15:/home/admin/backups/
The -i option tells scp which private key to use during authentication. SSH key authentication is generally more secure than password-based authentication.
Connect Using a Custom SSH Port
By default, SSH listens on port 22. If your server uses a different port, specify it with the uppercase -P option
scp -P 2222 backup.tar.gz admin@203.0.113.15:/home/admin/
scp connects to the remote server over SSH port 2222 instead of the default port 22.
Tip: The -P option uses an uppercase letter. The lowercase -p serves a different purpose and preserves file timestamps and permissions.
Preserve File Metadata
To preserve the original modification time, access time, and file permissions, use the lowercase -p option.
scp -p nginx.conf admin@203.0.113.15:/etc/nginx/
Preserving metadata is particularly useful when transferring configuration files, restoring backups, or comparing different versions of a file.
Compress Data During Transfer
Use the -C option to enable compression for text-based files such as logs, configuration files, and database dumps.
scp -C backup.sql admin@203.0.113.15:/home/admin/backups/
The -C option enables compression during the transfer.
Keep in mind that compression provides little or no benefit for files that are already compressed, such as ZIP archives, JPEG images, videos, or .tar.gz files.
Copy Files Between Two Remote Servers
scp can also copy files directly between two remote systems.
scp admin@server1.example.com:/home/admin/backup.tar.gz \
admin@server2.example.com:/home/admin/backups/
Note: Depending on your OpenSSH version and authentication setup, copying files directly between two remote servers may require the -3 option.
This approach is useful when migrating data, moving backups between servers, or transferring files between different environments without downloading them to your local machine first.
Common Linux scp Options
The table below summarizes the scp options you'll use most often.
| Option | Description |
|---|---|
| -r | Copy directories recursively. |
| -P | Use a custom SSH port. |
| -i | Authenticate with an SSH private key. |
| -p | Preserve timestamps and permissions. |
| -C | Compress data during transfer. |
| -v | Show verbose output for troubleshooting. |
| -q | Suppress progress messages. |
| -l | Limit transfer bandwidth. |
Common Errors and Troubleshooting
Most scp problems are caused by authentication issues, incorrect file paths, or SSH connectivity rather than the command itself. The following sections explain the errors you're most likely to encounter and how to resolve them.
Permission denied (publickey)
This error usually indicates that authentication failed.
Verify that:
● You're using the correct username.
● Your SSH key or password is valid.
● The destination directory is writable.
● The correct private key is specified with the -i option, if required.
Use the -i option to specify the private key required for authentication:
scp -i ~/.ssh/id_ed25519 backup.sql admin@203.0.113.15:/home/admin/backups/
No such file or directory
This means scp cannot locate the source file or the destination path.
Verify that:
● The filename is correct.
● The directory exists.
● The path is spelled correctly.
● You're copying the file in the intended direction.
Connection refused
ssh: connect to host 203.0.113.15 port 22: Connection refused
The server is reachable, but the SSH service is not accepting connections.
This usually happens when:
● The SSH service is stopped.
● SSH is listening on another port.
● A firewall blocks incoming connections.
If SSH listens on a non-default port, remember to specify it with the -P option.
Connection timed out
A timeout usually means your computer cannot establish a connection to the remote server.
Possible causes include:
● An incorrect IP address or hostname.
● Network connectivity problems.
● Firewall restrictions.
● The server is offline.
Verify network connectivity before troubleshooting scp itself.
Host key verification failed
This message appears when the SSH host key stored on your computer no longer matches the server's current host key.
This often happens after:
● Reinstalling a server.
● Redeploying a VPS.
● Changing SSH host keys.
Never remove a host key without first confirming that the server's identity has changed intentionally.
Quick Troubleshooting Checklist
If you're not sure why a transfer failed, work through this checklist:
● Verify that SSH access works independently of scp.
● Confirm the source and destination paths.
● Check file and directory permissions.
● Verify the SSH port.
● Run the command with the -v option for detailed diagnostic output.
scp vs rsync: What's the Difference?
Both scp and rsync transfer files securely over SSH, but they are designed for different tasks.
Use scp when you need to:
● Upload website files.
● Download server logs.
● Transfer configuration files.
● Copy backups between systems.
Use rsync when you need to:
● Synchronize directories.
● Transfer large datasets.
● Resume interrupted transfers.
● Automate recurring backups.
If you regularly synchronize files or automate backups, see our guide to the Linux rsync command.
Conclusion
The scp command remains one of the simplest and most reliable tools for securely transferring files between Linux systems. Its straightforward syntax and integration with OpenSSH make it a standard choice for everyday administrative tasks, from uploading website files to transferring backups and configuration files.
Once you're familiar with a handful of commonly used options, scp becomes a valuable tool for everyday Linux administration.
FAQ
You ask, and we answer! Here are the most frequently asked questions!
-
Is scp installed by default on Linux?
- In most cases, yes. The scp utility is included in the OpenSSH package, which is installed by default on many Linux distributions, including Ubuntu, Debian, Rocky Linux, AlmaLinux, and Fedora.
You can verify that scp is available by running:
which scp
If scp isn't installed, install the OpenSSH client using your distribution's package manager.
- In most cases, yes. The scp utility is included in the OpenSSH package, which is installed by default on many Linux distributions, including Ubuntu, Debian, Rocky Linux, AlmaLinux, and Fedora.
-
Does scp overwrite existing files?
- Yes. If a file with the same name already exists in the destination directory, scp replaces it without asking for confirmation.
To keep the existing file, rename it before transferring or copy the new file using a different filename.
- Yes. If a file with the same name already exists in the destination directory, scp replaces it without asking for confirmation.
-
Can scp resume interrupted transfers?
- No. If the connection is interrupted, the transfer starts again from the beginning.
For large files or recurring backups, rsync is usually a better choice because it can resume interrupted transfers and copy only changed data.
- No. If the connection is interrupted, the transfer starts again from the beginning.
-
Is scp secure?
- Yes. All data transferred with scp is encrypted using SSH, including authentication credentials and file contents. This makes it suitable for transferring sensitive files across both local networks and the Internet.
For improved security, use SSH key authentication instead of passwords whenever possible.
- Yes. All data transferred with scp is encrypted using SSH, including authentication credentials and file contents. This makes it suitable for transferring sensitive files across both local networks and the Internet.
-
What is the difference between scp and SFTP?
- Both scp and SFTP use SSH for secure communication, but they serve different purposes.
The scp command is designed for fast, non-interactive file transfers, while SFTP provides an interactive interface for browsing directories, transferring files, and managing remote file systems.
If you only need to copy files, scp is usually the simpler choice.
- Both scp and SFTP use SSH for secure communication, but they serve different purposes.