Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

DCP Copy Files Between Linux Servers

· 6 min read · Home Tech

In this article
  1. What Is dcp and How Does It Differ from scp?
  2. How dcp Works Under the Hood
  3. When to Use dcp vs. scp vs. rsync
  4. Installing dcp on Linux
  5. Prerequisites and Authentication
  6. Common Issues and Troubleshooting
  7. Alternatives to dcp
  8. Security Considerations
  9. Frequently Asked Questions

Yes, you can copy files between Linux servers with dcp, a distributed file copy tool that transfers data directly between two remote hosts without routing it through your local machine. This guide explains how dcp works, when to use it instead of scp or rsync, and how to set it up securely.

What Is dcp and How Does It Differ from scp?

dcp (distributed copy) is a command-line utility that copies files from one remote server to another using SSH. Unlike scp, which copies from your local machine to a remote host (or vice versa), dcp connects directly to both servers and streams the data between them. This means the file never touches your local disk or network connection, which can be much faster when the two servers are on the same high-speed network and your local connection is slow.

For example, to copy a file from server A to server B, you would run:

dcp user1@serverA:/path/to/file user2@serverB:/destination/

This command initiates an SSH connection to both servers and transfers the file directly. Your local machine only sends the command and receives status updates.

How dcp Works Under the Hood

dcp uses SSH to authenticate to both remote hosts and then establishes a data channel between them. The exact mechanism depends on the implementation:

  • Using SSH port forwarding: The local dcp process opens an SSH connection to server A and another to server B, then forwards a local port on server A to a port on server B. The file is streamed through these forwarded connections.
  • Using SSH's direct-tcpip channel: Some versions of dcp use SSH's built-in direct-tcpip forwarding to create a direct TCP connection between the two servers, bypassing the local machine entirely.

In both cases, the data is encrypted in transit because it travels over SSH connections. The local machine does not store the file contents; it only relays the data stream, so memory usage is low even for large files.

When to Use dcp vs. scp vs. rsync

Choose dcp when you need to copy files directly between two remote servers and want to avoid the bottleneck of your local connection. It is ideal for:

  • Migrating data between two cloud servers in the same data center.
  • Copying large backups from one server to another without downloading and re-uploading.
  • Transferring files between servers when your local internet is slow or metered.

However, scp is simpler for local-to-remote or remote-to-local copies, and rsync offers incremental transfers, compression, and better resume support. If you need to keep the two servers in sync, rsync is usually a better choice because it can compare file hashes and only transfer differences.

Installing dcp on Linux

dcp is not included in most default Linux installations. You can install it from your distribution's package manager:

  • Debian/Ubuntu: sudo apt install dcp
  • RHEL/CentOS: sudo yum install dcp
  • Arch Linux: sudo pacman -S dcp

If dcp is not available in your repositories, you can compile it from source. The source code is available on GitHub, and the build process typically requires only a C compiler and OpenSSL development headers. After compiling, place the binary in your PATH (e.g., /usr/local/bin).

Prerequisites and Authentication

Before using dcp, ensure that:

  • Both servers have SSH running and are reachable from your local machine.
  • You have valid login credentials (password or SSH key) for both servers.
  • The destination directory on the target server exists and is writable by the target user.

dcp uses SSH for authentication, so you can use SSH keys to avoid typing passwords. Set up key-based authentication by adding your public key to ~/.ssh/authorized_keys on both servers. If you use an SSH agent, dcp will automatically use it for authentication.

Common Issues and Troubleshooting

Here are typical problems you might encounter and how to resolve them:

  • Permission denied: Ensure the user on the destination server has write permission to the target directory. Use chmod or chown to fix permissions.
  • Connection timeout: Check that both servers are reachable from your local machine and that SSH is listening on port 22 (or the configured port). Use ssh -v to debug connectivity.
  • File not found: Verify the source path is correct and that the user has read permission on the source file.
  • dcp command not found: If you compiled from source, ensure the binary is in your PATH or use the full path to the executable.

If you need to copy directories recursively, check if your dcp version supports the -r flag. Some builds do not, in which case you should use rsync or tar over SSH instead.

Alternatives to dcp

Several other tools can copy files between remote servers without local involvement:

  • rsync with SSH: Run rsync -avz -e ssh user1@serverA:/path user2@serverB:/path from your local machine. This uses SSH to connect to both servers and transfers data directly between them, with the added benefit of compression and delta transfers.
  • scp with two remote hosts: Some scp versions allow copying between two remote hosts directly: scp user1@serverA:/file user2@serverB:/dest. However, this is not supported by all implementations and may fall back to routing through your local machine.
  • FTP/SFTP: You can use an SFTP client that supports server-to-server transfers, but this often requires a GUI and is less scriptable.

For most users, rsync is the most robust and feature-rich option. dcp is a lightweight alternative when you want a simple, direct copy without the extra features of rsync.

Security Considerations

Because dcp uses SSH, the data is encrypted during transfer, which protects it from eavesdropping. However, be aware of the following:

  • Your local machine sends authentication credentials to both servers, so ensure your SSH keys are protected and your local machine is secure.
  • If you use password authentication, the passwords are sent over SSH, which is safe, but consider using keys for automation.
  • Do not use dcp over untrusted networks without additional security measures, as SSH is generally safe but you should still verify server fingerprints.

Always verify the integrity of transferred files, especially if they are critical. You can compare checksums on both sides after the transfer.

Frequently Asked Questions

Can dcp copy directories recursively?

Some versions of dcp support the -r flag for recursive copying, but it is not guaranteed. Check your dcp version's help (dcp --help) to see available options. If recursive copying is not supported, use rsync or tar over SSH instead.

Does dcp work with SSH keys?

Yes, dcp uses SSH for authentication, so it works with SSH keys. Ensure your public key is added to the ~/.ssh/authorized_keys file on both servers, and your private key is available on your local machine (e.g., in ~/.ssh/id_rsa or via an SSH agent).

Is dcp faster than scp for server-to-server transfers?

Yes, dcp is typically faster than scp for server-to-server transfers because the data does not pass through your local machine. This avoids the bottleneck of your local network and disk I/O. However, if your local connection is fast and the servers are far apart, the difference may be negligible.

Can I use dcp to copy files between servers on different networks?

Yes, as long as both servers are reachable from your local machine and SSH ports are open. dcp will establish connections to both servers and transfer data between them, regardless of their network locations.

What happens if the transfer is interrupted?

dcp does not have built-in resume support. If the transfer is interrupted, you need to restart it from the beginning. For large files, consider using rsync with the --partial option, which can resume interrupted transfers.