How to connect to a MongoDB server with SSH tunnel using a Private key
October, 10, 2024
To generate SSH keys in macOS, follow these steps:
ssh-keygen -t rsa
Enter file in which to save the key (/Users/youruser/.ssh/id_rsa):
You can leave this empty to create the key in the default location, which is /home/youruser/.ssh/id_rsa, but we recommend to change it to the /Users/youruser/.ssh/mongolime for instance. The public key file will be created in the very same location, and with the same name, but with the .pub extension.
Enter passphrase (empty for no passphrase):
That completes the key generation. Below is an example of the entire process:
$ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/bushev/.ssh/id_rsa): /Users/bushev/.ssh/mongolime
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/bushev/.ssh/mongolime.
Your public key has been saved in /Users/bushev/.ssh/mongolime.pub.
The key fingerprint is:
SHA256:Z8Jub9g8YsWPRADJ5k7v7YzW24deY2f53kGjrMEaHM4 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| ..o |
| + . |
| o . |
| .o.. . o |
| oSo=o. o o|
| .++E.o.o .o|
| .o*o+oo +o|
| ..oB+.o+*|
| oo++o+o+|
+----[SHA256]-----+
ssh-copy-id -i ~/.ssh/mongolime.pub user@host
This logs into the server host, and copies keys to the server, and configures them to grant access by adding them to the authorized_keys file. The copying may ask for a password or other authentication for the server. Only the public key is copied to the server. The private key should never be copied to another machine.
Get in touch_
Or just write me a letter here_