Onjsdev

Share


How To Generate a New SSH Key


By onjsdev

Dec 16th, 2023

An SSH key is a pair of keys used to authenticate for secure remote access over an SSH protocol. It provides a more secure alternative to password-based authentication.

Here's how to generate a new SSH key:

1. Open a Terminal:

  • Windows: Search for "Command Prompt" and open it.
  • Mac/Linux: Open your preferred terminal application, like Terminal or iTerm.

2. Generate the Key Pair

Run the following command in your terminal:

ssh-keygen

This command will prompt you for several options:

  • Enter file in which to save the key (default: ~/.ssh/id_rsa): Press Enter to accept the default location. You can change the filename if you want.
  • Enter a passphrase (optional): A passphrase adds an extra layer of security to your key. It's highly recommended to set a strong passphrase. Enter and confirm your chosen passphrase.

Copy the Public Key

Once the key is generated, you'll see its details displayed in the terminal. Look for the line that starts with "ssh-rsa". This is your public key. You'll need to copy this key and add it to the server you want to access.

4. Add the Public Key to the Server

The process for adding the public key to the server will vary depending on the server you're using. Here are some general steps:

  • Log in to the server. -Open the authorized_keys file: This file stores authorized SSH keys for users. The location may vary, but it's typically in the .ssh directory of the user you want to grant access to.

You can use the following command to find it:

cat ~/.ssh/authorized_keys

Paste your public key into the file. You can use the nano or another text editor to edit the file. Save the file and close the editor. Lastly, test the connection.

Try connecting to the server using your SSH client and the username associated with the key. You should be able to connect without entering a password.

Best Practises

  • Keep your private key safe: Never share your private key with anyone.
  • Use a strong passphrase: Choose a long and complex passphrase that is difficult to guess.
  • Consider using key management tools that can help you manage your SSH keys securely.

Conclusion

Now you have generated a new SSH key pair and configured your system to use it. Thank you for readind.