Skip to content

Instantly share code, notes, and snippets.

@AlphaIru
Last active February 24, 2024 01:37
Show Gist options
  • Save AlphaIru/26a08adc7ed0df64c5f2db692c4ad8bd to your computer and use it in GitHub Desktop.
Save AlphaIru/26a08adc7ed0df64c5f2db692c4ad8bd to your computer and use it in GitHub Desktop.
A tutorial on how to connect to your host via SSH easier and more secure.

This tutorial will explain how to create and use keys (AKA Public and Private Keys) to connect to a Linux Host. Also, I will explain the steps to use Visual Studio Code with SSH.

What are the benefits of using keys in SSH?

  1. 🏃Speedy: SSH using keys makes the connection very fast, removing the process of password input on connection to the host machine.
  2. 🔒Secure: SSH using keys is much more secure than password inputs; due to the nature of key pairs, authentication cannot be brute-forced from a third party. (⚠️ Unless somehow you are leaking the keys to the public.)

Follow these two steps to complete the tutorial! ⬇️

  • 🔑Keygen: The steps to create the public and private keys for host and client.
  • 💻VSCode: The steps to connect the client's VS Code to the host Linux machine through SSH.

Keygen Tutorial

⚠️ DO NOT SHARE PRIVATE KEYS TO ANYONE AT ANY COSTS. (They can literally get into your machines and ruin your life.)

I am hiding my public keys because I don't want to cause any issues with that.

Many websites show Keygen tutorials. I did write the modern version at the bottom, but this website does the job, just a little obsolete.

Utilize the table of contents above to choose the right OS.

⚠️ If you use another OS or your PC does not work with the steps, you can use online RSA key generators. HOWEVER, IT IS NOT RECOMMENDED FOR SECURITY PURPOSES. Google Online Generate SSH keys and try following based on the steps below.

📓 Other valuable references: https://www.brainboxes.com/faq/how-do-i-connect-to-bb400-ssh-keys

Windows

If you are using a Windows OS Client, please reference here.

💡 Windows has many ways to create keys, and they are not limited to this tutorial; choose your favorite!

Powershell / CMD Key Gen

We will use the native SSH software in PowerShell/cmd to create the keys. Using Powershell or CMD is much simpler and faster but requires some typing and skills, which can irritate some people.

Step 0:

Open PowerShell (or cmd) and try running SSH in your shell. If you see usage:, you should be fine. If you do not see usage, you need to go to the Windows website on how to install OpenSSH. image

Step 1:

Change the directory to C:\Users\[Your Windows username]\.ssh by cd .ssh or cd C:\Users\[Your Windows username]\.ssh.
image

Make the directory by mkdir .ssh if it does not exist.

Step 2:

Run ssh-keygen -t ed25519 -C "your_email@example.com" by replacing "your_email@example.com" with your email. SSH software will ask you to name the file, so name the file that fits with your application.
image

📓 Alternative key generation method for older hardware: ssh-keygen -b 8192 -t rsa. RSA keys are more compatible with older hardware than ED25519 keys but run MUCH slower.
⚠️ I suggest adding one for passphrase, but I will skip it for the sake of the tutorial.

Step 3:

Use cat [Your keyname].pub to show the public key. Copy everything and save it somewhere safe for later.
image

Done!

This method is much faster than PuTTY, but the contents can get lost easily, so save those keys carefully! You can go to VSCode Tutorial.


PuTTY Key Gen

Here, we will use PuTTY's Key Generator to create keys. This method is more reliable and makes it very easy to create the keys. I would start here and understand the whole guide.

Step 1:

Download PuTTY from https://putty.org/.

Choose the Windows MSI installer under Package files with the correct version.

Step 2:

Run the installer. The installation process should be clear and well-defined.

⚠️ If you get an error code with 2502 or 2503, open the Windows PowerShell (or CMD) with admin privileges and run the MSI file through the shell. This method will bypass the privilege issues that the user faces.

Step 3:

Open PuTTY Key Generator. The software name should be PuTTYGen, which a window search can find.
image
image

Step 4:

Press the Generate button, and the app will tell you to move the pointer in the blank area, so do that.

⚠️ Make sure the bottom parameter is set to RSA with a number of bits set to 8192 or 4096 (8192 is better for modern security).

Step 5:

The text box in the center is the public key, so copy it and save it somewhere safe! You can save the key by adding a passphrase in the key passphrase text box to prevent others from cracking the key, but in this tutorial, we will pass that procedure.
image

Step 6:

Press the Save Private Key button and save the PuTTY's private key to C:\Users\[Your Windows Username]\.ssh with the extension .ppk. Name the file with your choice that you would not forget.

This step is quite useless if you copied the Public Key from the text box, but seeing it again after closing the application by pressing the load button can be useful.

Step 7:

Save the OpenSSH Private Key by clicking Export OpenSSH key under the Conversions tab. Please save the file WITHOUT any file extensions; I suggest naming the key that is easy to remember for later procedures.
image

Done!

You can go to VSCode Tutorial.


Mac OS / Linux

Linux is straightforward and very manageable to MacOS is tricky since it only allows one method of crucial creation. It might be helpful to rely on the steps from the Native SSH Creation from Windows.

Step 1:

Open Terminal and change the directory to /Users/[Your Mac username]/.ssh by cd .ssh or cd /Users/[Your Mac username]/.ssh. for Mac, and /home/[Your Linux username]/.ssh by cd .ssh or cd /home/[Your Linux username]/.ssh. for Linux
Using Linux, for example:
image

Make the directory by mkdir .ssh if it does not exist.

Step 2:

Run ssh-keygen -t ed25519 -C "your_email@example.com" by replacing "your_email@example.com" with your email. SSH software will ask you to name the file, so name the file that fits with your application.
image

📓 Alternative key generation method for older hardware: ssh-keygen -b 8192 -t rsa. RSA keys are more compatible with older hardware than ED25519 keys but run MUCH slower.
⚠️ I suggest adding one for the passphrase, but I will skip it for the tutorial's sake.

Step 3:

Use cat [Your keyname].pub to show the public key. Copy everything and save it somewhere safe for later.
image

Done!

You can go to VSCode Tutorial.


VSCode Tutorial

These steps are elementary and straightforward. Just hang on!

Step 1:

Download and install VSCode with the right version for your OS. It should not be a hassle.
https://code.visualstudio.com/

Step 2:

Open VSCode, click on the extension tab and search for Remote - SSH by Microsoft. Install that extension so your VSCode can connect to hosts via SSH.
image

⚠️ Make sure not to install the wrong extensions.

Step 3:

Click the Green icon to open the remote options, and click Connect to Host.
image
image

The pop-up should say, Open up Remote Window.

Step 4:

You might see a few options; click the bottom selection that says Configure SSH Hosts, and choose the config file where your SSH keys are located, opening up the ssh config file.
image

Windows: C:\Users\[Your Windows username]\.ssh\config
Mac: /Users/[Your Mac username]/.ssh/config
Linux: /home/[Your Linux username]/.ssh/config

Step 5:

In the config file, write the following, make sure to change the contents in the bracket to something else:

Host [Your preferred name of this connection to host]
	HostName [Your Host Machine Name]
	User [Your Host Machine User Name]
	IdentityFile [Direct directory of your PRIVATE key]
	IdentitiesOnly yes

📓 ProTip: You can connect to GitHub/GitLab/BitBucket or any domains by a similar method as shown below:

Host github.com
	IdentityFile [Direct directory of your PRIVATE key]

Host gitlab.com
	IdentityFile [Direct directory of your ANOTHER PRIVATE key]

Example:
image

Step 6:

Before SSH into Host by VSCode, you need to transfer the contents of your PUBLIC key into your host machine. So, either physically or virtually, connect to your host.

An SSH connection using the password should be fine if you are virtually connecting to the host.
If you are physically connecting to the host, having a flash drive containing your public key is easier (MAKE SURE YOU DELETE IT LATER).

Step 7:

Open the host shell and change the current directory to .ssh, so you should be at ~/.ssh.
image

Step 8:

Using your favorite text editor (such as Vim or Nano), create a file called authorized_keys ( ⚠️ CHECK THE SPELLING) and paste the contents from the public key in here.
image

📓 Use cat authorized_keys to see if the contents have been transferred successfully.

Step 9:

Go back to your client VSCode again. Click the green button again, choose 'Connect to the Host', and choose the configured ssh host connection name that you chose from Step 5, which will create a new window for your connection.
image

Step 10:

You will see Installing VSCode tools on the bottom right if you have done it correctly. You will see Start in the middle of your VSCode window, followed by SSH:[Your Host Machine Name] on the bottom left of your window.
image

Done!

CONGRATULATIONS, YOU HAVE SUCCESSFULLY CONNECTED YOUR VSCODE TO YOUR HOSE MACHINE!

Click the explorer tab and choose the open folder button.
image

⚠️ Your organization might be restrictive on your file size, so be cautious when you install extensions because they easily gobble up much of your memory!

Extra

If you do not want other people to brute force your host machine by password, you can turn it off by editing the file in /etc/ssh/sshd_config by root privilege. Scroll down to the PasswordAuthentication and set that to no with no comments!
image

Happy coding!

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