Setting up Public and Private Keys
To add private and public keys for accessing VSCode on a remote server, you'll need to set up SSH key-based authentication. Here's a concise guide on how to do this:
Generate SSH keys on your local machine
Make sure the private keys are in a place that will be searched for ie C:\Users\phil\.ssh\my_keys
To generate the keys open a terminal window
Win key +xSelect terminalIf the directory it's in is:C:\Users\phil
Change to the folder you have your keys inPS C:\Users\phil> cd ./.sshSo now using pwd (print working directory) will showC:\Users\phil\.ssh
So on a windows machine use the following to make the keys
ssh-keygen -t rsa -b 4096
Options we have in making a public private key pair
Lets break it all down
Structure of the command:
ssh-keygen [options]
Breaking down the specific command provided:
ssh-keygen: The base command to generate SSH keys.
-t rsa: Specifies the type of key to create.
-b 4096: Specifies the number of bits in the key.
Let's explore these options and other available flags:
-t (type):
Specifies the type of key to create.
Options include: rsa, dsa, ecdsa, ed25519
Example: -t ed25519
-b (bits):
Specifies the number of bits in the key.
For RSA, 2048 bits is considered secure, 4096 bits is more secure but slower.
Example: -b 2048-f (filename):
Specifies the filename of the key file.
Example: -f .ssh/my_key
-C (comment):
Adds a comment to the key, usually an email address.
Example: -C “[email protected]"
-N (new passphrase):
Provides a new passphrase for the key.
Example: -N "your_passphrase"
-q (quiet):
Silences ssh-keygen and doesn't ask for a file or passphrase.
-p (change passphrase):
Changes the passphrase of an existing private key file.
-y (read public key):
Reads a private key file and prints the public key.
-R (remove host):
Removes all keys belonging to a hostname from a known_hosts file.
-l (fingerprint):
Shows the fingerprint of a specified public key file.
So lets put it all togetherIf we want a 4096 bit rsa public and private key and we are in the folder
C:\Users\phil
We dont need to move to the folder now as we can use the -f flag. If we want to put it in a folder thats inside “phil” called .ssh where all the keys are and we want the file to be called “my_keys” and to add my email address as a comment inside the file then I would use the following:
ssh-keygen -t rsa -b 4096 -f .ssh/my_keys -C "myemail@mywebsite.co.uk"
Will produce 2 keys in C:\Users\phil\.sshmy_keys (private key)
and My_keys.pub (public key)
On the remote server, where the public key goes
In Scalahosting SPanel you need to switch SSH on which you do from the main admin panel and under Account Management Manage SSH Access
Copy the public key to the remote server:Copyssh-copy-id username@remote_host
The public keys need to be in a folder thats called .ssh in your root folder and the keys added to a file called authorized_keys
If using ScalaHosting you can open the terminal window and then run the following to make the folder and files:Check to see if there is already a .ssh folderHere are the commands:
To view the folders and files
ls -a
If it exists then
cd .ssh
If it doesn't exist
mkdir .ssh
and
touch .ssh/authorized_keys
NOTE: Make sure you run these commands from your account’s home directory. If you’re not sure whether you’re in the right place, you can use $cd ~ to navigate to it.
Set the file’s ownership and permissions. If you skip this step, your SSH client will be unable to access your public key and log you in. The commands you need to use are:
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
chown $your_username:your_username .ssh -R
Ensure the remote server's SSH configuration allows key-based authentication.
In VSCode, install the "Remote - SSH" extension.
Add your SSH configuration to VSCode:
Open the command palette (Ctrl+Shift+P)
Search for "Remote-SSH: Open Configuration File"
Add your server details
Host mywebsite
HostName server9.myservers.co.uk
User your_username
Port 6543
IdentityFile C:\Users\phil\.ssh\my_keys
Connect to the remote server through VSCode:
Open the command palette
Choose "Remote-SSH: Connect to Host"
Select your configured server