Home Learn Linux Scp command in Linux: Syntax, options, and examples

Scp command in Linux: Syntax, options, and examples

This article provides a detailed overview of using the scp (Secure Copy Protocol) command in Linux, a powerful tool for securely transferring files between hosts over a network. Learn the command's syntax, options, and practical use cases to enhance your file management and transfer tasks in Linux environments.

by Arun Kumar
scp command linux

Let’s get into the world of Linux file transfers with the powerful scp command, a staple in the toolkit of Linux aficionados! Whether you’re transferring files to a remote server or fetching data from another machine, scp stands out with its blend of simplicity, security, and efficiency.

From basic file transfers to advanced usage scenarios, this guide unpacks the scp command with practical examples, troubleshooting tips, and a handy cheat sheet. Get ready to elevate your command-line prowess and tackle file transfers like a pro, all while navigating common hiccups with ease and discovering the nuances that make scp an indispensable tool in the Linux universe.

What is the scp command in Linux?

scp stands for Secure Copy Protocol. It’s a command-line utility that allows you to securely transfer files between two machines over a network. It leverages SSH (Secure Shell) for data transfer, providing the same level of security and requiring the same authentication methods. What I particularly love about scp is its simplicity and effectiveness. It’s like the Swiss Army knife for file transfers—reliable, versatile, and secure.

Basic syntax of scp

The basic syntax for scp is quite straightforward:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
  • OPTION: scp options such as -p (preserve file attributes) or -r (recursive copy for directories).
  • SRC_HOST, DEST_HOST: Source and destination hosts. Omit for the local host.
  • file1, file2: Source and destination files or directories.

Simple file transfer

Let’s start with a basic example. Suppose you want to copy a file named example.txt from your local machine to a remote server. Here’s how you’d do it:

scp example.txt username@remotehost:/remote/directory/

This command will prompt you for the username‘s password on remotehost. Upon successful authentication, example.txt will be copied to /remote/directory/ on remotehost.

Output:

Assuming the transfer was successful, you typically won’t see much output, which is something I appreciate about scp—it’s quiet yet effective. However, if you’re like me and prefer to see what’s going on under the hood, you can add the -v (verbose) option to get detailed information about the transfer process.

Copying a directory

To copy a directory and its contents, use the -r (recursive) option. For instance, to copy a directory named example_dir from your local machine to a remote host:

scp -r example_dir username@remotehost:/remote/directory/

Output:

Again, without the verbose option, scp remains quite silent, quietly doing its job. The directory example_dir and its contents will be replicated at /remote/directory/ on the remote host.

Securing and optimizing transfers

One thing I’m particularly keen on is security and efficiency. Here are a couple of tips:

  • Use -C to enable compression. This can speed up transfers significantly, especially for large files or slow networks.
  • Always consider using -P [port] if your SSH server listens on a non-default port.

Advanced usage: Preserving file attributes

For those who are meticulous about file details, the -p option is a godsend. It preserves the modification times, access times, and modes from the original file.

scp -p example.txt username@remotehost:/remote/directory/

This ensures that the copied file retains its original metadata, which can be crucial for scripts or applications relying on timestamps or permissions.


Troubleshooting common issues

Permission errors

While scp is generally quite reliable, you might occasionally run into issues. Permission errors are common, especially when you don’t have write access to the destination directory. Always ensure you have the necessary permissions on both the source and destination systems.

Example: Encountering permission errors with scp

Imagine you’re trying to copy a file named report.pdf to a remote server’s directory, /var/www/html/reports/, where you intend to host this file for web access. However, this directory has restrictive permissions set up to ensure security.

Input:

You attempt the transfer with the following scp command:

scp report.pdf username@remotehost:/var/www/html/reports/

Output:

Instead of a successful transfer message, you’re greeted with an error like this:

scp: /var/www/html/reports/report.pdf: Permission denied

This error message clearly indicates that the user (username) does not have the necessary write permissions in the /var/www/html/reports/ directory on remotehost.

Resolving permission issues:
  • Verify permissions: The first step is to check the permissions of the destination directory. You can do this by logging into the remote host and using the ls -ld /var/www/html/reports/ command, which will show you the permissions of the directory.
  • Adjust permissions: If you have the necessary administrative rights or can request assistance from someone who does, the directory’s permissions can be adjusted to allow your user to write to it. This could involve:
      • Changing the directory’s owner or group to your user (using chown or chgrp) and ensuring the owner or group has write permissions.
      • Adding write permissions for your user specifically, or more broadly for all users (less secure), using the chmod command.
  • Use a different directory: If changing permissions is not feasible due to security or policy reasons, consider using a different directory where your user already has write access.
  • Elevate your permissions: In some cases, especially within controlled environments, you might need to use sudo or similar mechanisms to elevate your permissions for the transfer. This approach should be used cautiously and typically involves transferring the file to a temporary location and then moving it to the final destination with elevated permissions.
Example solution:

After verifying that you don’t have write access, you request the system administrator to grant your user the necessary permissions. The admin decides to add your user to the www-data group, which has write access to /var/www/html/reports/, and then executes:

sudo usermod -a -G www-data username

After logging out and back in to refresh your group membership, you retry the scp command, and this time, the file transfers successfully without any permission errors.

Output:

This time, there’s no error output. Instead, the file report.pdf is successfully transferred to /var/www/html/reports/, ready to be accessed or served as needed.

Network issues

Another common hiccup is network-related issues. If you’re experiencing slow transfer speeds, try the -C option to enable compression, or check your network connection for any underlying problems.

Example: Using compression with scp

Let’s say you have a large log file, app_log.txt, that you need to transfer from your local machine to a remote server. The file is quite large, and you’ve noticed that the transfer speed is slower than expected due to network constraints.

To use compression with scp, you would add the -C option to your command like so:

scp -C app_log.txt username@remotehost:/remote/directory/

Input:

This is your input command in the terminal, where username is your username on the remote host, and remotehost is the address of the remote server.

Output:

With scp, unless you use the -v (verbose) option, you won’t see detailed output about the compression process. Instead, you’ll see the progress of the file transfer, typically displayed as a percentage, file size, transfer speed, and time elapsed.

For example, the output might look something like this (note that the actual output can vary based on your terminal settings and the version of scp):

app_log.txt                            100%   45MB  1.5MB/s   00:30

This output indicates that the file app_log.txt has been successfully transferred, showing the total size (45MB), the transfer speed (1.5MB/s), and the total time taken for the transfer (30 seconds).

Observing the effect of compression:

It’s important to note that the effectiveness of compression (-C) largely depends on the type of data you’re transferring. For instance, compressing already compressed files (like .zip or .jpg) may not yield significant speed improvements and can sometimes even slightly increase the transfer time due to the overhead of compression and decompression. However, for uncompressed text files or certain types of data, compression can significantly reduce the amount of data sent over the network, potentially speeding up the transfer.

In our example with app_log.txt, if this file contains mostly text (like most log files do), compression can be quite effective in reducing the transfer size and, consequently, the time it takes to transfer the file over a slow network.


My experience with this command

What I find most appealing about scp is its blend of simplicity and power. It’s a testament to the Unix philosophy of doing one thing well. While some may argue that scp is showing its age, especially with the advent of more modern file transfer tools, I believe it remains a staple in the Linux command-line toolkit.

In my experience, the most common use cases for scp involve transferring configuration files, backups, or deploying applications to servers. Its integration with SSH makes it particularly handy in secure environments or when working across cloud platforms.

Scp command in Linux cheat sheet

This cheat sheet covers a variety of scp use cases, from basic file transfers to more advanced options for security, efficiency, and troubleshooting. Remember to use the corresponding path at the end of each command:user@remote:/path/

Command Description
scp file.txt Copy a file to a remote directory.
scp file.txt . Copy a file from a remote server to the local directory.
scp -r dir Recursively copy an entire directory to a remote directory.
scp -P 2222 file.txt Copy a file using a specific port (e.g., 2222).
scp -C file.txt Copy a file with compression enabled.
scp -i /path/to/key file.txt Copy a file using a specific SSH private key.
scp -p file.txt Preserve file attributes (modification times, access times) during copy.
scp -v file.txt Verbose mode, which outputs detailed information about the transfer process.
scp -q file.txt Quiet mode, suppresses the progress meter and warning messages.
scp -c aes256-ctr file.txt Specify a specific encryption cipher (e.g., aes256-ctr) for the transfer.
scp -o "StrictHostKeyChecking=no" file.txt Disable strict host key checking, useful in scripts to avoid prompts.
scp file.txt user1@remote1:/path/ user2@remote2:/path/ Copy a file from one remote server to another remote server.
scp -l 1024 file.txt Limit the bandwidth used to 1024 Kbit/s.
scp -4 file.txt Forces scp to use IPv4 addresses only.
scp -6 file.txt Forces scp to use IPv6 addresses only.

Frequently Asked Questions about scp Command

1. What is the difference between scp and rsync?

scp and rsync are both used for transferring files over a network. The main difference is that rsync can synchronize files and directories between two locations with minimal data transfer, as it only sends the differences. scp, on the other hand, copies files without checking for differences, making rsync more efficient for repeated transfers of the same files.

2. Can I use scp to copy files between two remote servers?

Yes, you can use scp to copy files between two remote servers. The command syntax involves specifying both source and destination in the format user@host:path. However, this process involves an intermediary transfer through the local machine, which might not be efficient for large files or slow connections.

3. How do I cancel an ongoing scp transfer?

To cancel an ongoing scp transfer, you can typically use the Ctrl + C keyboard shortcut in the terminal. This sends an interrupt signal to scp, terminating the transfer process.

4. Is scp secure for transferring sensitive files?

Yes, scp is secure for transferring sensitive files as it uses SSH (Secure Shell) for data transfer, which provides encryption and secure authentication. However, ensure your SSH configuration is secure and up to date.

5. Why is my scp transfer so slow?

scp transfer speeds can be affected by various factors, including network bandwidth, server load, and file size. Using the -C option enables compression, which can help with large files or slow networks. Additionally, using a different cipher (e.g., -c aes128-gcm@openssh.com) might increase transfer speed by reducing encryption overhead.

6. How do I increase the verbosity of scp output?

To increase the verbosity of the scp command and see more detailed output, use the -v (verbose) option. This can be helpful for troubleshooting or understanding more about the transfer process.

7. Can I resume an interrupted scp transfer?

Unlike rsync, scp does not natively support resuming interrupted transfers. If a transfer is interrupted, you’ll need to restart it. For resuming capabilities, consider using rsync with the --partial or --append options.

8. How can I specify a different SSH port for scp?

If your remote host uses a different port for SSH, you can specify it in scp using the -P option followed by the port number. For example, scp -P 2222 file.txt user@remote:/path/ uses port 2222 for the SSH connection.

9. Can scp preserve symbolic links during transfer?

By default, scp does not preserve symbolic links; it copies the files they point to. If preserving the structure is crucial, consider using rsync with the -a (archive mode) option, which maintains symbolic links, file permissions, and other attributes.

10. How do I specify which SSH key scp should use?

To specify a particular SSH key for scp, use the -i option followed by the path to your private key file. For example, scp -i ~/.ssh/id_rsa file.txt user@remote:/path/ uses the specified RSA key for authentication.

Conclusion

Mastering the scp command can significantly enhance your productivity and efficiency when working with Linux systems. It’s a powerful tool that embodies the simplicity and versatility of the command line. Whether you’re transferring files to a remote server or between local directories, scp offers a secure and reliable solution.

This exploration has armed you with the knowledge to wield scp in a variety of scenarios, decode error messages, and optimize your file transfers. With this comprehensive guide, from the nuts and bolts of basic commands to the wisdom of a curated FAQ section, you’re now equipped to navigate the Linux landscape more confidently.

You may also like

1 comment

Jalal Hajigholamali February 7, 2024 - 5:22 AM

Hi,
Very useful article
Thanks a lot

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.