SSH keys is a miracle and it eases all the authentication troubles that we faced in connecting server, uploading files and accessing console when dealing with multiple hosts. On the other hand, SSH keys will be a pain in the ass unless we know how to use them properly, when, how and why? This post will list the usages of the SSH keys in some development and deployment environments I am working with.
Access Digital Ocean droplet
The droplet already provides a web-based console to access your droplet. But it does not feel direct-remote to me but remote-remote access. So, let's find out how to access from our
Terminal in Mac.
Server info and Login Credentials
Before we begin, we need to note down our the following.
1. User name: the remote user to log in (it is root* if you haven't changed)
2. **A pair of SSH keys: the public key to add your droplet and the private key to authenticate (and passphrase if you set one).
3. Droplet's IP: Assigned by the Digital Ocean to address your droplet.
If you don't have a SSH key to continue, you can take a look at How to use SSH Keys in Mac OS X.
Add the SSH key to a new droplet
- In Terminal,
pbcopy < ~/.ssh/id_rsa.pubto copy the your generated SSH public key.
- Go to Settings => Security in your Digital Ocean account and Click Add SSH key.
- Paste in the SSH key context text area and Type a Name like your computer name or whatever identity to yourself.
- And when you create a new droplet, this SSH key will be automatically added.
Add the SSH key to the existing one
On OSX, the action is taken by only one command
cat ~/.ssh/id_rsa.pub | ssh email@example.com "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
If you had not changed your default root password of your droplet, you will need to change unless it wont let you execute this command.
On linux, it has never been easier...
Configuration ssh for multiple host
It is very unlikely that we all have only one server to enable access with SSH. You have to memorise username and host address for each server. But a simple
config file inside the
ssh folder can make your life easy.
cd ~./sshin your Terminal and create a new file named
- Edit our newly created config file, with whatever editor you like,
nano configin my case.
- Type the following configuration for each of your server, and Save.
Host Server1 HostName 18.104.22.168 User root IdentityFile ~/.ssh/id_rsa Host Server2 HostName 22.214.171.124 User root IdentityFile ~/.ssh/id_rsa
4 Finally, type
ssh-add in Terminal.
That's it, we can successfully login to our droplet with SSH.
A lot of thanks to my colleague, Du Wun Aung for helping me out with this post.