3v-Hosting Blog
Linux scp Command and Examples of Its Use
6 min read
Securely transferring files between systems is a core task in system administration and development workflows. The scp (secure copy) command, available on most Unix-like systems, including Linux-servers, is the most efficient way to copy files and directories between local and remote systems over SSH. Unlike traditional cp for local copying, scp is designed for secure transfers using SSH encryption, making it especially relevant for remote operations and automated scripts in hosting environments.
The Basics of scp Syntax
The basic syntax of the scp command follows a consistent structure that is easy to understand with a few examples:
scp [options] source destination
The source and destination can be local files, directories, or remote addresses defined using the format user@host:/path. This flexibility allows scp to copy in three common directions:
- From local to remote
- From remote to local
- From one remote system to another (through the local machine)
For instance, to upload a local file report.pdf to a remote server:
scp report.pdf [email protected]:/home/user/docs/
To retrieve a file from a remote server:
scp [email protected]:/home/user/docs/report.pdf .
Both operations require SSH access to the remote system. If the user is authenticated with SSH keys, the command will proceed without prompting for a password. Otherwise, a password will be required for each connection.
Transferring Directories with scp
By default, scp is designed for copying single files. However, it can recursively copy entire directories using the -r (recursive) flag. This is essential when moving large file structures or backing up folders between environments.
scp -r project_folder/ user@server:/var/www/
This command sends the entire project_folder directory to the specified path on the remote server. All files and subdirectories are preserved, and the permissions are maintained unless explicitly overridden using additional flags.
Other useful articles on administration in our Blog:
- Linux cat Command Guide
- How To Install Nginx on Ubuntu 22.04
- Installing and Using Virtualenv with Python3
- How to Install Node.js on Ubuntu 22.04
Common Options to Enhance File Transfers
The scp command includes a number of flags that can optimize or control the behavior of the file transfer process:
-P <port> Specifies a port if the remote SSH server runs on a non-standard port.
-C Enables compression to speed up large file transfers over slow connections.
-p Preserves modification times, access times, and modes from the original file.
-q Quiet mode. Suppresses progress meter and non-error messages.
-l <limit> Limits the bandwidth used, specified in Kbit/s.
For example, transferring files over a non-standard port with compression might look like:
scp -P 2222 -C bigfile.tar.gz user@host:/data/
This command transfers the file using SSH port 2222 and applies compression to optimize throughput.
Security Considerations and Best Practices
SCP is generally secure due to its reliance on SSH, but administrators must ensure that the remote SSH server is properly configured to prevent unauthorized access. Strong SSH key authentication is preferable over password-based logins, especially in automated deployment or CI/CD pipelines. Disabling root login via SSH is also a common practice to minimize risks.
In production environments where performance and scalability matter, SCP is not the most efficient solution. Tools like rsync or sftp offer additional features like delta transfers or interactive sessions. However, for quick and secure one-time copy operations, scp remains the clear choice.
Advanced Use Cases in Hosting and DevOps Workflows
In managed hosting environments, scp is often used for tasks such as transferring website backups, uploading configuration files to remote servers, or retrieving log files for diagnostics. DevOps engineers may include scp in scripts that deploy new application versions to remote nodes.
Consider a scenario where a nightly backup script pushes database dumps from a staging server to an external storage host:
scp /backups/db-$(date +%F).sql.gz backupuser@storage:/mnt/backups/
This type of command can be scheduled via cron for automation. Combined with SSH key-based access and encrypted backup files, it creates a reliable backup routine.
Another advanced usage is transferring files between two remote hosts from a third machine:
scp user1@host1:/var/www/index.html user2@host2:/var/www/
This is helpful when migrating services or data without downloading files to the intermediary system. Note that this use case requires SSH trust and access permissions between the initiating machine and both remote systems.
Limitations and Alternative Tools
Despite its simplicity, scp does have limitations. It does not support resume functionality for interrupted transfers, which can be problematic for large files or unstable network connections. It also lacks fine-grained progress indicators or synchronization logic. For these needs, tools like rsync are more robust:
rsync -avz -e ssh myfolder/ user@remote:/home/user/
Nonetheless, scp still holds a valuable place in the Linux ecosystem due to its universal availability and simplicity. It requires no setup on the target host beyond SSH, making it a great choice for ad-hoc transfers or environments with limited tooling.
Conclusion
The scp command is the go-to utility for secure file transfers on Linux and Unix-like systems. It combines the familiarity of the cp command with the security of SSH, enabling simple yet effective remote file operations. System administrators, developers, and hosting providers use scp for one-off transfers, deployment tasks, and quick automation. While it may not be the best solution for large-scale synchronization, its convenience and universal presence on modern systems make it a reliable go-to tool in many workflows.