Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Using SSH keys with Fabric is almost mandatory if convenience is a priority. By using the SSH key you will get a secure and KISSable script, by removing the necessity of password prompts.
The first thing we have to do is to generate our SSH key. Open up terminal, and type in:
$ ssh-keygen -t rsa -b 4096
This will generate a SSH key by using the RSA algorithm. After hitting [enter], the terminal will present you with a series of prompts. Just keep hitting [enter] to use their default values.
The newly generated key should now be stored as:
~/.ssh/id_rsa # our private key ~/.ssh/id_rsa.pub # our public key
The public key’s content must be added to the authorized_keys file on the remote server. Connect to your remote server and verify that the directory structure ~/.ssh/authorized_keys exists on your server. If this is not the case, simply create it with:
$ mkdir ~/.ssh && touch ~/.ssh/authorized_keys
Next up, you paste the content of the generated ~/.ssh/id_rsa.pub into the ~/.ssh/authorized_keys file on your server.
You're now ready to use SSH keys instead of password when connect to your server! Configure Fabric to use the SSH key by specifying the environment setting key_filename:
env.key_filename = "~/.ssh/id_rsa"