Skip to content

SSH key generation

In this article

Using SSH keys allows you to avoid entering the remote user's password and replace it with a hard-to-find SSH key, making the process of server management more secure.

Information

An SSH key is a cryptographic key used to authenticate a user when connecting to a remote server using the SSH protocol.

SSH keys are created in pairs:

  1. A public key is a key that can be sent to a remote server for identification. The server stores it in the ~/.ssh/authorized_keys file and uses it to verify that the connecting user owns the corresponding private key.

  2. A private key is a key that must be kept secret on the user's local machine. It is used to prove the user's identity when connecting and is never sent to the server.

To connect to a server using SSH keys, you need to create a key pair (public and private) on the local machine and then copy the public key to the remote server. It is then possible to connect to the server without providing the remote user's password. If the private key is protected with a passphrase (recommended), you will need to enter it when connecting.

Which key type to choose

It is recommended to use Ed25519 keys: they are shorter, faster and more secure than RSA and are supported by all modern systems (OpenSSH 6.5 and newer, Windows 10/11, PuTTY 0.68 and newer). Use RSA keys (at least 3072 bits long) to connect to older systems that do not support Ed25519, for example, servers with outdated OpenSSH versions.

SSH key generation for Linux/MacOS operating systems

Create an SSH key

  1. Generate an SSH key on the local machine (not on the server). To do this, run the following command in a terminal:

    ssh-keygen -t ed25519
    

    If you need an RSA key (for example, for older systems), run:

    ssh-keygen -t rsa -b 4096
    

    After running the command, you will be asked to select a filename and location for the key. By default it is stored in the ~/.ssh/ directory and the file name is id_ed25519 (id_rsa for an RSA key).

    You will then be prompted to enter a passphrase to further secure the SSH connection. You do not have to enter the passphrase:

    Enter file in which to save the key (/home/user/.ssh/id_ed25519):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    

    The fingerprint of the key and its image appear on the terminal:

    The key's randomart image is:
    +--[ED25519 256]--+
    |      .     ..o+=|
    |     . .     o +.|
    |      o . o . o.o|
    |       = o . + oo|
    |      . S . + o o|
    |       . = ..o . |
    |        o ..=o+  |
    |         ..+=Xo+ |
    |         .E=*=Oo |
    +----[SHA256]-----+
    

    You can use commands to view the SSH keys created:

    • cat ~/.ssh/id_ed25519 (or ~/.ssh/id_rsa) - private key lookup;
    • cat ~/.ssh/id_ed25519.pub (or ~/.ssh/id_rsa.pub) - public key lookup.
  2. Add the public key to the server. To do this, type the following command on the local machine, replacing username with the username on the server and ip_address with the IP address of the server:

    ssh-copy-id <username>@<ip_address>
    
    • <username> — username;
    • <ip_address> — server public IP address.

    After entering the command, the system will prompt for the password of the account on the server. After entering the password, the public key is copied to the server and added to the ~/.ssh/authorized_keys file on the server.

  3. Once you have completed the above steps, you will need to enter the command to connect to the server using an SSH key:

    ssh <username>@<ip_address>
    

Creating an SSH key during an Linux reinstall

The SSH key can be added during the reinstallation of the operating system. To do this, before starting the installation of a new operating system in Invapi, fill in the SSH key (ssh-rsa) field in the Advanced options step of the Reinstall section on the Configuration tab: paste the contents of the public key (the file with the .pub extension) into it. See OS reinstallation for details:

Note

For an example of an OS reinstallation request, see OS reinstallation API methods

SSH key generation for Windows

Information

There are two methods of generating SSH keys on the Windows operating system: one using the OpenSSH client and one using the PuTTY client. Below are instructions for generating keys using either method.

OpenSSH client

To generate an SSH key using the OpenSSH client, you need to perform the following sequence of actions:

  1. Check for the presence of the OpenSSH client (in Windows 10 version 1809 and newer and in Windows 11, it is usually installed by default). To do this, go to Settings >> Apps >> Optional features (in Windows 11, Settings >> System >> Optional features) and type SSH in the search bar.

    If the client is not installed, click the Add a feature button and locate it using the search bar. Then click the Install button.

    You can also use the command line to check for the OpenSSH client:

    ssh -V
    
  2. Go to the command prompt (shortcut Win+R >> type cmd >> press OK) and enter the command:

    ssh-keygen -t ed25519
    

    To create an RSA key, use the ssh-keygen -t rsa -b 4096 command.

    After running the command, you will be asked to select a filename and location for the key. By default, it is stored in the C:\Users\<user_name>\.ssh\ directory and the file name is id_ed25519 (id_rsa for an RSA key).

    You will then be prompted to enter a passphrase to further secure the SSH connection. This is optional:

    Enter file in which to save the key (C:\Users\user_name/.ssh/id_ed25519):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    

    The fingerprint of the key and its image appear on the terminal:

    The key's randomart image is:
    +--[ED25519 256]--+
    |      .     ..o+=|
    |     . .     o +.|
    |      o . o . o.o|
    |       = o . + oo|
    |      . S . + o o|
    |       . = ..o . |
    |        o ..=o+  |
    |         ..+=Xo+ |
    |         .E=*=Oo |
    +----[SHA256]-----+
    
  3. To check for generated keys, change to the previously specified directory on the local device. For example, C:\Users\user_name\.ssh:

  4. Copy the public key to the server. The ssh-copy-id command is not available in Windows, so you can use the following PowerShell command (you will be prompted for the user's password on the server):

    type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh <username>@<ip_address> "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
    
    • <username> — username;
    • <ip_address> — server public IP address.

PuTTY

  1. Download and install the PuTTY client on your local device;

  2. Run the PuTTYgen program (puttygen.exe) from the Start menu.

  3. Select the EdDSA (Ed25519) or RSA key type and press the Generate button:

  4. Move the mouse several times in random directions in the Key Generation window, this action will create a random noise:

  5. To protect the key with a password, enter it in Key passphrase and repeat it in Confirm passphrase. If no additional protection is required, leave these fields blank:

    Click the Save private key button to save the private key. The key will be saved in the PuTTY format (.ppk), which is used only by PuTTY and compatible programs. If you need to use the same key with the OpenSSH client, export it via the Conversions >> Export OpenSSH key menu.

    The Save public key button saves the public key in a format that is not suitable for the authorized_keys file on the server. To add the key to the server, use the text from the Public key for pasting into OpenSSH authorized_keys file field at the top of the PuTTYgen window; it starts with ssh-ed25519 or ssh-rsa.

  6. Add the public key to the server. To do this, connect to the server using a password and append the line copied from PuTTYgen to the end of the ~/.ssh/authorized_keys file:

    mkdir -p ~/.ssh && chmod 700 ~/.ssh
    echo "<public_key>" >> ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    
    • <public_key> — the public key line from the Public key for pasting into OpenSSH authorized_keys file field.

    To connect with the key, specify the path to the .ppk private key in PuTTY under Connection >> SSH >> Auth >> Credentials (in older versions, Connection >> SSH >> Auth).

Creating an SSH key during an Windows reinstall

It is possible to add the SSH key during the reinstallation of the operating system. To do this, before starting the installation of a new operating system in Invapi, fill in the SSH key (ssh-rsa) field in the Advanced options step of the Reinstall section on the Configuration tab:

Note

For an example of an OS reinstallation request, see OS reinstallation API methods

Delete SSH key

To delete an SSH key, you must

  • Open the authorized_keys file, which is usually located in the user's ~/.ssh/ directory on the server. You can use a command line text editor such as Vim or Nano.
  • Locate the line containing the public key you wish to delete. Each line corresponds to a key.
  • Delete the line with the public key you want to remove from the authorized_keys file.
  • Save the changes and close the file.
  • Check that the key no longer works for SSH login. Restarting the SSH service is usually not required: the authorized_keys file is read on every connection.

Note

The known_hosts file on the local device (~/.ssh/known_hosts in Linux/macOS, C:\Users\<user_name>\.ssh\known_hosts in Windows) stores the keys of the servers you have connected to, not your own keys. If the server has been reinstalled and a warning about a changed host key appears when connecting, remove the corresponding entry with the ssh-keygen -R <ip_address> command.

question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×