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.
There are no resources for this lesson.
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
- 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
.sshdirectory within your user's home directory (
C:\Users\<your_username>\.sshby default). The public key typically has a
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.
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-idcommand from your host machine:bash
paulh with your username if different. You'll need to enter your VM's password.
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
$ip_address = "220.127.116.11"
$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 email@example.com -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
For Windows, In command prompt (not Powershell) console, type the following:
In Linux, you can type the following:
Add the Host and IdentityFile Directives:
id_rsa with the name of your private key file if it's different, and replace the
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.