SSH & rsync File Transfer - CodeSwordsman

Key-based SSH login, ssh config host aliases, local and remote port forwarding, plus scp and rsync incremental sync recipes with dry-run previews and permission fixes.

OpenSSH 8.x+ / rsync 3.2+Updated 2026-10-3019 commands

Keys & Passwordless Login

Generate an ed25519 key pair; shorter and faster than RSA

ssh-keygen -t ed25519 -C "you@example.com"

Append the public key to the remote authorized_keys for passwordless login

ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@10.0.0.12

Start the agent, load the key once and list loaded keys with -l

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh-add -l

Put this in ~/.ssh/config to run ssh prod; keepalive reduces idle disconnects

Host prod
  HostName 10.0.0.12
  User deploy
  Port 2222
  IdentityFile ~/.ssh/id_ed25519
  ServerAliveInterval 30

Keep the config at 600 or newer OpenSSH versions ignore the key.

Connect & Port Forwarding

Log in on a custom port; note that scp uses uppercase -P

ssh -p 2222 deploy@10.0.0.12

Reach an internal host through a bastion without a separate login

ssh -J jump@bastion.example.com deploy@10.0.0.12

Local forward: map local 3307 to remote MySQL; -N skips the shell, -f backgrounds it

ssh -N -f -L 3307:127.0.0.1:3306 deploy@10.0.0.12

Remote forward: expose local 3000 on the remote 9000 for callbacks and demos

ssh -N -R 9000:127.0.0.1:3000 deploy@10.0.0.12

Dynamic forward creates a SOCKS5 proxy on 127.0.0.1:1080

ssh -N -D 1080 deploy@10.0.0.12

scp & rsync

Recursively copy over SSH; always full transfer, fine for small outputs

scp -P 2222 -r ./dist deploy@10.0.0.12:/opt/app

Incremental sync of directory contents; the trailing slash copies contents

rsync -avz --progress ./dist/ deploy@10.0.0.12:/opt/app/

Dry-run first to see which extra remote files would be deleted

rsync -avz --dry-run --delete ./dist/ deploy@10.0.0.12:/opt/app/
Risky

Sync and delete extra remote files; a wrong path can wipe the target

rsync -avz --delete ./dist/ deploy@10.0.0.12:/opt/app/

Exclude dependency folders and logs to cut transfer volume

rsync -avz --exclude "node_modules" --exclude "*.log" ./app/ deploy@10.0.0.12:/opt/app/

Use -e to pass a custom SSH port to the remote shell

rsync -avz -e "ssh -p 2222" ./dist/ deploy@10.0.0.12:/opt/app/

Permissions & Troubleshooting

Verbose handshake output helps locate key or algorithm negotiation issues

ssh -v deploy@10.0.0.12

Authenticate without a shell to confirm a deploy key works

ssh -T git@github.com

Validate sshd_config syntax before reloading to avoid locking yourself out

sudo sshd -t

Fix overly open permissions, the most common cause of key login failures

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys ~/.ssh/config
chmod 644 ~/.ssh/*.pub

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.