Loading...

Knowledge Base

SSH Keying through PuTTY on Windows or Linux

SSH keys let you log in to your server securely without entering a password each time. This article explains how SSH keys work and how to set them up on Windows, using PuTTY or PowerShell, and on Linux or macOS, using the Terminal.

What Are SSH Keys and How Do They Work?

SSH keys are generated as a pair, made up of a public key and a private key.

  • Private key - This key stays on your local computer. Do not share it with anyone.
  • Public key - This key is placed on the server you want to connect to, so the server can match it to your private key.

When you connect to a server, your SSH client uses your private key to verify your identity, and the server checks it against your public key. If the two match, you are granted access to the server. Because no password is sent to the server, the process is both safer and more convenient.

Benefits of Using SSH Keys

  • Secure Authentication: SSH keys are a secure way to verify users and systems on remote servers, replacing the less secure method of using passwords.
  • Encrypted Communication: SSH encrypts communication between devices, helping protect data in transit during remote access or file transfer.
  • Automation of Secure Access: SSH keys allow automated scripts and processes to securely access servers or other remote devices without you entering your credentials manually each time.
  • Remote Server Access: SSH keys let you access servers remotely, so you can manage websites or databases securely without the risks of using passwords.
  • Enhanced Security: SSH keys reduce exposure to password-based attacks, since the private key is never transmitted over the network.
  • Convenience: With SSH keys, you don't need to remember complex passwords, which makes remote server access faster and more convenient.
  • Stronger Authentication: SSH keys use asymmetric cryptography, which provides much stronger authentication than a typical password.

How to Set Up SSH Keys on Windows Using PuTTY

PuTTY is a popular SSH client for Windows, and PuTTYgen is its key generator. Follow these steps to generate your keys, add them to your server, and connect.

  1. Download PuTTY and PuTTYgen. You can get the latest version of both at the following links:
    • PuTTY: the Telnet and SSH client itself (Windows x86)
    • PuTTYgen: the SSH key generation utility (Windows x86)

    Other versions, including Unix/Linux, are available on the PuTTY download page.

  2. Create a PuTTY profile for your server. Open PuTTY and, in the Host Name field, enter your domain or your server's IP address. The Port defaults to 22. If you're connecting to a shared or reseller account, change it to 2222.

    Under Connection > Data, enter the username you want to connect with in the Auto-login username field. If this is not root, it will most likely be your cPanel username.

    Click Session at the top of the left menu, type a name for the profile (your domain name works well), then click Save. PuTTY saves this configuration for later use.

  3. Connect to the server. Click Open to start the PuTTY session. The first time you connect, PuTTY shows a security alert about the server's host key. This is normal, so click Yes. If you see your server's bash prompt, similar to the example below, the connection worked.
    gator100 ~ $
  4. Generate the SSH key. Open PuTTYgen. Under Type of key to generate, select Ed25519 if it's available.
    • If the server or application you are connecting to does not support Ed25519, select RSA instead, and set Number of bits in a generated key to 4096.

    Click Generate, then move your mouse around the blank area until the progress bar is full. PuTTYgen uses this movement to make the key random.

    Once the key is generated, you can type a Key comment to help identify the key pair; it's only a label. You can also type a Key passphrase and confirm it. A passphrase is recommended, since it protects the key if someone copies the file, but you'll need to enter it each time you use the key. For a passwordless login, leave it blank.

    Click Save public key and save the file somewhere safe on your computer. Then click Save private key to save the private key in PuTTY's .ppk format. Remember where you saved these files, as you'll need them later. Keep the private key secure and do not share it.

  5. Add the public key to the server. In PuTTYgen, copy the text in the Public key for pasting into OpenSSH authorized_keys file box. It should look similar to the example below.
    ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGv0w5mD3bXk9Qm1s8nYyq3Zr2pHcT7uLfEo4aJdW6xK [email protected]

    Go back to your PuTTY session and run the following commands, one at a time.

    mkdir ~/.ssh
    chmod 700 ~/.ssh
    nano ~/.ssh/authorized_keys2

    This opens a text editor. Right-click in the PuTTY window to paste the key you copied. If the key pastes onto more than one line, go to the beginning of each extra line and press Backspace once until the whole key sits on a single line.

    Press CTRL+X, then Y, then Enter to save the file and return to the bash prompt. Then run the following command:

    chmod 600 ~/.ssh/authorized_keys2
  6. Configure PuTTY to use your private key. Close PuTTY and reopen it. Select the profile you saved earlier and click Load.

    In the left menu, go to Connection > SSH > Auth (in newer PuTTY versions, Connection > SSH > Auth > Credentials). Click Browse and select the .ppk private key you saved. Then click Session at the top of the left menu and click Save again.

  7. Confirm the setup is successful. Click Open. PuTTY connects to your server and sends your key, and the server should accept it without asking for your password. If you set a passphrase, enter it when prompted. You should be taken straight to the bash prompt.

How to Generate SSH Keys on Windows Using PowerShell

Windows includes OpenSSH, so you can also generate SSH keys from Command Prompt or PowerShell instead of PuTTYgen.

  1. Press Windows + X or open the Start menu, then launch PowerShell.
  2. Run the following command to generate the SSH key pair.
    ssh-keygen
  3. Press Enter when you see the following prompt to accept the default location.
    Enter a file in which to save the key (C:\Users\<username>\.ssh\id_ed25519):
  4. When prompted, set a passphrase for added security.
    • For additional protection, enter a passphrase, then press Enter.
    • To skip this part, press Enter twice.
  5. Once done, you will see a message similar to the following:
    Your identification has been saved in C:\Users\<username>\.ssh\id_ed25519.
    Your public key has been saved in C:\Users\<username>\.ssh\id_ed25519.pub.
    The key fingerprint is:
    SHA256: xxxxxxxxxxxxxxx...
  6. To check that the keys were created, go to the .ssh folder by running the following command:
    cd ~\.ssh

    You should see these two files:

    • id_ed25519 (private key)
    • id_ed25519.pub (public key)
  7. To connect with PuTTY, convert your private key to .ppk format. Open PuTTYgen (see step 1 above to download it), then:
    1. Click the Load button and locate the private SSH key you generated.
    2. Type in the passphrase for your SSH key, if you set one.
    3. Click the Save private key button to create the .ppk file.
  8. Open the .pub file in a text editor and copy its contents. Then continue with step 5 of the PuTTY instructions to add your public key to the server, and steps 6 and 7 to connect using your .ppk file.

How to Set Up SSH Keys on Linux or macOS Using the Terminal

These steps apply to OpenSSH users on Unix-like operating systems, including Linux, macOS, and BSD. For most users, Ed25519 is the recommended key type.

  1. Launch your terminal application.
  2. Run the following command to generate the SSH key pair.
    ssh-keygen -t ed25519
  3. When you see the prompt "Enter file in which to save the key," press Enter to accept the default location.
  4. Type a secure passphrase, then press Enter. Type it again to confirm, then press Enter.
  5. You will receive a success message with the details of the key you generated, similar to the following:
    Generating public/private ed25519 key pair.
    Your identification has been saved in /home/<username>/.ssh/id_ed25519.
    Your public key has been saved in /home/<username>/.ssh/id_ed25519.pub.
    The key fingerprint is:
    SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx username@localhost
  6. (Optional) Make sure your ~/.ssh folder has the correct permissions by running the following command:
    chmod 0700 ~/.ssh
  7. Correct the file permissions within the ~/.ssh folder.
    chmod 0600 ~/.ssh/*
  8. Display your public key, then copy the output. This is the key you'll add to your server.
    cat ~/.ssh/id_ed25519.pub
  9. Log in to your server with your password, replacing username with your cPanel username and 203.0.113.10 with your server's IP address. Shared and reseller accounts use port 2222.
    ssh -p 2222 [email protected]

    Then run the following commands, one at a time:

    mkdir ~/.ssh
    chmod 700 ~/.ssh
    nano ~/.ssh/authorized_keys2

    Paste your public key on a single line, then press CTRL+X, then Y, then Enter to save. Then run the following command:

    chmod 600 ~/.ssh/authorized_keys2

    Type exit to disconnect.

  10. Connect to your server with your SSH key using the following command. Replace port with your SSH port, ssh-key with the path to your private key, user with your username, and IP with your server's IP address.
    ssh -p port -i ssh-key user@IP

    For example:

    ssh -p 2222 -i ~/.ssh/id_ed25519 [email protected]
  11. Enter the passphrase for the key. A passphrase helps protect your private key if someone obtains a copy of the key file. Without one, anyone who has a copy of your private key file could use it to access your server. After entering the passphrase, you'll be connected to the server.
Have another question? HostGator supports are here to help; please contact us via phone or chat.

Loading...