Skip to content

Instantly share code, notes, and snippets.

@vicradon
Created March 28, 2024 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicradon/ce9ef2437c43588eba072ed6fc71e0eb to your computer and use it in GitHub Desktop.
Save vicradon/ce9ef2437c43588eba072ed6fc71e0eb to your computer and use it in GitHub Desktop.
This gist explains the process of setting up and SSH config file that makes it easier for you to connect to remote hosts

Getting started

SSH is an integral part of a software engineer's day to day activity. You can use SSH commands to connect to a remotely hosted machine over the internet or a local network. The normal way to connect to a machine is using this syntax:

ssh -i identityfile.pem username@hostname 

You could also use a password. There's always a hassle to type this complete command, so you might want to abstract it to an alias in your ~/.bashrc or ~/.bash_profile file like this:

alias vm="ssh -i ~/linuxmachinekey.pem linuxuser@192.168.0.1"

If you have multiple virtual machines, you have to create multiple aliases. What if there was an easier way? Good news! There is an easier way. This is the SSH configuration file: ~/.ssh/config. You define the properties of your SSH connection in this file in this fashion:

Host LinuxVM
  HostName 192.168.0.1
  User linuxuser
  IdentityFile ~/linuxmachinekey.pem
  
Host KaliVM
  HostName 192.168.20.1
  User kali
  IdentityFile ~/kali.pem

You then connect to the VM in this fashion:

ssh LinuxVM

# or

ssh KaliVM

No need for typing IP addresses and the likes.

Bonus

You also get to easily connect to a remote VSCode instance with this configuration using VSCode Remote SSH.

Remote SSH VSCode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment