Hopping Through a Host with No Keys, No sshpass, and Hung Nested SSH
Remote work with a single entry point forces everything through an intermediate host, which is often a machine I would rather not install anything else on. What finally worked was not nesting ssh more deeply, but having the intermediate host open two pipes and connect them.
1-minute summary
- Symptom / problem
- The VPN reaches only one machine, the middle host has no keys and no sshpass, and nested ssh hangs during file transfer
- Who should read
- People moving large files through a jump host where they do not want to install anything
- Fix in short
- Use an askpass wrapper in tmpfs and have the middle host pipe two single-hop connections together instead of nesting ssh, as in the 122 GB transfer
I took on a job managing a set of systems inside a customer's closed network. The only way in from outside was a VPN that reached a single intermediate host. The machines I actually needed to work on were another hop behind it. Along the way, I failed four times, from sshpass failing to enter the password and file transfers hitting Argument list too long to nested ssh hanging indefinitely when I needed to move a 122 GB file between hosts. I wrote down the whole sequence because each attempt taught me something different.
I have replaced all hostnames, paths, and values that could identify the systems with made-up ones. This network has security monitoring that blocks repeated incorrect password attempts, so every step in this article assumes no guessing is allowed, and all the work was on machines I was assigned to manage.
The problem: one entry point, with the target hosts another hop behind it
There are four machines involved.
| Name | Role | Accessible from |
|---|---|---|
| dev machine | My Windows machine, off-site | - |
| host-j | Intermediate Linux host running a reporting web app | VPN from the dev machine |
| host-a | Database replica being rebuilt | SSH from host-j |
| host-b | Backup storage host with a large disk | SSH from host-j |
There is also a master and another replica with only their database ports open. Site policy prohibits SSH access to them, so those two machines are outside the scope of this article.
The constraints that shaped every decision were that host-j had no keys for the other machines, had no sshpass installed, and I did not want to install extra packages on a customer's machine for a one-off job.
First attempt: sshpass fails to enter the password
On my very first attempt, I used OpenSSH on Windows with sshpass and got Permission denied even though the password was correct. I suspected the password first, which was a dangerous assumption at a site like this: just a few more retries would get me blocked.
The cause was that the target sshd offered keyboard-interactive authentication, which the version of sshpass I had on Windows could not handle. I could check this without sending a password at all.
# ดูว่าเซิร์ฟเวอร์เสนอวิธียืนยันตัวอะไรบ้าง โดยไม่ลองรหัสสักครั้ง
ssh -v -o PreferredAuthentications=none -o BatchMode=yes root@host-j 2>&1 | grep 'can continue'
# debug1: Authentications that can continue: publickey,keyboard-interactive
PuTTY's plink -batch worked because it could answer keyboard-interactive prompts itself. From that point on, I switched to plink on Windows.
On a network that blocks repeated password attempts, do not troubleshoot this by retrying. First check which authentication methods the server offers and whether the tool supports them. If an attempt is necessary, limit it to one with -o NumberOfPasswordPrompts=1
Choosing the wrong ssh binary on Windows is another problem I wrote about separately in An automated job stopped sending images for 4 days because subprocess picked ssh from PATH.
Second attempt: transferring files through a pivot hits Argument list too long
Initially, my access reached host-b and host-a first, and I was able to install dedicated ed25519 keys for the job on both machines. Deploying code to host-j therefore had to go through host-b.
My first approach was straightforward: base64-encode the file locally and paste it into an argument to the remote command. Small files worked, but once a file exceeded roughly 1 MB, I got Argument list too long. The kernel limits both the total size of arguments and the size of each individual argument. There is no need to memorize the numbers; just remember that file contents should not travel as arguments.
# ผิด: เนื้อไฟล์กลายเป็น argument ของคำสั่งระยะไกล
B64=$(tar -cz app/ | base64 -w0)
ssh -i KEY root@host-b "ssh root@host-j 'echo $B64 | base64 -d | tar -xz -C /var/www/app'"
# ถูก: เนื้อไฟล์ไหลทาง stdin ตลอดทาง ขนาดเท่าไรก็ได้
tar -cz app/ | base64 -w0 | ssh -i KEY root@host-b "ssh root@host-j 'base64 -d | tar -xz -C /var/www/app'"
In the second version, the remote command has a fixed length. Data flows through the outer ssh connection's stdin into the inner ssh connection's stdin. I included base64 here to keep the data as plain text throughout, without having to worry about any layer interpreting the bytes.
Third attempt: plink and pscp with host key pinning
Later, host-j established a split-tunnel WireGuard connection to a public hub, so I could access host-j directly without going through host-b. Deployment changed to transferring files with pscp and then running commands with plink.
# รันจาก Git Bash บน Windows
export MSYS_NO_PATHCONV=1 # กัน Git Bash แปลง path ที่ขึ้นต้นด้วย / เป็น path ของตัวเอง
pscp -batch -i deploy.ppk -hostkey 'SHA256:<FINGERPRINT>' release.tar.gz root@host-j:/var/www/app/
plink -batch -i deploy.ppk -hostkey 'SHA256:<FINGERPRINT>' root@host-j \
'cd /var/www/app && tar -xzf release.tar.gz && echo "deployed $(date +%F)"'
I had made mistakes in three places before arriving at this version.
-batchprevents plink from stopping to ask anything. If it needs to ask, it fails instead. That suits scripts, but means the host key must be supplied explicitly.-hostkeypins the target machine's fingerprint, preventing both a hang at a confirmation prompt and another machine impersonating it at the same address. I read the fingerprint from the target machine once and stored it with the script.- The remote command must be enclosed in single quotes because Git Bash expands
$(...)and parentheses on my machine before sending the command to plink. With double quotes, the date would come from the dev machine rather than the server. - Git Bash converts paths starting with
/into paths under its own installation directory. I had to setMSYS_NO_PATHCONV=1first.
Fourth attempt: the intermediate host has no keys and no sshpass
Once the only entry point left was the VPN to host-j, the problem reversed. Previously, I had used host-b to reach host-j; now I had to hop from host-j to host-a and host-b instead, and host-j had neither keys nor sshpass.
The solution that required no extra installation was OpenSSH's existing askpass mechanism. I placed two wrapper files in /dev/shm, which is tmpfs: it lives in memory, stays off disk, and disappears on reboot.
# ทำบน host-j - ไฟล์รหัสสิทธิ์ 600 อยู่ใน tmpfs เท่านั้น
umask 077
cat > /dev/shm/.pw # วางรหัสแล้วกด Ctrl-D ไม่พิมพ์รหัสลงในบรรทัดคำสั่ง
cat > /dev/shm/ap.sh <<'EOF'
#!/bin/sh
cat /dev/shm/.pw
EOF
cat > /dev/shm/sshto.sh <<'EOF'
#!/bin/sh
host=$1; shift
SSH_ASKPASS=/dev/shm/ap.sh SSH_ASKPASS_REQUIRE=force DISPLAY=:0 \
setsid -w ssh -o StrictHostKeyChecking=accept-new root@$host "$@"
EOF
chmod 700 /dev/shm/ap.sh /dev/shm/sshto.sh
It works like ordinary ssh.
/dev/shm/sshto.sh host-a "hostname; df -h /data"
Each component has a different role. SSH_ASKPASS points to the program ssh calls when it needs a password. SSH_ASKPASS_REQUIRE=force forces askpass even when a tty is present; this option has been available since OpenSSH 8.4. DISPLAY must have a value because older versions check this variable before deciding to invoke askpass. setsid -w detaches the controlling terminal, and -w makes it wait for ssh to finish and return its exit code. Without -w, the calling script cannot tell whether the remote command succeeded or failed.
This method stores the password as plain text in a file. Even in tmpfs, with permissions set to 600 and the file disappearing on reboot, anyone with root access on that machine during that time can read it. I use it only while working, then immediately delete /dev/shm/.pw and the wrapper rather than waiting for a reboot. The proper long-term approach is a dedicated key with restricted permissions, revoked when the job is done.
askpass in tmpfs
No packages to install and no changes to the target's authorized_keys. It takes one minute to set up, at the cost of keeping a plaintext password in the intermediate host's memory for the duration of the job.
A dedicated key
Create a new key pair just for this job and install it in the target's authorized_keys. No password is left anywhere, and access can be revoked by deleting one line. This requires permission to modify the target machine.
Host-to-host file transfer: two single-hop connections joined by a pipe
The actual job that needed this wrapper was moving a 122 GB backup file from host-b to host-a to rebuild a replica. Neither machine had a key for the other, and I did not want a file this large written to disk on host-j, which was a web server.
My first idea was to nest ssh: have host-j run a command on host-b, then have host-b ssh into host-a itself. It hung indefinitely. The askpass wrapper existed only on host-j; the inner ssh running on host-b had neither askpass nor a session in which to ask for a password. It was waiting for something that would never arrive.
What worked was to avoid nesting entirely: have host-j open two single-hop ssh connections and connect stdout from the first to stdin of the second.
/dev/shm/sshto.sh host-b "cat /backup/replica-full.xbstream" \
| /dev/shm/sshto.sh host-a "cat > /data/stream/replica-full.xbstream"
Both connections are started on host-j, where askpass is available to both. Neither ssh process has to ask for a password on a machine that has no way to answer.
A file this large takes too long to leave a session open. If the VPN disconnected midway, the job would die with it. I therefore wrapped it in a script that ran in the background with logging, always creating the log directory first.
# บน host-j
mkdir -p /root/joblog
cat > /dev/shm/copy.sh <<'EOF'
#!/bin/sh
echo "start $(date '+%F %T')"
/dev/shm/sshto.sh host-b "cat /backup/replica-full.xbstream" \
| /dev/shm/sshto.sh host-a "cat > /data/stream/replica-full.xbstream"
echo "rc=$? end $(date '+%F %T')"
EOF
chmod 700 /dev/shm/copy.sh
nohup /dev/shm/copy.sh > /root/joblog/copy.log 2>&1 < /dev/null &
# ดูความคืบหน้าจากขนาดไฟล์ปลายทาง ไม่ต้องแตะงานที่รันอยู่
/dev/shm/sshto.sh host-a "ls -l /data/stream/replica-full.xbstream"
The rc returned by a pipeline like this belongs only to the last command. If the connection reading from host-b fails midway, the writer may still exit with 0. After the job finishes, compare the file sizes on both ends before using the file, and compare checksums too if there is time.
Database notes from along the way
Once I could access host-a, two more issues cost me time. Both produced Access denied even though the password was correct, just like the first ssh attempt.
First, a database account created as user@'%' could not connect through a unix socket on the same machine. I had to force TCP.
# ผ่าน socket: Access denied
mariadb -u appadmin -p
# บังคับ TCP: ผ่าน
mariadb -h 127.0.0.1 -u appadmin -p
A socket connection is treated as coming from localhost, and the database selects the account row with the most specific host first. A common cause is another account tied to localhost matching before the % account.
Second, for backup jobs on the database host, use root through a unix socket, which requires no password. The reason is not convenience: a password supplied with -p on the command line goes into argv, where anyone running ps on that machine while the job is running can see it.
Summary: what each attempt taught me
| Attempt | What failed | What I used instead |
|---|---|---|
| 1 | sshpass with keyboard-interactive | plink -batch and checking authentication methods before trying a password |
| 2 | File contents as an argument | Send through stdin with tar piped to base64 and then ssh |
| 3 | Git Bash expanding commands and converting paths | Single quotes, MSYS_NO_PATHCONV=1, and host key pinning |
| 4 | No keys on the intermediate host, and nested ssh hanging | askpass in tmpfs and two single-hop connections joined by a pipe |
The lesson is that when an intermediate host is mandatory, do not try to nest ssh more deeply. Ask which machine has the means to authenticate, and have that machine open every connection itself. Data can flow through pipes without the two endpoints needing to know each other. I still consider an askpass wrapper a temporary tool for a job with a clear time limit. If the job becomes routine, it needs a dedicated key. Anyone who can install a key from the start can take that route right away. If I have left anything out or made any typos, I will come back and correct them later.
Want us to set this up?
Get help with server setup, configuration, migration, or troubleshooting. Save time for the work that matters.
Hit something like this? Stuck somewhere?
Come trade notes on the LinuxThai board



Comments 0
No comments yet — be the first