Home / Blog / SCP Command in Linux: A Complete Guide to Secure File Transfer

SCP Command in Linux: A Complete Guide to Secure File Transfer

Learn how to use the Linux scp command to securely copy files and directories between local and remote systems over SSH. Understand SCP syntax, common options, practical examples, permissions, troubleshooting, and best practices for secure server-to-server file transfers.

SCP Command in Linux: A Complete Guide to Secure File Transfer

Moving files between Linux servers is a common requirement for developers, system administrators, and DevOps teams. Linux provides several tools for transferring files, and SCP (Secure Copy Protocol) is one of the simplest options for securely copying files between systems.

The scp command uses SSH to transfer data between computers. This means that files are transmitted through an encrypted SSH connection rather than being sent as plain text.

SCP is particularly useful when working with remote Linux servers, cloud infrastructure, deployment environments, backups, and application files.

What Is SCP in Linux?

SCP stands for Secure Copy. It is a command-line utility traditionally used to securely copy files between a local system and a remote system.

A basic SCP command looks like this:

scp file.txt username@remote-server:/path/to/destination/

In this example:

file.txt is the local file.
username is the account on the remote server.
remote-server is the remote host.
/path/to/destination/ is where the file will be copied.

SCP uses SSH for authentication and encrypted communication.

SCP Command Syntax

The general syntax for copying a local file to a remote server is:

scp [options] source username@host:destination

To copy a remote file to your local computer:

scp [options] username@host:source destination

To copy files between two remote systems:

scp [options] username1@host1:source username2@host2:destination

The exact syntax depends on the direction of the transfer.

Copy a Local File to a Remote Server

Suppose you have a file named backup.zip and want to upload it to a remote server:

scp backup.zip user@example.com:/home/user/

SCP authenticates with the remote server and transfers the file.

After successful authentication, the file will be available in the specified remote directory.

Copy a Remote File to Your Local System

You can also download a file from a remote server:

scp user@example.com:/home/user/backup.zip .

The . represents the current local directory.

You can specify a different local destination:

scp user@example.com:/home/user/backup.zip /home/user/downloads/
Copy a File Between Remote Servers

SCP can also be used to transfer files between remote systems.

For example:

scp user1@server1:/var/www/app.zip user2@server2:/home/user/

Depending on the SCP implementation and configuration, the transfer may involve the local machine as an intermediary or use remote-copy capabilities.

For modern environments, always verify how your installed SCP implementation handles remote-to-remote transfers, particularly when working with sensitive production data.

Copy an Entire Directory

To copy a directory and its contents, use the -r option:

scp -r project user@example.com:/home/user/

The -r option means recursive.

This allows SCP to copy:

Files
Subdirectories
Nested directory structures

For example:

project/
├── index.html
├── css/
│   └── style.css
└── images/
    └── logo.png

Using:

scp -r project user@example.com:/home/user/

copies the complete directory structure.

Specify an SSH Port

SSH normally uses port 22, but a server may be configured to use another port.

Use the -P option to specify the SSH port:

scp -P 2222 backup.zip user@example.com:/home/user/

Here, SCP connects to port 2222.

Be careful to use an uppercase -P. SCP options are case-sensitive.

Use a Specific SSH Identity File

If you authenticate using an SSH private key, you can specify it with -i:

scp -i ~/.ssh/my-key.pem backup.zip user@example.com:/home/user/

This is commonly useful with cloud servers and environments configured for SSH key authentication.

Make sure the private key has appropriate permissions and is protected from unauthorized access.

Copy Multiple Files

You can specify multiple local files in one SCP command:

scp file1.txt file2.txt file3.txt user@example.com:/home/user/

All specified files are copied to the destination directory.

You can also use shell patterns when appropriate:

scp *.log user@example.com:/home/user/logs/

This can be useful when transferring multiple files with a common extension.

Preserve File Attributes

The -p option attempts to preserve file modification times, access times, and modes:

scp -p backup.zip user@example.com:/home/user/

This can be useful when maintaining file metadata during transfers.

Enable Compression

The -C option enables compression during the SSH transfer:

scp -C backup.zip user@example.com:/home/user/

Compression can sometimes help when transferring data over slower connections, although its benefit depends on the type of data.

Already-compressed files such as ZIP archives, JPEG images, and many video formats generally gain little from additional compression.

Limit Transfer Bandwidth

SCP supports the -l option for limiting bandwidth.

For example:

scp -l 5000 backup.zip user@example.com:/home/user/

The value is specified in kilobits per second.

Bandwidth limiting can be useful when transferring large files over a connection that is also being used by other services.

Display More Transfer Information

The -v option enables verbose SSH output:

scp -v backup.zip user@example.com:/home/user/

This can be helpful when troubleshooting authentication, connection, or SSH configuration problems.

Verbose output may contain technical connection details, so avoid sharing it publicly without reviewing it first.

Copy Files Using a Different Remote Username

If the remote username is different from your local username, specify it explicitly:

scp backup.zip deploy@example.com:/var/www/

Here, deploy is the account used on the remote system.

This is especially useful for deployment accounts and restricted server users.

Copy Files From a Specific Remote Directory

You can specify the complete remote path:

scp deploy@example.com:/var/www/app/config.php .

This downloads config.php from the remote server to the current directory.

Copy a Directory to a Remote Server

For an application directory:

scp -r myapp deploy@example.com:/var/www/

This copies myapp and its contents to /var/www/.

When working with web applications, ensure the remote user has appropriate permissions to write to the destination.

Copy a Directory From a Remote Server

You can download an entire directory:

scp -r deploy@example.com:/var/www/myapp ./myapp

The remote directory and its contents will be copied to the local system.

SCP With IPv6

When connecting to an IPv6 address, you may need to use the appropriate address syntax supported by your SCP and SSH implementation.

For example, an IPv6 literal may need to be enclosed in brackets depending on the command and context.

For production environments, using a hostname is generally easier to manage than manually specifying IPv6 addresses.

SCP With SSH Configuration

If you frequently connect to the same servers, you can simplify SCP commands by configuring SSH aliases in:

~/.ssh/config

For example:

Host production
    HostName example.com
    User deploy
    Port 2222
    IdentityFile ~/.ssh/production_key

You can then use:

scp backup.zip production:/home/deploy/

This makes commands shorter and reduces the possibility of repeatedly entering connection parameters.

SCP Authentication

SCP relies on SSH authentication.

Depending on your server configuration, authentication may use:

Passwords
SSH keys
Hardware-backed authentication
Other SSH-supported authentication mechanisms

SSH key authentication is commonly preferred for automated server operations because it avoids storing passwords in scripts.

SCP and File Permissions

A successful SCP transfer does not necessarily mean the application can use the copied file.

For example, you might successfully upload:

scp application.php user@example.com:/var/www/app/

but the web server may not have permission to read it.

You may need to review:

ls -l /var/www/app/

and, where appropriate, adjust ownership or permissions according to your server's security model.

Avoid blindly using broad permissions such as:

chmod 777

on production files or directories.

Common SCP Errors
Permission Denied

You may see:

Permission denied

This can occur because:

The SSH credentials are incorrect.
The remote user cannot access the destination.
The private key permissions are incorrect.
The destination directory is not writable.

Check the SSH connection separately:

ssh user@example.com

If SSH itself fails, investigate authentication or server configuration first.

No Such File or Directory

An error such as:

No such file or directory

usually means the source or destination path is incorrect.

Check the remote directory:

ssh user@example.com

Then:

ls -la /path/to/directory
Connection Refused

If SCP reports:

Connection refused

possible causes include:

SSH is not running.
The server is listening on another port.
A firewall is blocking the connection.
The hostname points to the wrong system.

You can test the SSH connection directly:

ssh user@example.com

If the server uses a custom port:

ssh -p 2222 user@example.com
Host Key Warning

SSH may warn you when a server's host key changes.

Do not simply bypass the warning. A changed host key can be caused by legitimate server replacement, but it can also indicate a security problem.

Verify the server identity through a trusted channel before accepting a changed host key.

SCP vs SFTP

SCP and SFTP both use SSH, but they serve somewhat different purposes.

SCP is primarily designed around copying files from one location to another.

SFTP provides a richer interactive file-transfer protocol with commands for browsing directories, transferring files, renaming files, deleting files, and managing remote filesystem operations.

For simple one-off transfers, SCP can be convenient.

For interactive file management or applications requiring a file-transfer protocol, SFTP may be more appropriate.

SCP vs Rsync

rsync is another popular tool for transferring files between Linux systems.

For example:

rsync -avz project/ user@example.com:/home/user/project/

Rsync is particularly useful when synchronizing directories repeatedly because it can transfer only changed portions of files.

SCP is often simpler for straightforward file copying.

Choose the tool based on the requirement rather than using SCP for every transfer scenario.

SCP for Application Deployment

SCP can be used as part of a simple deployment process.

For example:

scp application.tar.gz deploy@example.com:/tmp/

You could then connect to the server:

ssh deploy@example.com

and deploy the application from the transferred archive.

For larger production systems, dedicated CI/CD pipelines and deployment tools usually provide better consistency, rollback capabilities, auditing, and automation.

SCP for Backups

SCP can also be used to copy backup files to another server:

scp database-backup.sql backup@example.com:/backups/

For automated backups, consider factors such as:

Encryption
Authentication
Retention
Storage capacity
Transfer reliability
Backup verification
Recovery testing

Simply copying a backup file does not guarantee that the backup is usable.

SCP in Cloud Environments

SCP is frequently used when working with Linux-based cloud servers.

For example:

scp -i cloud-key.pem application.zip ubuntu@server-ip:/home/ubuntu/

This allows developers and administrators to move files between their workstation and a cloud-hosted Linux instance.

Cloud firewalls and security groups must allow the relevant SSH connection.

SCP Security Best Practices

Because SCP transfers data over SSH, it benefits from SSH's encrypted transport. However, secure usage still requires good operational practices.

Follow these recommendations:

Use strong SSH authentication.
Prefer SSH keys over passwords for automation.
Protect private keys carefully.
Verify remote host identities.
Avoid transferring sensitive files unnecessarily.
Use least-privilege remote accounts.
Restrict SSH access where possible.
Avoid embedding passwords or private keys in scripts.
Review file permissions after transfers.
Use SFTP or other modern transfer mechanisms when they better match the requirement.
Frequently Asked Questions
What is SCP in Linux?

SCP is a command-line utility traditionally used to securely copy files between local and remote systems using SSH.

How do I copy a file using SCP?

Use:

scp file.txt user@example.com:/home/user/
How do I copy a directory using SCP?

Use the recursive option:

scp -r myfolder user@example.com:/home/user/
How do I specify an SSH port with SCP?

Use the uppercase -P option:

scp -P 2222 file.txt user@example.com:/home/user/
Is SCP secure?

SCP traditionally uses SSH for encrypted communication and authentication. However, secure configuration of SSH credentials, host verification, permissions, and access controls remains important.

Is SCP better than SFTP?

Neither is universally better. SCP is convenient for straightforward copying, while SFTP provides more comprehensive remote file-management capabilities. The appropriate choice depends on the workflow.

Conclusion

The SCP command in Linux provides a convenient way to securely transfer files and directories between systems using SSH. It is particularly useful for developers, Linux administrators, DevOps engineers, and teams managing remote servers.

By understanding commands such as scp, scp -r, scp -P, and scp -i, you can handle many common file-transfer requirements from the Linux terminal.

For more complex synchronization, interactive file management, and automated deployments, tools such as rsync, SFTP, or dedicated CI/CD solutions may be better suited.

SCP remains a valuable Linux command to understand, especially when working with remote servers, cloud infrastructure, and software deployment environments. Solace Infotech's sitemap lists SCP among its Linux command resources and also includes a related “secure cp (copy)” topic.

Contact Us

1119 W Duarte Rd, Arcadia, CA 91007

Solace Infotech Pvt. Ltd, Supreme HQ,
          HQ3C+9F2, Yash Orchid Society,
          Baner, Pune, Maharashtra 411021

4th Floor, Samraat Nucleus,
           Mumbai Naka, Nashik - 422001