Creating a new SSH key is a straightforward process that involves using the ssh-keygen
command in your terminal.
Generating an SSH Key
- Open your terminal.
- Run the command:
ssh-keygen
- Choose a file location for your key: If you don't specify a location, the key will be saved in your home directory under
.ssh
. - Enter a passphrase: This is optional, but highly recommended for security.
- Confirm your passphrase: Type it again.
Understanding Key Components
Once generated, you'll have two files:
id_rsa
: This is your private key. Keep this file secure and private.id_rsa.pub
: This is your public key. You'll share this with servers or services that you want to access.
Adding Your Public Key to a Server
To use your new SSH key, you'll need to add your public key to the server you want to access.
Here's how:
- Copy your public key: Open the
id_rsa.pub
file and copy the contents. - Add the public key to the server: There are different ways to do this, depending on the server you are using.
- SSH: You can use the
ssh-copy-id
command to copy the key directly to the server. - Manual addition: You can add the public key to the authorized_keys file on the server.
- SSH: You can use the
Practical Insights
- Use a strong passphrase: Choose a complex passphrase that you can remember easily.
- Protect your private key: Never share your private key with anyone.
- Use different key pairs for different servers: This helps to improve security.