Posted in Linux commands

ssh Keys

ssh and scp are the usual programs to login to a remote host and to copy files from or to a remote host. Normally users just start the program and type their password. But this is not very convenient, and even impossible, if these programs are used in a script which has to run automatically, e.g. as a cron job.

To this end you have to create a key pair – a public and a private key – on the machine where you want to connect from. The command is:

$ ssh-keygen

Just hit several times Enter; especially be sure to not enter a password. In the .ssh directory are now the new public and the private keys:

$ ls -l .ssh
total 64
-rw-------  1 maker  staff  1675 Aug  1  2017 id_rsa
-rw-r--r--  1 maker  staff   410 Aug  1  2017 id_rsa.pub

Now you have to copy the public key to the file authorized_keys (in the directory .ssh in your HOME directory) on the machine where you want to connect to. You can do this manually with the editor of your choice (which is surely vi ;-)), or you can use a command from the ssh family:

$ ssh-copy-id <user>@<machine>

e.g.

$ ssh-copy-id maker@sensor-node

Finally log in to the remote host, so that the machine name is stored in the file known_hosts.

$ ssh maker@sensor-node

If everything is correct you don’t have to type the password. Now you are ready to (for instance) copy files securely with cron.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s