Introduction

In this tutorial, you will learn how to establish an SSH connection from a Windows machine to a remote server. We will cover the process of setting up SSH support on Windows, generating SSH key pairs, managing SSH keys, and setting up automatic authentication.

Prerequisites

Before getting started, ensure you have the following:

  • Local machine with Windows 10/11
  • Remote server with a public IP address
  •  

Step 1 - Getting SSH Support on Windows

To establish an SSH connection, you need to ensure your Windows machine supports SSH. Depending on your Windows version, you can choose one of the following options:

Option 1 - Use Windows PowerShell

This option allows you to use basic SSH commands without installing additional software. It works on Windows 10 version 1709 and later.

  1. Check your Windows version by opening Windows PowerShell and running:

    Get-ComputerInfo | select OsName, OSDisplayVersion, OsBuildNumber
  2. If you have Windows 10 version 1709 or later, you can use PowerShell for SSH. If the OpenSSH Client is not installed, you can install it by:

    • Searching for "Optional features" in the Windows search bar.
    • In the "Installed features" section, look for "OpenSSH Client." If it's not available, select "+ Add a feature" and install "OpenSSH Client."
  3. Once you have the correct PowerShell version and OpenSSH Client installed, open PowerShell and use the SSH command to connect to your server:

    ssh root@<add your IP here>

    Replace <add your IP here> with your server's IP address.

  4. If a warning about host authenticity appears, you can enter "yes" to continue.

Option 2 - Install Linux Subsystem

This option allows you to install a WSL (Windows Subsystem for Linux) distribution, enabling you to run Linux commands on your Windows machine. It works on Windows 10 version 1607 and later.

  1. Enable Windows Subsystem for Linux (WSL) by searching for "Turn Windows features on or off" in the Windows search bar. Enable "Virtual Machine Platform" and "Windows Subsystem for Linux." Then, click "OK" and reboot your machine.

  2. Install a Linux distribution, either via the Microsoft Store or PowerShell:

    • Via Microsoft Store: Search for your preferred Linux distribution (e.g., "Ubuntu") in the Microsoft Store and install it.

    • Via PowerShell: List available Linux distributions and install one of your choice:

      wsl --list --online # List available Linux distributions wsl --install <distribution> # Install a Linux distribution, e.g., "wsl --install Ubuntu"
  3. Open the Linux terminal by searching for your installed Linux distribution in the Windows search bar. You can now execute Linux commands.

  4. Connect to your server using SSH in the Linux terminal:

    ssh root@<add your IP here>

    Replace <add your IP here> with your server's IP address.

  5. If a warning about host authenticity appears, you can enter "yes" to continue.

Option 3 - Install PuTTY

You can also use PuTTY, which provides a graphical user interface (GUI) for SSH connections.

  1. Download the PuTTY package file.

  2. Install the downloaded PuTTY package, and once the installation is complete, select "Finish."

  3. In the Windows search bar, enter "PuTTY" to open the PuTTY window.

  4. In the top left text box, enter your username and IP address in the format <username>@<IP-address>. Keep port 22.

  5. Click "Open" to establish the SSH connection. You may receive a warning about host authenticity; select "Accept" to continue.

  6. Enter your password when prompted in the terminal. Note that the password won't be displayed as you type it.

Step 2 - Generating an SSH Key

Now that you know how to connect to your server, you can generate an SSH key pair for secure authentication without a password.

  1. Follow the instructions for generating an SSH key based on the SSH method you chose:

    • Linux Terminal or PowerShell: Follow the steps outlined in "Setting up an SSH key."

    • PuTTY: If you installed PuTTY, it comes with the "PuTTY Key Generator." Search for "PuTTYgen" in the Windows search bar and follow the tutorial "Generate SSH key using PuTTYgen."

  2. Save the SSH key pair in the appropriate location:

    • With PowerShell and the Linux terminal, save the keys in ~/.ssh.

    • With PuTTY, save the keys in a location of your choice.

  3. Copy your public key from your local machine:

    • PowerShell:

      Get-Content ~/.ssh/id_<type>.pub
    • Linux terminal:

      cat ~/.ssh/id_<type>.pub

Step 3 - Managing the SSH Key

Now, let's learn how to manage SSH keys for different SSH methods.

PowerShell:

All SSH files are saved in the ~/.ssh directory. To update or remove a key, you can delete the key files:

  • To remove a key:

    Remove-Item ~/.ssh/id_<type> ~/.ssh/id_<type>.pub
  • For security reasons, remove the public key from the authorized_keys file on the remote server too.

Linux Terminal:

All SSH files are saved in the ~/.ssh directory. To update or remove a key, you can delete the key files:

  • To remove a key:

    rm -rf ~/.ssh/id_<type> ~/.ssh/id_<type>.pub
  • For security reasons, remove the public key from the authorized_keys file on the remote server too.

PuTTY:

PuTTY manages SSH files such as config and known_hosts for you. To edit an existing SSH key, use the "Load" option in PuTTYgen. To delete an SSH key, simply delete the key files from your Windows machine. Remember to remove the public key from the authorized_keys file on the remote server for security.

Step 4 - Setting up Automatic Authentication

To simplify SSH connections, you can set up automatic authentication.

PowerShell and Linux Terminal:

Edit the config file in the ~/.ssh directory to add entries for default connections. This way, you can use ssh <unique-name> instead of specifying the username and IP address every time.

  1. Edit the config file:

    • PowerShell:

      notepad ~/.ssh/config
    • Linux Terminal:

      nano ~/.ssh/config

    If the file doesn't exist, create it in C:\Users\<username>\.ssh (PowerShell) or ~/.ssh (Linux Terminal).

  2. Add a new entry in the following format:

    Host <unique-name> HostName <IP-address> User <username> PreferredAuthentications publickey

    Replace <unique-name> with a preferred name, <IP-address> with your server's IP address, and <username> with your server's username. If you haven't added an SSH key to your server, change "PreferredAuthentications" to "password."

  3. Save the file and close it.

  4. With the new entry, you can now use ssh <unique-name> to connect to your server.

PuTTY:

For PuTTY, follow these steps to set up automatic authentication:

  1. Open PuTTY and navigate to "Connection" » "SSH" » "Auth" » "Credentials."

  2. Click "Browse" to select your private SSH key file (usually ending in .ppk).

  3. Go back to the "Session" category.

  4. In the top left text box, enter your username and the remote server's IP address.

  5. In the "Saved Sessions" section, enter a unique name for this configuration and click "Save."

  6. Now, when you double-click on the saved session, PuTTY will automatically connect to the specified username and IP address using the selected key.

Conclusion

Congratulations! You now know how to connect to a remote server from your Windows machine via SSH. Before working with the server, you should complete any necessary configurations based on your specific server setup. For further guidance, consider exploring the tutorial on "Initial Server Setup with Ubuntu" or equivalent documentation for your server's operating system.

Hjälpte svaret dig? 527 användare blev hjälpta av detta svar (917 Antal röster)