Okay, this is a classic "paranoid sysadmin" scenario, and a valid one! You want to avoid typing Host B's password (or using a sensitive private key) on Host A if A might be compromised, and vice-versa. The core idea is to initiate the transfer from the "more trusted" host at the moment of transfer, or use an intermediary that doesn't require direct authentication between A and B for the data stream.
Here are several methods, ranging in complexity and features:
Overarching Principle:
If you initiate from Host A (pushing to B), you need B's credentials on A.
If you initiate from Host B (pulling from A), you need A's credentials on B.
Your constraint is "do not want to type ssh password of host B on A." This means you generally want to initiate the copy from Host B (pulling files from A), or use a method that doesn't involve direct SSH login for the data transfer itself.
Method 1: rsync initiated from Host B (Pull Method) with SSH Keys & Restricted Command
This is generally the most robust and recommended method if set up carefully.
-
On Host B (Destination):
-
On Host A (Source):
- Copy the public key (
~/.ssh/hostA_pull_key.pub from Host B) into the ~/.ssh/authorized_keys file of the user on Host A that owns the files (or root's authorized_keys if copying as root).
- Crucially, restrict what this key can do. This is vital if Host B gets compromised.
In
/root/.ssh/authorized_keys (or relevant user's file) on Host A, prefix the key with a command restriction:
command="rsync --server --sender -vlogDtprRe.iLsfx --acls --xattrs . /path/to/source_directory_on_A",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-ed25519 AAAAC3NzaC1lZDI1NTE5A... user@hostB
/path/to/source_directory_on_A: The exact absolute path on Host A you want to make available.
- The
. after --xattrs tells the rsync server to serve files relative to the path specified in the command string.
no-port-forwarding, etc., are additional security hardening options.
- The
rsync --server --sender options are what rsync uses internally when it's on the "server" side of a connection.
-vlogDtprRe.iLsfx are common rsync flags: verbose, log, preserve devices, times, perms, recurse, links, symlinks, owner, group, special files, etc. --acls for POSIX ACLs, --xattrs for extended attributes.
-
On Host B (Destination):
Security Implications of Method 1:
- If Host A is compromised: The attacker cannot get Host B's credentials from A. They could tamper with the source files before B pulls them.
- If Host B is compromised: The attacker gains access to the private key (
hostA_pull_key). However, because of the command restriction on Host A, this key can only be used to rsync data from the specified directory on A. They cannot execute arbitrary commands on A.
Method 2: tar over socat (with SSL/TLS for encryption)
This avoids SSH for the data transfer itself but requires setting up socat and SSL certificates. It transfers a tar stream.
-
Generate Self-Signed Certs (or use real ones):
You'll need a CA, and certs for A and B signed by that CA, or use self-signed certs and explicitly trust them. For simplicity, let's assume self-signed for now.
On Host B (acting as server):
openssl genrsa -out b.key 2048
openssl req -new -key b.key -out b.csr
openssl x509 -req -days 365 -in b.csr -signkey b.key -out b.crt
On Host A (acting as client):
openssl genrsa -out a.key 2048
openssl req -new -key a.key -out a.csr
openssl x509 -req -days 365 -in a.csr -signkey a.key -out a.crt
You'll need to copy a.crt to B and b.crt to A.
-
On Host B (Destination - Receiver):
- Open a port and listen for the tar stream, then extract.
# As root on B
sudo socat OPENSSL-LISTEN:12345,fork,reuseaddr,cert=b.crt,key=b.key,cafile=a.crt \
SYSTEM:'tar -C /path/to/destination_directory_on_B/ --numeric-owner -xJpf -'
OPENSSL-LISTEN:12345: Listen on port 12345 with SSL.
fork,reuseaddr: Standard socat options for servers.
cert=b.crt,key=b.key: B's SSL certificate and private key.
cafile=a.crt: Trust A's certificate.
SYSTEM:'tar ...': Pipe the received data to tar.
tar -C /destination/path --numeric-owner -xJpf -:
-C /path/to/destination_directory_on_B/: Change to destination directory.
--numeric-owner: Crucial! Preserves UID/GID numerically, not by name (which might differ between hosts).
-x: extract.
-J: handle .xz compression (use -z for gzip, or omit if no compression on sender).
-p: preserve permissions (includes ACLs and xattrs if tar supports them well, GNU tar does with --acls --xattrs but -p often suffices for basic POSIX perms).
-f -: read from stdin.
- You might need to add
--acls --xattrs to the tar command on both sides explicitly if -p isn't enough for your tar version or needs.
-
On Host A (Source - Sender):
- Create a tar stream and send it to Host B.
Security Implications of Method 2:
- Data is encrypted in transit.
- No SSH credentials exchanged for the data part.
- If Host A is compromised: They could send malicious data. Certs could be stolen (but are usually less privileged than SSH access keys).
- If Host B is compromised: They could manipulate received data. Certs could be stolen.
- Requires careful certificate management.
Method 3: tar over netcat (or ncat) with external encryption (e.g., gpg)
Simpler than socat if you don't want to manage SSL certs, but encryption setup is manual.
-
On Host B (Destination - Receiver):
# As root on B
sudo nc -l -p 12345 | gpg -d | tar -C /path/to/destination_directory_on_B/ --numeric-owner -xpf -
(You'll be prompted for the GPG passphrase used on Host A). Add compression flags to tar if used on A.
-
On Host A (Source - Sender):
# As root on A
sudo tar --numeric-owner --acls --xattrs -cpf - /path/to/source_directory_on_A | \
gpg -c --cipher-algo AES256 | \
nc hostB_ip_or_hostname 12345
(You'll be prompted to set a GPG passphrase. Use the same one on B).
Security Implications of Method 3:
- Like Method 2 for
tar part.
- Relies on GPG for encryption. The passphrase needs to be securely communicated or pre-shared.
netcat itself is unauthenticated. Anyone who can guess the port and timing could try to connect (though GPG would prevent them from decrypting).
Method 4: Magic Wormhole (for smaller, less frequent transfers)
magic-wormhole provides end-to-end encrypted transfer using a rendezvous server. It's very user-friendly.
-
Install magic-wormhole on both hosts:
pip install magic-wormhole
-
On Host A (Source - Sender):
# As root on A
sudo tar --numeric-owner --acls --xattrs -cpf - /path/to/source_directory_on_A | wormhole send --hide-progress -
This will output a wormhole code like 7-ambush-cornfield.
-
On Host B (Destination - Receiver):
# As root on B
# Enter the code from Host A when prompted
wormhole receive --accept-file --output-file=- | sudo tar -C /path/to/destination_directory_on_B/ --numeric-owner -xpf -
Security Implications of Method 4:
- End-to-end encrypted.
- Relies on a public (by default) rendezvous server, though data doesn't transit through it.
- The "wormhole code" is effectively a one-time password. If an attacker is on Host A when you generate it, or on Host B when you type it, they could potentially intercept. Best if you have a secure channel to transmit the code.
- Good for ad-hoc transfers, may struggle with very large (multi-GB) directories due to overhead.
Method 5: USB Drive / External Storage (The Air-Gap Method)
If physical access is possible and security is paramount.
-
On Host A (Source):
-
Physically move the drive to Host B.
-
On Host B (Destination):
Security Implications of Method 5:
- No network exposure during transfer.
- If the USB drive is encrypted, data is safe even if lost/stolen.
- Risk of malware transfer if one host is compromised and autorun features are enabled (disable them!). Treat the USB as potentially untrusted.
- Requires physical access.
Key Considerations for All Methods:
- Root Access: Most of these methods will require
sudo or running as root on both ends if you need to preserve ownership of files owned by various users (especially root) and maintain permissions correctly in system directories. The --numeric-owner flag for tar is essential when UIDs/GIDs might not align between hosts. rsync handles this better by default with -o and -g.
- Permissions, ACLs, Symlinks, Xattrs:
rsync -aAXH: Handles all of these very well.
tar (GNU): Use --acls --xattrs --hard-dereference (or let it handle symlinks as symlinks) and -p. --numeric-owner is key.
- Firewalls: Methods involving
netcat or socat will require opening ports on the listening host's firewall. magic-wormhole typically works through firewalls due to its relay mechanism.
- Testing: Always test with a non-critical subset of data first to ensure permissions, ownership, ACLs, and symlinks are handled as expected.
Recommendation:
For your specific needs ("correctly handles permissions, posix ACLs, symlinks, can work as root"), Method 1 (rsync pull from B with restricted SSH key on A) is generally the most robust, feature-rich, and secure if set up correctly. It's designed for exactly this kind of synchronization and minimizes the attack surface.
If setting up restricted SSH keys is too complex or you prefer a non-SSH approach for the data stream, Method 2 (tar over socat with SSL) is a strong contender, followed by Method 3 (tar over nc+gpg). Magic Wormhole is excellent for convenience if the other methods are overkill for the size/frequency.