SSH Magic

Seedling Confidence: Medium | Effort: Medium
Created: 2026-07-19 | Updated: 2026-07-19

I found (source) this cool method of synchronizing certain files from my laptop to my remote hosts. The main idea is to use a LocalCommand to change out the original ssh process for a different process: an rsync one. Then, after rsync is finished, execute the original ssh command but disable the LocalCommand setting.

This command is scoped to home directories. Without this, different machines would have to have the same home directory path. One restriction is that you can't automatically copy something to the root directory (for instance).

Host foo bar baz
    # rsync flags
    # -v Verbose.
    # -R Use relative paths.
    # -r Recursive.
    # -l Preserve symlinks.
    # -p Preserve permissions.
    # -t Preserve modification times.
    # -z Compress during transfer.

    # ssh variables
    # %d Local home directory.
    # %r Remote username.
    # %n Remote hostname.

    PermitLocalCommand yes
    # NOTE: newlines added for readability, this all needs to be on one line.
    LocalCommand bash -c '
cd "${1:?}" &&
rsync -LvRrptz -e "ssh -o PermitLocalCommand=no" 
"bin/cl" "bin/pi" "bin/,amend-commit-cl" "bin/,amend-commit-pi" 
"bin/d" "bin/t" "bin/,ccusage" "bin/ocl" "bin/lc" "bin/dlc" ".bashrc" 
".profile" ".bash_profile" ".pi/agent/models.json" 
".gitconfig" ".git-credentials" ".agents/skills" 
"${2:?}@${3:?}:./"' '<bash -c>' %d %r %n