DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

SSH Config

We saw that we can ssh into our vagrant virtual machine in the post Add SSH Public Key to Vagrant:

$ ssh -i ~/.ssh/id_rsa.pub -p 2222 vagrant@localhost
Enter fullscreen mode Exit fullscreen mode

However it is too long to type, right? And if you ssh into a server you should remember the IP, like 173.108.251.102. There should be an easy way to do that since we all are lazy developers (?).

The ssh program on a host receives it's configuration from either the

  1. cli (as we've seen above),
  2. /etc/ssh/ssh_config file for global-scope,
  3. ~/.ssh/config file for user-scope.

These files are formatted like below:

For our local vagrant machine:

Host vagrantlocal
  HostName localhost
  Port 2222
  User vagrant
  IdentityFile ~/.ssh/id_rsa.pub
  PasswordAuthentication no
Enter fullscreen mode Exit fullscreen mode

For a remote server:

Host myremoteserver
  HostName 173.108.251.102
  User user
  IdentityFile ~/.ssh/id_rsa.pub
  PasswordAuthentication no
Enter fullscreen mode Exit fullscreen mode

And then you can access them via ssh:

# vagrant virtual machine
$ ssh vagrantlocal

# remove server
$ ssh myremoteserver
Enter fullscreen mode Exit fullscreen mode

You can see more configuration file options at Configuration Options.

All done!

Top comments (0)