Here's a step-by-step guide on how to generate and add SSH keys to Ubuntu or Debian:

  1. Open a terminal on your Ubuntu/Debian machine (NOT THE SERVER! We are generating your key pair so this has to be done on your local system) and type the following command:

    ssh-keygen -t rsa
    This will start the ssh-keygen utility, which will prompt you to specify a location to save the key. By default, the utility will save the key in the ~/.ssh/ directory with the filename id_rsa. You can accept the default location by pressing Enter.
     
  2. The utility will then prompt you to enter a passphrase. This is an optional security measure that adds an additional layer of protection to your SSH key. If you choose to set a passphrase, you will be required to enter it each time you use the key. If you prefer not to set a passphrase, just press Enter.
  3. Once the key has been generated, you can view it by running the following command:

    cat ~/.ssh/id_rsa.pub
    This will print the contents of your public SSH key to the terminal.

  4. To add the key to your account on a remote server, you will need to copy the contents of your public key and add it to the ~/.ssh/authorized_keys file on the remote server. You can do this by running the following command while logged in via ssh on the remote server:

    echo "paste-your-key-here" >> ~/.ssh/authorized_keys
    Replace "paste-your-key-here" with the contents of your public SSH key. Make sure to include the entire key, including the ssh-rsa at the beginning and the username@hostname at the end.


That's it! You should now be able to use your SSH key to log in to the remote server.

Here are a few additional tips:

  • If you want to use a different name for your SSH key, you can specify it when generating the key using the -f flag. For example, to save the key as my_key, you can run ssh-keygen -t rsa -f ~/.ssh/my_key.

  • If you want to use a different directory for your SSH keys, you can specify it when generating the key using the -C flag. For example, to save the key in the ~/keys directory, you can run ssh-keygen -t rsa -C ~/.ssh/keys/my_key.

  • If you want to change the passphrase for an existing SSH key, you can use the ssh-keygen utility with the -p flag. For example, to change the passphrase for the default id_rsa key, you can run ssh-keygen -p -f ~/.ssh/id_rsa.

Was this answer helpful? 0 Users Found This Useful (0 Votes)