In order to use public and private key based authentication to SFTP to your server, you need to have SSH enabled on your hosting account. Most hosts do not enable SSH by default, so you might want to check with your host and get it enabled if it isn't already. Once SSH is enabled, connecting to your server is simple. Here are three main steps involved:
- Generating public and private Key pairs using Cpanel.
- Downloading and converting the private key into PPK (PuTTY Private Key) format.
- Connecting to your server using an FTP client and using the PPK key for authentication.
So let's look at these steps in details:
Generating Public and Private Key Pairs Using Cpanel
In order to use SFTP, we first need to generate public and private key pairs. This can easily be done using Cpanel as detailed in the steps below:
Step 1: Login to Your Cpanel and click on SSH Shell Access under the security section.
Step 2: Click on the Manage SSH Keys button and then Click on the Generate a New Key link.
Step 3: On this page, enter the following details:
Key Password: Any password. (Note: This is the passpharse that you will need to enter while you SFTP.)
Key Type: RSA
Key Size: 2048
Once all details are entered, click on Generate Key (refer image above). This will generate a public and private key pair. You should now be able to see these files in your Manage SSH Keys page.
Step 4: On the Manage SSH Keys page, click on Manage Authorization and then click the Authorize button. This will authorize the key for usage as shown in the image below.
Step 5: Click on the View or Download link in the Private Keys section to covert and download your private key.
Converting Private Key to PPK Format
We now need to convert the private key to PPK format. You can do this using the covert key option on Cpanel, or you can download the raw file and covert it to PPK format using PuttyGen. In most cases, the Cpanel convert option works pretty good, so you can stick with it. But in-case, you don't have that option in your Cpanel account, you can use the Puttygen method. Let's look at both these methods:
Option 1: Converting the key to PPK format using Cpanel Covert key option:
To use this option, enter your passphrase in the space provided and click Convert as shown in the image below. You can then download the converted key to your computer and save it in an accessible location.
Note: The passpharse is the key password that you used while generating the keys in Cpanel.
Option 2: Converting the Key to PPK format Using PuttyGen:
This option involves using PuttyGen to convert the key. If you don't have PuttyGen installed, you can download it free from here. Once downloaded and installed, follow these steps:
Step 1: As shown in the image above (marked Option 2), click on the 'Download Key' button on the View or Download SSH Keys page. This will download the private key (id_rsa) to your computer. Copy and save this file in an accessible location.
Step 2: Open the PuttyGen application and click Run.
Step 3: Go to Conversions > Import Key, browse to the location of your downloaded private key file (id_rsa) and select the file.
Once you load the file you will be prompted to enter the passpharse. Enter the passpharse and click ok.
Step 4: Make sure that the SSH2 RSA option is selected and the number of bits is set to 2048.
Step 5: Click on Save private key and save the file with your preferred name. (Refer image above).
SFTP to the Server
Now that we have our public and private keys setup, we can SFTP to the server. You can do this using any FTP client like Filezilla or WinSCP. I am using WinSCP for this tutorial.
Step 1: Open WinSCP and create a new FTP connected by clicking on New Site and enter the following details:
Host Name: ftp.domainname.com
Port Number: 22
Username: Cpanel Username
Password: Cpanel Password
Step 2: Click on the Advanced botton to open the Advanced Site Settings page as shown in point no.6 in the image above.
Step 3: On the Advanced Site Settings page click on Authentication and then browse to the location of your PPk file. Refer image below:
Step 4: Once done, click ok and then click Save to save the settings.
Step 5: Click Login to login to your server using SFTP. Once the connection is establised and the server has finished verifing the private and public keys, you will be promoted to enter the passpharse. Enter the passpharse and click Ok.
You should now be connected to your server using SFTP.