Step 4.7 Using Public Key Authentication for Automated File Transfers

Problem: Automated scripts and file transfers cannot decrypt password-protected public keys.

It is possible to use public key authentication to automatically transfer files from one machine to another. While this is usually not recommended, it may be desirable for batch scripts. However, this involves setting a blank passphrase which clearly leads to some risks. Therefore this mechanism should only be used for a one-way connection between two specific, non-privileged user IDs on different hosts .

Action 4 7 1 Decide who trusts who

Action 4 7 2 Generate a key pair

To accomplish this, do not enter a passphrase when prompted by the ssh-keygen program - instead just press enter . This can also be accomplished by supplying the “P option with a null passphrase to ssh-keygen (e.g. “P "" ). When you do not enter a passphrase, ssh-keygen understand this to indicate that you do not want the private key encrypted:

[sshuser@client.example.com]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/sshuser/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/sshuser/.ssh/id_rsa. Your public key has been saved in /home/sshuser/.ssh/id_rsa.pub. The key fingerprint is: 7e:ab:80:12:06:0f:b9:1f:f1:64:b0:0a:00:7c:69:b4 sshuser@client.example.com [sshuser@client.example.com]$

Action 4 7 3 Copy the public key to the remote system

Action 4 7 4 Verify that you are able to log in without an authentication prompt

In order to verify that the public key authentication is not prompting for a passphrase, try logging in from server.example.com as sshuser to server.example.com as sshuser2. If everything is configured properly, you should not be prompted for a passphrase and should log in automatically.

[sshuser@client.example.com]$ ssh sshuser2@server.example.com Last login: Mon Feb 24 12:25:16 2003 from client.example.com [sshuser2@server.example.com]$

Action 4 7 5 Verify that a scp transfer works

If you are able to log in to the remote system using OpenSSH without being prompted for a passphrase, you should be able to use scp to copy a file to server.example.com as sshuser2 from client.example.com as sshuser , also with no passphrase:

[sshuser@client.example.com]$ scp test.pl sshuser2@server.example.com:~ test.pl 100% ***************************** 151 00:00 [sshuser@client.example.com]$

Action 4 7 6 Secure the connection

At this point you have created the capability for a user account on a client to log in to a server using public key authentication without being prompted for a passphrase. While this makes using public key authentication more convenient , it also opens up some security risks. Namely, the private key used to log in to the remote server is now sitting unencrypted. If a malicious attacker were able to obtain the unencrypted private key, he would be able to log in as the remote user. Therefore, some further restrictions should be put into place to lock down the use of the unencrypted private key.

OpenSSH provides a number of options that can be used to restrict a key used for authentication. These options are entered in the authorized_keys file on the remote server, immediately preceding the associated key entry. Multiple options can be used to further restrict the use of the key. The options available are described below.

Категории