sshfs, autossh, mosh, sshpass
sshfs, autossh, mosh and sshpass are four helpers that run on top of SSH: a remote folder mounted like a local one, a tunnel that restarts itself, a shell that survives a laggy or changing network, and a password feeder for scripts.
145.1 What they do, where they run
| Tool | Job | Install |
|---|---|---|
| sshfs | Mount a remote folder via FUSE + SFTP | apt/dnf (RHEL: EPEL); macOS needs macFUSE; Windows: SSHFS-Win |
| autossh | Restart an ssh tunnel when it drops | apt/dnf, brew |
| mosh | Roaming shell over UDP after SSH login | apt/dnf (EPEL), brew |
| sshpass | Type the ssh password for scripts | apt/dnf |
- None are native on Windows: use WSL.
- Lab: cli 192.168.165.10, srv 192.168.165.20 with an internal app on 127.0.0.1:8080.
145.2 sshfs: how it works
- Step 1
1.Editor runs ls/cat on /mnt/data (looks like a local folder)
- Step 2
2.Kernel FUSE layer hands the request to sshfs
- Step 3
3.sshfs turns it into an SFTP request inside SSH
- Step 4
4.srv's sftp-server reads the file
- Step 5
5.File data comes back and looks local to the editor
- Needs the kernel FUSE device: a container without /dev/fuse gave fuse device not found.
- An edit from cli showed up on srv immediately; fusermount3 -u unmounts (umount on macOS).
- -o passes mount and ssh options; -p port, -C compression, -F ssh config, -d debug, -f foreground.
- allow_other + default_permissions for a mount shared with other local users.
# plain container: no /dev/fusesshfs dev@192.168.165.20:/srv/data /mnt/x# cli: --cap-add SYS_ADMIN --device /dev/fusesshfs dev@srv:/srv/data /mnt/data \ -o reconnect,ServerAliveInterval=15mount | grep sshfsecho 'edit from cli' >> /mnt/data/notes.txtfusermount3 -u /mnt/data$ fusermount3 -u /mnt/data; echo rc=$?rc=0$ mount | grep -c sshfs0145.3 sshfs: connection, symlinks and ID mapping
- sshfs runs a normal ssh process asking for the sftp subsystem; ps shows options like ServerAliveInterval passed through.
- reconnect re-mounts after a network drop (open files must be reopened); max_conns uses several ssh connections.
- A missing remote dir fails before mounting; no_check_root mounts anyway and ls fails later.
- Absolute symlinks pointing outside are blocked by default; transform_symlinks makes them relative, follow_symlinks shows them as files.
- idmap=none (default) shows the remote uid (1000); idmap=user maps it to the mounting user (uid 0 here); idmap=file uses uidfile/gidfile.
- workaround=rename/truncate/... for old SFTP servers; fstab type fuse.sshfs for boot mounts.
ps -eo pid,args | grep '[s]sh -x'sshfs -d -f dev@srv:/srv/data /mnt/v2sshfs -p 2222 dev@srv:/srv/data /mnt/datasshfs dev@srv:/nope /mnt/v2 # fails before mountingsshfs dev@srv:/nope /mnt/v2 -o no_check_rootls /mnt/v2$ sshfs dev@srv:/nope /mnt/v2 -o no_check_root; echo rc=$?rc=0$ ls /mnt/v2ls: /mnt/v2: No such file or directory# srv: notes.txt owner dev = uid 1000, abs-link -> /srv/data/a.txtsshfs dev@srv:/srv/data /mnt/datals -ln /mnt/datafusermount3 -u /mnt/datasshfs dev@srv:/srv/data /mnt/data \ -o idmap=user,transform_symlinksls -ln /mnt/datasshfs dev@srv:/srv/data /mnt/data \ -o follow_symlinks,dir_cache=no$ sshfs dev@srv:/srv/data /mnt/data -o idmap=user,transform_symlinks$ ls -ln /mnt/data-rw-r--r-- 1 0 0 6 Nov 9 05:21 a.txt-rw-r--r-- 1 0 0 24 Nov 9 05:22 notes.txt145.4 autossh: a tunnel that never stays down
- Step 1
1.cli: -L 9090 -> srv -> internal app 127.0.0.1:8080
- Step 2
2.srv freezes (docker pause): ServerAlive gets no reply
- Step 3
3.After 5 s x 2 misses ssh says server not responding and exits
- Step 4
4.autossh sees the exit and starts a new ssh (count 3)
- Step 5
5.After unpause the tunnel serves the page again
- -M 0 turns off the old monitor port; ServerAliveInterval/CountMax detect a dead link instead (about 10 s here).
- ExitOnForwardFailure=yes is recommended so a broken forward also restarts ssh.
- Signals: USR1 restarts ssh, HUP retries immediately during back-off, TERM stops everything.
- Env vars: AUTOSSH_GATETIME=0 (retry even if first attempt fails), AUTOSSH_POLL (default 600), AUTOSSH_MAXSTART, AUTOSSH_LOGFILE, AUTOSSH_PIDFILE.
export AUTOSSH_DEBUG=1 AUTOSSH_POLL=10 \ AUTOSSH_GATETIME=0 \ AUTOSSH_LOGFILE=/tmp/autossh.log \ AUTOSSH_PIDFILE=/tmp/autossh.pidnohup autossh -M 0 -N \ -o ServerAliveInterval=5 \ -o ServerAliveCountMax=2 \ -o ExitOnForwardFailure=yes \ -L 9090:127.0.0.1:8080 dev@srv \ >/dev/null 2>/tmp/ssh.err &wget -qO- http://127.0.0.1:9090/cat /tmp/autossh.log$ cat /tmp/autossh.log2026/11/09 05:22:55 autossh[197]: port set to 0, monitoring disabled2026/11/09 05:22:55 autossh[197]: short poll time: adjusting net timeouts to 50002026/11/09 05:22:55 autossh[197]: starting ssh (count 1)2026/11/09 05:22:55 autossh[197]: ssh child pid is 1992026/11/09 05:22:55 autossh[197]: set alarm for 10 secskill -9 $(pgrep -P $(cat /tmp/autossh.pid) ssh) # -> starting ssh (count 2)# host: docker pause n145-srv (20 s) # -> count 3cat /tmp/ssh.err# host: docker unpause n145-srvwget -qO- http://127.0.0.1:9090/kill -TERM $(cat /tmp/autossh.pid)$ kill -TERM $(cat /tmp/autossh.pid)2026/11/09 05:23:46 autossh[197]: received signal to exit (15)$ ps -eo args | grep -c '[a]utossh -M'0145.5 mosh: SSH login, then UDP
- Step 1
1.mosh logs in with normal ssh (here ssh -p 2222)
- Step 2
2.ssh starts mosh-server new -p 60001 on srv
- Step 3
3.srv prints MOSH CONNECT 60001 <secret key>, then ssh exits
- Step 4
4.mosh-client talks only over UDP 60001 (AES-128-OCB encrypted)
- Step 5
5.Wi-Fi change or sleep: the session continues, no reconnect
- --ssh sets the ssh command, -p fixes the server's UDP port, --port=60000:60010 a range for firewalls.
- --predict=adaptive (default) echoes typing locally on slow links; always / never.
- During a 12 s pause mosh showed a Last contact banner; after unpause the same session continued.
- Ctrl-^ . ends the session (mosh is exiting); no port forwarding or file transfer.
ssh -p 2222 dev@srv mosh-server new -p 60002 -smosh --ssh='ssh -p 2222' -p 60001 \ --predict=always dev@srv# srv:netstat -lun | grep 6000ps -eo user,args | grep '[m]osh-server'[srv]$ netstat -lun | grep 6000udp 0 0 192.168.165.20:60001 0.0.0.0:*[srv]$ ps -eo user,args | grep '[m]osh-server'dev mosh-server new -c 256 -s -p 60001 -l LANG=C.UTF-8[cli]$ ps -eo args | grep '[m]osh-client'mosh-client -# --ssh=ssh -p 2222 -p 60001 --predict=always dev@srv | 192.168.165.20 60001Ctrl-^ . close the connectionCtrl-^ Ctrl-Z suspend mosh clientCtrl-^ ^ send the escape char itselfMOSH_ESCAPE_KEY='~' change escape keyMOSH_PREDICTION_DISPLAY=always prediction--predict=adaptive|always|never--port=60000:60010 UDP port range--family=inet|inet6|prefer-inet (default)--bind-server=ssh|any|IP145.6 sshpass: password for scripts
- sshpass recognises ssh's password prompt and types the password.
- -p puts the password on the command line: the calling shell's ps line and history leak it.
- Better: -f file, -e with SSHPASS env var, -d file descriptor, or stdin.
- -v shows which prompt it matches, -P changes the prompt string.
- Exit codes: 5 wrong password, 6 unknown host key (never auto-accepted), 7 host key changed, 255 ssh error.
- Best option: key login, no password at all.
O='-o PreferredAuthentications=password -o PubkeyAuthentication=no'sshpass -p lab-pass-145 ssh $O dev@srv hostnamesshpass -f /tmp/pass.txt ssh $O dev@srv whoamiSSHPASS=lab-pass-145 sshpass -e \ ssh $O -p 2222 dev@srv 'echo via port 2222'sshpass -d 3 ssh $O dev@srv id 3< /tmp/pass.txt$ sshpass -f /tmp/pass.txt ssh $O dev@srv whoamidev$ SSHPASS=lab-pass-145 sshpass -e ssh $O -p 2222 dev@srv ...via port 2222$ sshpass -d 3 ssh $O dev@srv id 3< /tmp/pass.txtuid=1000(dev) gid=1000(dev) groups=1000(dev)sshpass -v -P 'password:' -f /tmp/pass.txt ssh $O dev@srv truesshpass -p wrong-pass ssh $O dev@srv hostname # exit 5sshpass -p lab-pass-145 ssh $O \ -o UserKnownHostsFile=/tmp/empty_kh dev@srv hostname # exit 6ssh -o BatchMode=yes dev@srv 'echo key login, no password'$ ssh -o BatchMode=yes dev@srv 'echo key login, no password'key login, no password[srv]$ grep -E 'Accepted|Failed' /tmp/sshd.log | tail -4Accepted password for dev from 192.168.165.10 port 60830 ssh2Failed password for dev from 192.168.165.10 port 60846 ssh2Accepted password for dev from 192.168.165.10 port 34328 ssh2Accepted publickey for dev from 192.168.165.10 port 34336 ssh2: ED25519 SHA256:qgT3bNt01fiN0LFSMB57oN0Vwy...145.7 When to use which
- Edit remote files with local tools: sshfs.
- A 24x7 tunnel (DB, internal app): autossh -M 0 + ServerAlive options.
- Interactive shell on a laggy or roaming link (train, mobile): mosh.
- Legacy device that only accepts passwords: sshpass -e / -f; otherwise use keys.
145.8 Common mistakes
- Believing sshpass -p is safe: it leaks via ps and shell history.
- Expecting mosh to forward ports: it does not, use ssh/autossh for tunnels.
- Forgetting to open UDP 60000-61000 in the firewall for mosh.
- Using autossh without ServerAlive options: a frozen server is never detected.
- Running sshfs in a container without /dev/fuse.
145.9 Interview tips
- mosh vs ssh: mosh uses ssh for login, then runs the session over UDP and survives IP changes; no -L/-R, needs UDP open.
- Laggy train Wi-Fi with a DB tunnel and a shell: autossh for the tunnel, mosh for the shell.
- sshfs = FUSE + SFTP; unmount with fusermount3 -u.
- Prefer key login over sshpass; if needed, use -e or -f, never -p.