SSH Config and SSH Key Pairs
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lesson, you will learn how to generate an SSH key pair on both Windows and Linux, and how to use it for a secure connection to a virtual machine running Ubuntu Server on VirtualBox. By the end of this lesson, you'll be able to create your own keys, copy the public key to your server, and configure SSH to automatically use your private key for a seamless and secure connection.
Generating an SSH Key Pair on Windows
Let's generate a SSH Key Pair. You will generate private and public key, store the private key on your local computer (host computer), and store the public key on your destination server (Ubuntu Server VM). This allows for a more secure (and passwordless, if desired) authentication.
The commands below will work on any Linux or up to date Windows machine. The Windows 10 April 2018 update and Windows 11 include the OpenSSH client by default, which comes with the ssh-keygen
utility
- Open Command Prompt or PowerShell: You can search for it in the Start menu.
- Run ssh-keygen: Type the following command and press Enter:
ssh-keygen -b 4096
- Follow the prompts to create your key pair. You'll be asked to choose a file to save the key and to enter a passphrase (optional but recommended for additional security).
- Locate the SSH Keys: The public and private keys will be saved in the
.ssh
directory within your user's home directory (C:\Users\<your_username>\.ssh
by default). The public key typically has a.pub
extension
Copying the Public Key to our target server
The next step involves getting the public key on to our Ubuntu Server VM. This is where there will be specific instructions depending on whether you're using Windows or Linux.
Linux
Now that you have your SSH key pair, the next step is to copy the public key to your Ubuntu Server VM. The VM in this scenario is named "Ubuntu Server" with the hostname ubuntu-server
and has the IP address 192.168.1.153
. It's also connected to a bridged network adapter.
- Copy the Public Key: Use the
ssh-copy-id
command from your host machine:bash
ssh-copy-id paulh@192.168.1.153
Replace paulh
with your username if different. You'll need to enter your VM's password.
Windows
Unfortunately Windows does not come with the ssh-copy-id utlity, so we need to write some PowerShell code to do the same thing. Of course, you can manually copy the contents of the public key and place it in the ~/.ssh/authorized_keys
file, but this little script will do it for you in a couple lines of code. Be sure to update the $username
and $ip_address
variables:
$ip_address = "1.2.3.4"
$username = "paulh"
type $env:USERPROFILE\.ssh\id_rsa.pub | ssh $username@$ip_address "cat >> .ssh/authorized_keys"
Verify the Connection: Test your SSH connection:
Run the command below to test your connection:
ssh paulh@192.168.1.153 -i ~\.ssh\id_rsa
If everything is set up correctly, you should connect to your VM without needing a password. Your Windows host my also try that id_rsa private key by default, so you can try to SSH without specifying the id_rsa file.
Modifying the SSH Configuration File
If you're computer doesn't automatically use the correct ssh key to connect, you will need to make the SSH client on your host automatically use your private key, modify the ssh_config
file.
For Windows, In command prompt (not Powershell) console, type the following:
notepad C:\Users\%username%\.ssh\config
In Linux, you can type the following:
nano ~/.ssh/config
Add the Host and IdentityFile Directives:
Host ubuntu-server
HostName 192.168.1.153
User paulh
IdentityFile ~/.ssh/id_rsa
Replace id_rsa
with the name of your private key file if it's different, and replace the User
and HostName
values if you're differ.
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.