Generate a New SSH Key Pair
To generate a new SSH key pair, use the ssh-keygen
command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-t rsa
: Specifies the type of key (RSA in this case).-b 4096
: Sets the key length to 4096 bits for added security.-C "your_email@example.com"
: Adds a label (your email) to the key for identification.
Press Enter to accept the default file location (~/.ssh/id_rsa
), or specify a different path if needed.
You’ll be prompted to set a passphrase. This is optional but adds an extra layer of security.
Add the SSH Key to the SSH Agent
The SSH agent makes it easier to manage your keys. Start the agent and add your key:
ssh-copy-id username@remote_host
Copy the Public Key to the Remote Server
You need to copy the public key to the server you want to connect to. Use the ssh-copy-id
command:
ssh-copy-id username@remote_host
Replace username
with your remote server's username and remote_host
with its IP address or domain name.
This command will:
- Prompt you for the password of the remote user.
- Add your public key to the server’s
~/.ssh/authorized_keys
file.
If ssh-copy-id
is not available, you can manually copy the public key:
- Display the key:
cat ~/.ssh/id_rsa.pub
- Copy the output and paste it into the
~/.ssh/authorized_keys
file on the remote server.
Test the SSH Connection
To verify the setup, try connecting to the server: