Back

How to connect to a MongoDB server with SSH tunnel using a Private key

To generate SSH keys in macOS, follow these steps:

  1. 1. Open up the Terminal by going to Applications -> Utilities -> Terminal
  2. 2. Enter the following command in the Terminal window: 
ssh-keygen -t rsa
  1. 3. Next, you will be prompted to provide the location where you want to create the private key file:
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.

  1. 4. Afterwards, you will be prompted to choose a password. This is the password required to use the private key.

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]-----+

  1. 5. Copy public key to a server

ssh-copy-id -i ~/.ssh/mongolime.pub [email protected]

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.

  1. 6. Finally, copy-paste content of a Private key file to a the MongoLime connection settings.

Get in touch_

Or just write me a letter here_