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:
-
A public key is a key that can be sent to a remote server for identification. The server stores it in the
~/.ssh/authorized_keysfile and uses it to verify that the connecting user owns the corresponding private key. -
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¶
-
Generate an SSH key on the local machine (not on the server). To do this, run the following command in a terminal:
If you need an RSA key (for example, for older systems), run:
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 isid_ed25519(id_rsafor 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.
-
Add the public key to the server. To do this, type the following command on the local machine, replacing
usernamewith the username on the server andip_addresswith the IP address of the server:- <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_keysfile on the server. -
Once you have completed the above steps, you will need to enter the command to connect to the server using an SSH key:
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:
-
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
SSHin the search bar.If the client is not installed, click the
Add a featurebutton and locate it using the search bar. Then click theInstallbutton.You can also use the command line to check for the OpenSSH client:
-
Go to the command prompt (shortcut Win+R >> type
cmd>> pressOK) and enter the command:To create an RSA key, use the
ssh-keygen -t rsa -b 4096command.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 isid_ed25519(id_rsafor 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:
-
To check for generated keys, change to the previously specified directory on the local device. For example,
C:\Users\user_name\.ssh:
-
Copy the public key to the server. The
ssh-copy-idcommand 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¶
-
Download and install the PuTTY client on your local device;
-
Run the
PuTTYgenprogram (puttygen.exe) from the Start menu. -
Select the
EdDSA(Ed25519) orRSAkey type and press theGeneratebutton:
-
Move the mouse several times in random directions in the Key Generation window, this action will create a random noise:

-
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 keybutton 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 keybutton saves the public key in a format that is not suitable for theauthorized_keysfile 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 withssh-ed25519orssh-rsa. -
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_keysfile: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
.ppkprivate 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_keysfile, 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_keysfile. - 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_keysfile 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.