grokking hard

code smarter, not harder

TIF - Powerful SSH #1

Posted at — 2020-Apr-01

Recently, I discovered that SSH have some wonderful features and usages that I didn’t know before.

Faster copying directories with rsync via SSH

When it comes to copying files back and forth to a remote server, I usually go for scp.

1
$> scp hello.txt remote_user@server.example.com:/tmp/

scp even supports to copy a whole directory:

1
$> scp -r files/ remote_user@server.example.com:/tmp

Not until recently, a colleague of mine, Alex, taught me that using rsync happens to be faster than scp when it comes to syncing directories between local and remote server.

1
rsync -a files/ remote_user@server.example.com:/tmp

The result is fascinating! It is much much faster than scp when it comes to hundreds of files need to be synced. Better, rsync only copy files that has been changed.

There are some more advanced use cases with rsync and SSH like you can establish somehow a rsync daemon on the remote server so that you can sync files/directories over a bastion host. See “Advanced Usage” on man page of rsync.

Check out code

I usually have a need to log in to the server and do a git clone on that server for testing some code.

Cloning a repository via SSH on a remote server requires that server to have an SSH key-pair registered..

…unless we use ssh-agent.

Using SSH Agent Forwarding allows me to SSH into a remote server and do git clone on without the need to actually transfer my private key to that server.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# First run the ssh-agent daemon in case you haven't.
# See https://unix.stackexchange.com/questions/351725/why-eval-the-output-of-ssh-agent for why we gotta use `eval`.
$> eval $(ssh-agent -s)

# Add your identity key into ssh-agent.
# In case you have a key somewhere else, simply specify the path to it.
# You can attach multiple keys if you want.
$> ssh-add -K

# SSH into the remote server using Agent Forwarding option.
$> ssh -A remote_user@server.example.com

# On the remote server, perform git clone as usual
remote_user@server $ git clone git@github.com/myuser/myrepo.git