How to login with SSH keys via mac terminal to view more logs

This article looks at how to generate SSH keys and use them to login via the mac terminal. This is useful if you want to view your database logs from the terminal.

Skip to the bottom of the article if you are using digital ocean and want to access the console via their browser.

Below are the steps to do the same:

1. Generate SSH keys on your mac

Generate the SSH keys on your system by typing this into the terminal.

ssh-keygen -t rsa -b 4096 -C "you@email.com"

You will then be asked to enter the file in which to save this key, or you can press enter to save at the default location.

Enter file in which to save the key (/Users/arjunrajkumar/.ssh/id_rsa):

You will then need to enter a passphrase, or leave empty.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Once this is done, you will see the confirmation that the identification and the public key has been generated.

Your identification has been saved in /Users/arjunrajkumar/.ssh/id_rsa
Your public key has been saved in /Users/arjunrajkumar/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:nzBmJihXYZ you@email.com
The key's randomart image is:
+---[RSA 4096]----+
|.... oo+.        |
| ......
|        .o..     |
+----[SHA256]-----+

2. View and access your SSH key

To view your SSH keys which you just generated, you can type the following into the terminal

cat ~/.ssh/id_rsa.pub

This will output the SSH key in a similar format as below which you can use to talk to your database.

ssh-rsa AAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZAAAAB3NzaC1yc2XYZXYZ== you@email.com

3. Add the SSH key to your server

Copy paste the newly generated SSH key to your server. This is depended on the service you choose. I am using Digital Ocean, and these are the docs for adding the SSH keys to an existing server.

4. Access the server from the terminal

Run the following command to access the server from the terminal

ssh deploy@X.X.X.X
Replace X.X.X.X with your server's public IP address.

You will then be logged into your server and you should see a list of instructions on how to access your app folders and Rails console.

Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-127-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

   __ __       __        __    ___
  / // /___ _ / /_ ____ / /   / _ ) ___  __ __
 / _  // _ `// __// __// _ \ / _  |/ _ \ \ \ /
/_//_/ \_,_/ \__/ \__//_//_//____/ \___//_\_\

The following apps deployed to this server:

* myapp
  To open the console:
      cd ~/myapp/current && bundle exec rails c
  To read the logs:
      tail ~/myapp/current/log/production.log

Bonus: Viewing DB logs via Digital Ocean

I use Hatchbox + Digital Ocean to run my Rails apps, and if you are also on Digital Ocean, you can easily access the console from the droplets page.

Click on the droplet you want to access, and then click on access, and then 'Launch Droplet Console'. Also, remember to change 'root' to 'deploy' to get access to rails console, and the database logs.

Conclusion

This article showed how you can login to your database server from the terminal. This is useful for viewing database logs etc.