The world’s leading publication for data science, AI, and ML professionals.

Benefits of Remote Entry with SSH

Keep your momentum going with ssh-keygen

Quick tutorial

Photo by Goh Rhy Yan on Unsplash
Photo by Goh Rhy Yan on Unsplash

All of my work is done on a remote computer, which would normally require me to enter my password multiple times throughout the day. This small task can be avoided.

In this post, I will describe how to set up keyless entry into a server so you don’t have to type a password each time you log in or scp a file. I will also describe how to remote copy a file through a bastion server without entering a password and how to schedule remote backups.


0. Set up keyless entry

On your local machine, make an RSA key using the following command:

ssh-keygen

This will create a file with your RSA key. The default location is

~/.Ssh/id_rsa

Copy that key into ~/.ssh/authorized_keys on your remote server:

ssh-copy-id [email protected]

Now you can ssh into the server without a password:

ssh [email protected]

What if my remote server has a bastion host?

In that case, you can follow the same steps as above, treating your bastion server as your local machine.

First, log in to your bastion server and create an RSA key. Then use ssh-copy-id to copy it to your remote server. Finally, confirm that you can ssh into the remote server with a password.


1. Copying files

Once an ssh key is set up, you can transfer files across a server without a password.

1.1 Copy from local machine → remote server

1.2 Copy from remote server → local machine


2. Move files across a bastion server

You can use move files to a remote server, even if you have a bastion node in the middle.

First, make sure you set up an RSA key on your bastion server and your remote server using ssh-keygen

2.1. Copy from your local machine → remote server

You can use rsync to copy a file from your local machine to a remote server.

The -e flag executes the command that follows it. It will first ssh into the bastion server and then ssh into the remote server.

2.2 Copy from a remote server → local machine

This will copy a file from a remote server to your local machine. Again, bypassing the bastion server.


2. Remote backup

With an RSA key enabled, you can seamlessly backup a remote directory every month using a cron job. Here is a simple example.

First, open your crontab:

crontab -e

and paste the following to schedule a remote backup

This backs up the remote directory to your local machine at 1:00 AM on the 1st of each month.

The rsync flags are:

  • -r → backs up the source directory recursively
  • -t → preserves the time stamp on the files

I recommend the following video if you are not familiar with cron jobs


Conclusions

The ability to remotely log into a server has more advantages than just keyless entry. You can seamlessly move files to a remote server, transfer data across a bastion server, and schedule your remote directories to be backed up at a specified time.


Thank you for reading and supporting Medium writers

Join Medium with my referral link – Luke Gloege, Ph.D.


Related Articles