CH-J Toolsserver management over SSH
Menu
All guides and articles →

SFTP transfer between two Linux servers without SCP

Copy files between two remote servers through your own computer, handle conflicts and verify the result with SHA-256.

When do you need to relay files through your computer?

You have an archive on source.example and need a copy on target.example. Your workstation can connect to both Linux servers over SSH, but the servers are in separate networks and cannot connect to each other. A transfer relayed through your computer fits this situation: you authenticate to each server locally, read from the source and write to the target.

This guide uses user as the login on both servers, /srv/data/archive.tar.gz as the source and /srv/data/incoming as an existing target directory. Replace these examples with your actual accounts and paths. Start with a copy and keep the source until you have checked the destination. A successful upload alone is not sufficient evidence to retire the original.

There are two relay workflows. A client with two remote panels can stream data between its SFTP connections. The OpenSSH sftp command instead connects to one server per session; the command-line procedure below downloads a local copy and then uploads it. That procedure needs local disk space and leaves a local copy to manage afterwards.

How do direct transfers, SFTP and SCP differ?

With a direct transfer, a program running on one server connects to the other. That requires a usable network route, access to the destination SSH port and an authentication method available on the initiating server. Relaying moves those requirements to your workstation. You do not need to copy your private key to either server or enable agent forwarding for this workflow.

SFTP runs over SSH and is separate from FTP and FTPS. Both servers must provide the SFTP subsystem to the accounts you use. A working interactive SSH shell does not guarantee that SFTP is enabled; an account restricted to SFTP may also be unable to run shell commands.

The scp command and the legacy SCP protocol are different things. OpenSSH 9.0 and later use SFTP by default for scp transfers. If you want a one-command relay, the following is an alternative, but the staged procedure later uses only sftp for transferring files. Check that the target name is unused before running this alternative because an existing file can be overwritten.

scp -3 user@source.example:/srv/data/archive.tar.gz user@target.example:/srv/data/incoming/archive-copy.tar.gz

Do not add -O to force the legacy SCP protocol as a routine fix. First identify whether the failure concerns connectivity, authentication, SFTP availability or a particular path.

How do you diagnose connection failures before copying?

From your workstation, test each server independently. Open one SFTP session, inspect its starting directory with pwd, then leave with bye before testing the other. Verify each host fingerprint against a trusted console or an administrator through another channel before accepting an unfamiliar key.

sftp user@source.example
sftp user@target.example

A timeout points towards routing, a firewall or the wrong address or port. Connection refused means the connection was rejected, often because nothing is listening there. Permission denied during login concerns authentication or account policy; the same message after get or put usually concerns filesystem access. A subsystem request failure calls for checking the server SFTP configuration.

Use verbose mode on the failing connection to locate the stage at which it stops. For an SSH service on port 2222, use capital -P with sftp. The example below combines both options. Do not publish verbose logs without checking them for account names, internal addresses and local paths.

sftp -v -P 2222 user@target.example

Do not bypass host key verification to make a transfer proceed. An unexplained changed host key can mean that you are about to send data or credentials to a different machine.

What should you check about paths, permissions and space?

The transfer account needs permission to read the source, traverse its parent directories and create files in the destination directory. For the shell checks below, both servers are Linux systems with the usual GNU utilities and the accounts allow SSH command execution. Run these commands from your workstation; they inspect the servers without changing files.

ssh user@source.example 'id; ls -l /srv/data/archive.tar.gz'
ssh user@target.example 'id; ls -ld /srv/data/incoming; df -h /srv/data/incoming; df -i /srv/data/incoming'

Check the filesystem that actually contains the target directory. Leave room for the incoming copy and any old version you retain. Available space reported by df does not account for every user quota or storage policy. With many small files, exhausted inodes can also prevent new files even when capacity remains.

For an SFTP-only account, use pwd and ls inside the session and ask the administrator about capacity and quotas. A chroot can make the SFTP path differ from the path visible in a shell. Do not guess a path by prepending the chroot directory. If access fails, request a narrowly scoped staging directory or an approved export rather than making application directories writable to everyone.

How do you copy with sftp step by step?

First arrange a stable source: finish generating the archive and stop anything that would rewrite it during the copy and verification. Use an application-supported backup for a live database. A file copied while its contents change may not represent a usable backup even if the transfer completes. Before downloading, record the source SHA-256 using the command in the verification section.

  1. Prepare a private local staging directory with enough free space for the archive. Keep the same terminal working directory for the download and upload.
  2. Download the source under an explicit local name. Check the SFTP messages and stop if get reports an error.
  3. Connect to the target, inspect the destination and choose an unused upload name. Keep application processes from modifying files in this staging location.
  4. Upload the completed local file. Leave the source and any previous destination version in place until verification and application checks succeed.

On a Linux workstation, this creates a private temporary directory and starts the source session only if directory creation and the directory change succeed. For sensitive archives, choose protected local storage appropriate to your policy. On Windows with the OpenSSH client available, open your terminal in a newly created private staging directory and run sftp user@source.example there instead of this Linux shell command.

transfer_stage=$(mktemp -d) && cd "$transfer_stage" && sftp user@source.example

At the source SFTP prompt, run these commands. lpwd confirms the local directory. The commands following it retrieve a single archive into that directory and then close the connection.

lpwd
get /srv/data/archive.tar.gz archive.tar.gz
bye

Back in the same local terminal, connect to the destination. If the download failed, resolve that failure before starting an upload.

sftp user@target.example

At the target SFTP prompt, list the directory first. Continue with put only if archive-copy.tar.gz is unused and the directory is the intended staging location. Otherwise select a different unused name. These interactive lines are separate decisions, not a script to paste without reviewing the listing.

ls -l /srv/data/incoming
put archive.tar.gz /srv/data/incoming/archive-copy.tar.gz
bye

OpenSSH sftp put can overwrite an existing destination file. A prior listing is not a lock against another writer. Use a dedicated staging directory with controlled access and do not upload directly over a live configuration, database or only backup.

How do you verify the copy before using it?

Calculate the source checksum before the download and record it with the transfer record. After upload, calculate both checksums again. These commands run sha256sum on the Linux servers, so they also work from a Windows workstation with ssh available.

ssh user@source.example sha256sum /srv/data/archive.tar.gz
ssh user@target.example sha256sum /srv/data/incoming/archive-copy.tar.gz

Compare the entire 64-character hexadecimal digest, not the filenames, which intentionally differ. The final source digest must still match the recorded original, and the destination must match it too. If any value differs or hashing fails, keep the source and investigate. Matching hashes establish matching file contents for the stable versions checked; they do not establish that the archive contains the right business data.

You can also hash the staged local file to locate whether a mismatch arose on the download or upload leg. On a Linux workstation, run the following from the staging directory. On Windows PowerShell, use Get-FileHash -Algorithm SHA256 -LiteralPath .\archive.tar.gz instead. Letter case in the displayed hexadecimal digest does not change its value.

sha256sum archive.tar.gz

Finally, check archive readability with the appropriate archive tool and test restoration or application use in the intended environment. Confirm the required permissions separately. Hash equality does not check ownership, permissions or whether an application can open the result.

What should you do after interruptions or filename conflicts?

Treat a disconnected upload as incomplete even if a file with the expected name exists. Reconnect, inspect that exact file and decide whether to retry under another name or resume. An ordinary retry with put can overwrite the partial destination from the beginning.

  • Overwrite only after confirming the existing file is expendable and a recoverable previous copy exists if needed. A matching filename does not establish matching contents.
  • Keep both versions by using a distinct transfer name when you need review or rollback. Publish the verified copy separately under your normal deployment procedure.
  • Skip a file only after deciding the target already contains the required version. File size and modification time alone are weaker evidence than a matching checksum.
  • OpenSSH sftp provides reget and reput for resuming downloads and uploads. Resume only when the partial file is an unchanged prefix of the same source, then verify the completed file again.

Resuming against a changed source or an unrelated partial file can produce corrupt contents. If you cannot establish that they belong together, transfer a fresh copy under a new name. Do not delete the source to recover space while the destination is still unverified.

How do you make future transfers safer and predictable?

Write down the endpoints, account names, source path, destination path, expected checksum and conflict decision before a large transfer. Keep a record of completion and validation. For a directory tree, record the file inventory too; one checksum for one archive does not verify extra files copied alongside it.

  • Keep the workstation awake and connected. A relay uses its connection to both servers; a staged download and upload take place in sequence and both consume local network capacity.
  • Check metadata requirements before choosing the tool. A normal-user copy does not preserve arbitrary original ownership, and a basic SFTP copy is not a full-fidelity filesystem migration with every ACL, extended attribute and hard link.
  • Test representative files before copying a tree, including hidden files and symbolic links. Do not assume recursive options and link handling are identical across clients.
  • Agree on retention and cleanup. Keep the original through acceptance, then remove only the explicitly approved files. Manage the local staging copy as another copy of the same potentially sensitive data.

If the source requires sudo to read, ordinary SFTP does not become privileged because your account can use sudo in a terminal. Have an administrator produce an authorized, readable export with suitable permissions. Avoid broad permission changes and do not place private authentication keys on a server just to bypass a transfer problem.

How does CH-J Tools help with transfers between servers?

In Alpha 0.1.1 the File Manager has one to three panels. Each panel shows either the local disk or any open SSH session. You can copy or move files between panels using the keyboard, dragging or the context menu.

  • Data streams from one SFTP session to the other through your computer without being stored on its disk.
  • Transfers use a queue with pause, resume, cancel and conflict handling.
  • When moving, the source file is deleted only after its SHA-256 matched and only if it has not changed.

The transfer is relayed, so speed depends on your connection to both servers and your computer must remain connected. Files readable only through sudo cannot be copied between servers this way. Beta 0.1.0 does not include the multi-panel File Manager.

Frequently asked questions

Can I transfer between servers that cannot reach each other?

Yes, if your workstation can reach and authenticate to both. A relay client passes the data through it; separate sftp download and upload sessions additionally require local staging space.

Does SFTP require an interactive shell account?

No. An account restricted to the SFTP subsystem can transfer permitted files without an interactive shell. The remote sha256sum commands in this guide require separate permission to execute SSH commands or help from an administrator.

Does a jump host copy files between two servers?

A jump host provides a route to an endpoint. It does not itself select source files or coordinate two file-transfer sessions. You still need a client workflow for the copy and independent destination verification.

Does scp -3 use the old SCP protocol?

The -3 option selects a relay through the local host. Protocol selection is separate: OpenSSH 9.0 and later use SFTP by default, while -O requests the legacy SCP protocol.

Can I delete the original as soon as SHA-256 matches?

Only after confirming the source stayed stable and the copied data is usable for its intended purpose. Follow your retention policy and keep a rollback copy where required; checksums do not replace a restore test.

Detailed documentation

OpenSSH: sftp →

OpenSSH: scp (-3) →

Linux manual: sha256sum →

Biometric unlock and security →

All tutorials by category →