13

How do you setup server to server SFTP to use public-key authentication instead of user account and password?

3 Answers3

12

In the client you need to generate its public key and add it to server's authorized key list.

The following are the commands you can use.

On client machine

ssh-keygen -t dsa -f id_dsa
mv id_dsa* ~/.ssh/
scp ~/.ssh/id_dsa.pub USER_NAME@SERVER:~/.ssh/HOST_NAME.key

On the server

cat ~/.ssh/HOST_NAME.key >> ~/.ssh/authorized_keys2
beaver
  • 523
  • 1
  • 9
  • 20
bhups
  • 14,345
  • 8
  • 49
  • 57
  • 5
    Use `ssh-copy-id` instead of your `scp` + `cat` dance. – ephemient Feb 09 '10 at 20:25
  • Nice, like that ssh-copy-id. I'm on a HP-UX and wish it were installed. –  Feb 09 '10 at 20:40
  • Oh right... I forget that not all the world uses OpenSSH. Sad. – ephemient Feb 10 '10 at 22:05
  • Can I login from unknown machine using the private key that was generated for known/configured client to communicate to server? I mean, is this private key portable & could be used to authenticate myself to server from other machines, in case of emergency needs ?? – Rajat Gupta Feb 28 '14 at 08:35
3

Remember to

chmod 700 .ssh

and also

chmod 600 authorized_keys
JONATHAN SEDER
  • 376
  • 1
  • 4
1

This is a solution for windows users

I had a similar issue on windows so I used Putty from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

If you need to generate a public key then use: http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe

Then, when you want to automate a batch to download from the FTP server the Pageant in order to load the private key into session http://the.earth.li/~sgtatham/putty/latest/x86/pageant.exe

Then use the PSFTP to connect and perform actions http://the.earth.li/~sgtatham/putty/latest/x86/psftp.exe

So here is sample code for the batch:

!--Loading the key to session--!
@C:\pageant.exe "C:\privatekey.ppk"
!--Calling the PSFTP.exe with the uaser and sftp address + command list file--!
@C:\psftp user@your.server.address -b C:\sftp_cmd.txt

Command list file (sftp_cmd.txt) will like like this:

mget "*.*" !--downloading every thing
!--more commands can follow here
close

Now, all you need to to schedule it in scheduled tasks *I wish it was simple as unix's cron job....

Gil Allen
  • 1,169
  • 14
  • 24