Transfer Files Between Two Linux Machines
In this article
- Why SSH-Based Transfer Is the Default
- Prerequisites: What You Need Before Transferring
- Using scp for Simple, One-Time Copies
- Using rsync for Large or Repeated Transfers
- Setting Up SSH Keys for Passwordless Transfers
- Alternatives: sftp, Netcat, and USB Drives
- Common Problems and How to Fix Them
- Frequently Asked Questions
The quickest way to transfer files between two Linux machines is to use `scp` (secure copy) for a one-off file or folder, or `rsync` for larger or repeated transfers. Both use SSH, so they are encrypted and require no extra setup if SSH is already running on the target machine.
Why SSH-Based Transfer Is the Default
Linux systems have SSH (Secure Shell) built into most distributions, and it is the backbone of remote administration. Because SSH encrypts the entire session, any file transfer tool that runs over it—`scp`, `rsync`, `sftp`—inherits that encryption. This matters because unencrypted protocols like FTP send your password and data in plain text, which is readable by anyone on the network. For most users, there is no reason to set up a separate FTP server when SSH is already present and configured.
Another advantage of SSH-based tools is that they use the same authentication as your normal login. If you can log into a machine remotely, you can almost certainly copy files to it. No additional daemons, ports, or firewall rules are needed beyond what SSH already uses (port 22 by default).
Prerequisites: What You Need Before Transferring
Before you run any command, confirm the following:
- SSH is installed and running on the destination machine. Most server distributions enable it by default; desktop distros often do not. Check with
systemctl status sshorsudo systemctl status sshd(the service name varies). If it is not running, start it withsudo systemctl start sshand enable it withsudo systemctl enable ssh. - You know the IP address or hostname of the target machine. Find it with
ip addrorhostname -Ion the target. - You have login credentials (username and password, or an SSH key) for the target machine.
- Firewall rules allow SSH on the target. If you use `ufw`, run
sudo ufw allow ssh. For `firewalld`, usesudo firewall-cmd --add-service=ssh --permanentand reload.
If you are transferring between two machines on the same local network, the IP address will be something like 192.168.1.10. For transfers over the internet, you will need the public IP and proper port forwarding—but that is riskier and beyond typical use.
Using scp for Simple, One-Time Copies
scp (secure copy) is the simplest tool for copying a single file or a directory. Its syntax mirrors the `cp` command but adds a remote host specification.
To copy a file from your current machine to a remote one:
scp /home/user/document.pdf username@192.168.1.10:/home/username/To copy a file from a remote machine to your local one, reverse the order:
scp username@192.168.1.10:/home/username/document.pdf /home/user/To copy an entire directory, add the -r (recursive) flag:
scp -r /home/user/project/ username@192.168.1.10:/home/username/If you use a non-standard SSH port (not 22), specify it with -P (capital P, unlike `ssh` which uses lowercase -p):
scp -P 2222 file.txt user@host:/path/You will be prompted for the password unless you have set up SSH keys. For a few files, scp is perfectly adequate. However, it does not resume interrupted transfers, and it does not compare files to skip duplicates—which is where rsync shines.
Using rsync for Large or Repeated Transfers
rsync is a more powerful tool that synchronizes files between two locations. It checks which files have changed and only transfers the differences, making it ideal for large datasets or backups. It also supports resuming interrupted transfers and preserving permissions, timestamps, and symbolic links.
The basic syntax is similar to scp:
rsync -av /home/user/project/ username@192.168.1.10:/home/username/project/Here, the -a flag (archive) preserves permissions, timestamps, and other metadata, and -v gives verbose output. Add -z to compress data during transfer, which speeds things up on slow connections:
rsync -avz /home/user/project/ user@host:/home/username/project/To copy from a remote machine to your local one, swap the source and destination:
rsync -av user@host:/home/username/project/ /home/user/project/One critical detail: a trailing slash on the source directory matters. rsync -av /source/ /dest/ copies the contents of /source into /dest. Without the trailing slash, it copies the directory itself, creating /dest/source. This is a common source of confusion.
For a first transfer, you might run rsync with --dry-run to see what it would do without actually copying:
rsync -av --dry-run /source/ user@host:/dest/If the transfer is interrupted, simply run the same command again—rsync will resume where it left off, skipping files that are already complete.
Setting Up SSH Keys for Passwordless Transfers
Typing a password every time becomes tedious, especially for automated or scheduled transfers. SSH keys solve this by using a public/private key pair. The private key stays on your local machine; the public key is added to the target machine's ~/.ssh/authorized_keys file.
Generate a key pair (if you do not already have one) with:
ssh-keygen -t ed25519This creates ~/.ssh/id_ed25519 (private) and ~/.ssh/id_ed25519.pub (public). Then copy the public key to the remote machine using ssh-copy-id:
ssh-copy-id username@192.168.1.10You will be prompted for the password once. After that, scp and rsync will connect without asking for a password. This also makes it possible to run transfers in cron jobs or scripts.
Keep your private key safe—anyone who obtains it can log into any machine that has the corresponding public key.
Alternatives: sftp, Netcat, and USB Drives
While scp and rsync cover most needs, there are other methods worth knowing:
- sftp is an interactive file transfer program that works over SSH. It is useful if you want to browse the remote filesystem and transfer files manually. Start it with
sftp user@hostand use commands likeput,get,ls, andcd. - Netcat (nc) is a raw TCP/UDP tool that can pipe data directly between two machines. It is fast and simple, but it is not encrypted and has no error checking. Example: on the receiving machine, run
nc -l -p 1234 > file.tar; on the sending machine, runnc 192.168.1.10 1234 < file.tar. This is handy for quick transfers on a trusted network, but you should not use it over the internet. - USB drive or external disk is the old-fashioned method, but it is still the best choice when the machines are not on the same network or when you need to move hundreds of gigabytes. It is also useful when SSH is not available due to firewall restrictions.
Each method has trade-offs between speed, security, and convenience. For most users, rsync over SSH is the sweet spot.
Common Problems and How to Fix Them
Even with a straightforward setup, things can go wrong. Here are the most frequent issues:
- Connection refused — This usually means SSH is not running on the target, or the port is blocked. Check the SSH service status and firewall rules.
- Permission denied — The username or password is wrong, or the account is not allowed to log in via SSH. On some systems, root login is disabled by default; use a regular user account.
- Host key verification failed — This happens when the remote machine's key has changed (e.g., after a reinstall). Remove the old key from your
~/.ssh/known_hostsfile withssh-keygen -R hostnameand try again. - rsync: connection unexpectedly closed — Often caused by a full disk on the target, or by a network issue. Check disk space with
df -hon the target. - Transfer is very slow — If you are on a high-latency link, try adding
-zfor compression. Also, make sure you are not transferring over a VPN that adds overhead.
If you are behind a NAT and trying to transfer from outside your local network, you will need to set up port forwarding on your router—but that exposes SSH to the internet, so it is safer to use a VPN or a tool like Tailscale (which is not covered here).
Frequently Asked Questions
Can I use scp to copy files between two remote machines (not my local one)? Yes, but you need to run the command from your local machine and specify both remote hosts: scp user1@host1:/file user2@host2:/dest. This will copy the file from host1 to host2, but the data passes through your local machine, so it is not a direct transfer. For direct server-to-server transfer, you would need to log into one of them and use scp from there.
What is the difference between scp and rsync? scp copies everything you specify, without checking if the file already exists or has changed. rsync compares files and only transfers the differences, which saves bandwidth and time on repeated transfers. rsync also preserves metadata better and can resume interrupted transfers.
Is it safe to transfer files over SSH on a public network? Yes, SSH encrypts all data, so your files and password are protected from eavesdropping. However, you should still use strong passwords or SSH keys, and avoid using default ports if you are exposing SSH to the internet.
How do I transfer a file that is larger than the available disk space on the target? You cannot—the target needs enough free space to hold the file. If you are running out of space, consider cleaning up the target or using a streaming method like netcat that does not store the file, but that is rarely practical.
Can I transfer files between Linux and Windows using these commands? Not directly. scp and rsync are Unix tools. On Windows, you can use pscp (from PuTTY) or install WSL (Windows Subsystem for Linux) to get native scp and rsync. Alternatively, use an SFTP client like FileZilla.
What if I need to transfer files on a schedule? Use rsync in a cron job. For example, to back up a directory every night at 2 AM, add this to your crontab: 0 2 * * * rsync -av /source/ user@host:/dest/. Make sure SSH keys are set up so it runs without a password prompt.