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 .
In the example below, we will set up sshuser on client.example.com to connect to server.example.com as user sshuser2 .
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]$
[sshuser@client.example.com]$ cd .ssh [sshuser@client.example.com]$ ls -l total 7 -rw-r--r-- 1 sshuser sshuser 223 Jan 22 11:00 authorized_keys -rw------- 1 sshuser sshuser 883 Feb 24 12:29 id_rsa -rw-r--r-- 1 sshuser sshuser 239 Feb 24 12:29 id_rsa.pub -rw-r--r-- 1 sshuser sshuser 2880 Feb 21 08:50 known_hosts [sshuser@client.example.com]$ cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAvXDPUt60ScgSrCFcYffvMTRVK8+IzOe1JKGDoIwCKlZB1E1GGkGQPHftVnan5oc+hX6C1wg66TFL M/38jtQhWmGpLo00ZbfzmA9qrE72T/G58Vl57chZOmFWMWzXXYuni8v7ThT2pMXTY5csJRqX4pPquM76LGK6Qtpf5vyQH9M= sshuser@client.example.com [sshuser@client.example.com]$
[sshuser@client.example.com]$ scp id_rsa.pub sshuser2@server.example.com:~ sshuser2@server.example.com's password: ******* id_rsa.pub 100% ***************************** 239 00:00
[sshuser@client.example.com]$ ssh sshuser2@server.example.com sshuser2@server.example.com's password: ******* Last login: Mon Feb 24 11:19:15 2003 from client.example.com [sshuser2@server.example.com]$
[sshuser2@server.example.com]$ cd .ssh [sshuser2@server.example.com]$ ls -l total 12 -rw-r--r-- 1 sshuser2 staff 227 Feb 24 12:21 authorized_keys -rw-r--r-- 1 sshuser2 staff 223 Dec 9 14:25 known_hosts [sshuser2@server.example.com]$
[sshuser2@server.example.com]$ ls ~/id_rsa.pub /home/sshuser2/id_rsa.pub [sshuser2@server.example.com]$ cat ~/id_rsa.pub >> authorized_keys [sshuser2@server.example.com]$ cat authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEA2Rv612mkhAXokW2tvA/6w34OM2PzYS9xQmY9PQg07qMqp0UbQAuEQEUc7Oga6wY9jBBQWmLx7B3c RuDdOQnASUugqQypuIVqqw4iaEi24qmrI6JTDCwEWZQ+ndODVXDDQeTVEVJLxNvvbwSvcvi3tV+7m26HooFYvoGWb7njJqk= sshuser2@server.example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAvXDPUt60ScgSrCFcYffvMTRVK8+IzOe1JKGDoIwCKlZB1E1GGkGQPHftVnan5oc+hX6C1wg66TFL M/38jtQhWmGpLo00ZbfzmA9qrE72T/G58Vl57chZOmFWMWzXXYuni8v7ThT2pMXTY5csJRqX4pPquM76LGK6Qtpf5vyQH9M= sshuser@client.example.com
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]$
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]$
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.
from="192.168.1.10,*.example.com,!client.example.com" ssh-rsa AAAAB3NzaC1yc2Eaaaa253gAAIEAuG8ugce5+/ Cno2FCcyraJN2LK5Gsl+ujV8ad8s1lobhldonA20x6i9eNln8+LP76k2jMuLFtWEmc8MwmvrnIL2ZH2SOC90QpB3KwBuS6suVWT X5AKkXpB5uEKMWQJjHoEFOQx7Kpzmkav83n73naE8u/oyagSIgLVxCIvEhk9XX8= sshuser@example.com
Note |
If the OpenSSH daemon is set to fall back to password authentication when public key authentication is not successful, a malicious user may still be able to log in to the system if they know the password for the account! That is because these options only restrict the use of the key, not the use of the account. |
command="/home/backup/backup.sh" ssh-rsa AAAAB3NzaC1yc2Eaaaa253gAAIEAuG8ugce5+/ Cno2FCcyraJN2LK5Gsl+ujV8ad8s1lobhldonA20x6i9eNln8+LP76k2jMuLFtWEmc8MwmvrnIL2ZH2SOC90QpB3KwBuS6suVWT X5AKkXpB5uEKMWQJjHoEFOQx7Kpzmkav83n73naE8u/oyagSIgLVxCIvEhk9XX8= sshuser@example.com
environment="PATH=/bin:/usr/bin",environment="TERM=vt100" ssh-rsa AAAAB3NzaC1yc2Eaaaa253gAAIEAuG8ugce5+/ Cno2FCcyraJN2LK5Gsl+ujV8ad8s1lobhldonA20x6i9eNln8+LP76k2jMuLFtWEmc8MwmvrnIL2ZH2SOC90QpB3KwBuS6suVWT X5AKkXpB5uEKMWQJjHoEFOQx7Kpzmkav83n73naE8u/oyagSIgLVxCIvEhk9XX8= sshuser@example.com
permitopen="mail.example.com:25",permitopen="mail.example.com:143" ssh-rsa AAAAB3NzaC1yc2Eaaaa253gAAIE AuG8ugce5+/Cno2FCcyraJN2LK5Gsl+ujV8ad8s1lobhldonA20x6i9eNln8+LP76k2jMuLFtWEmc8MwmvrnIL2ZH2SOC90 QpB3KwBuS6suVWTX5AKkXpB5uEKMWQJjHoEFOQx7Kpzmkav83n73naE8u/oyagSIgLVxCIvEhk9XX8= sshuser@example.com
no-pty,no-agent-forwarding,no-X11-forwarding,no-port-forwarding ssh-rsa AAAAB3NzaC1yc2Eaaaa253gAAIE AuG8ugce5+/Cno2FCcyraJN2LK5Gsl+ujV8ad8s1lobhldonA20x6i9eNln8+LP76k2jMuLFtWEmc8MwmvrnIL2ZH2SOC90 QpB3KwBuS6suVWTX5AKkXpB5uEKMWQJjHoEFOQx7Kpzmkav83n73naE8u/oyagSIgLVxCIvEhk9XX8= sshuser@example.com
SECTION I - Obtaining, Compiling and Installing OpenSSH
SECTION II - How to Use OpenSSH Clients for Unix-to-Unix Connectivity
SECTION III - How To Use PuTTY/WinSCP For PC-To-Unix Connectivity
SECTION IV - Using Public Key Authentication
SECTION V - Troubleshooting SSH Connections
SECTION VI - Advanced SSH Topics
Conclusion
Appendix - Sample sshd_config File