SFTP: Pull Files from a Remote Host Without Directly Knowing
In some cases, you may need to pull files from a remote host using the Secure File Transfer Protocol (SFTP), but you don't have direct access to the remote host. This article will cover the key concepts and steps to achieve this, specifically for two Debian hosts, one of which is located within a DMZ in a secure environment.
Prerequisites
- Two Debian hosts
- One host located within a DMZ
- SSH access to the DMZ host
Key Concepts
The main concept behind this process is to use the ssh-keygen and ssh-copy-id commands to create and copy SSH keys between the two Debian hosts. Once the keys are copied, you can use the scp or sftp commands to securely transfer files between the hosts.
Step 1: Generate SSH Keys
On the host that will be pulling the files (let's call it Host A), generate a new SSH key pair using the following command:
$ ssh-keygen -t rsa
This will generate a new RSA key pair. You can press Enter to accept the default file locations and passphrase.
Step 2: Copy SSH Keys to the DMZ Host
Next, copy the public key generated in the previous step to the DMZ host (Host B) using the following command:
$ ssh-copy-id -i ~/.ssh/id\_rsa.pub user@hostb
Replace user@hostb with the appropriate username and hostname for Host B.
Step 3: Test SSH Access
Test SSH access to Host B from Host A to ensure the keys have been copied correctly:
$ ssh user@hostb
If the keys have been copied correctly, you should be able to log in to Host B without entering a password.
Step 4: Transfer Files Using SFTP
Now that SSH access has been established, you can use the sftp command to transfer files between Host A and Host B:
$ sftp user@hostb
sftp> cd /path/to/remote/directory
sftp> get /path/to/local/file
sftp> exit
In this article, we covered the key concepts and steps to pull files from a remote host using SFTP, specifically for two Debian hosts, one of which is located within a DMZ in a secure environment. By using the ssh-keygen and ssh-copy-id commands to create and copy SSH keys between the hosts, you can securely transfer files using the scp or sftp commands.