PowerShell: Quick Start Guide for OpenSSH

Published

Here is an example of how to connect to a remote virtual machine or server from Peerobyte to manage it using OpenSSH via PowerShell. In this example, the client system is running Windows and the server system is running Ubuntu 22.04. For other Linux distributions, the procedure will be similar, but the OpenSSH installation and user authorization procedures described in the “Installing an SSH server” and “Creating a new user” sections may be different.

Most of our Linux-based OS distribution images already have an OpenSSH server installed. If you want to connect to a virtual machine or server with a pre-installed OpenSSH server as a root user rather than a new user you created, you do not need to follow the steps in the “Server Side Actions” section.

Prerequisites

Customer requirements

  1. The client system must be running the Windows operating system.
  2. The client system must have PowerShell version 5.1 or higher installed. Instructions for installing PowerShell can be found in the guide “Installing PowerShell on Windows”.

Server requirements

  1. The server system must be running a Linux-based operating system.
  2. The server system must be running on a Peerobyte virtual machine or server.
  3. A Linux terminal must be available on the server system.

Steps to Follow

Server Side Actions

1. Connect to a virtual machine or server.
Connect using the Peerobyte control panel console as described in the "Connecting via Console" section.


2. Update the information about the current versions of system packages available:

		
sudo apt update


3. Install OpenSSH for the server:

		
sudo apt install openssh-server

If you want to remotely manage the server as the root user, you can skip steps 4-6 about creating a new user and go directly to the "Client Side Actions" section.

4. Create a new user and a home directory for them:

		
useradd newuser1 -d /home/newuser1 -m


5. Set a password for the new user:

		
passwd newuser1

When prompted:

		
New password:

Come up with a user password, enter it, and press the Enter key; then repeat the input and press Enter again. The password will not be displayed on the screen during this process.

6. Add the new user to the Sudo group if needed:

		
usermod -aG sudo newuser1

The server side is ready for connection; proceed to the client side.

Client Side Actions

1. Launch PowerShell as an administrator.

2. Check the version of OpenSSH available to the system:

		
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Client*'

If the result of the command shows that the State is Installed, it means that the OpenSSH client is already installed on your system, and no further installation actions are required. If this is the case, proceed to step 5.

3. Install OpenSSH for the client:

		
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

If the installation result indicates that RestartNeeded is False, then the OpenSSH Client is ready to use without a reboot. If this is the case, proceed to step 5.en the OpenSSH Client is ready to use without a reboot. If this is the case, proceed to step 5.

4. Reboot the device.

Before rebooting, save all unsaved files, then execute the command:

		
Restart-Computer

The device will reboot, after which the OpenSSH Client will be ready to use. After rebooting, you will need to launch PowerShell as an administrator again.

5. Create a key pair:

		
ssh-keygen

When prompted:

		
PS C:\WINDOWS\system32> ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (C:\Users\current_user/.ssh/id_rsa):

Press Enter to accept the default location and name for the key pair. Alternatively, you can enter your own path for the key, which will also need to be specified when copying the key and connecting to the server.

When prompted:

		
Enter passphrase (empty for no passphrase):

Create and enter a passphrase, then press Enter. Re-enter the passphrase and press Enter again. If the passphrases do not match, repeat the input.

6. Transfer the public key to the Server:

		
cat C:\Users\current_user\.ssh\id_rsa.pub | ssh [email protected] "mkdir -p /home/newuser1/.ssh; cat >> /home/newuser1/.ssh/authorized_keys"

Here:
C:\Users\current_user\.ssh\id_rsa.pub - path and name of the key pair, displayed during key pair creation. Append with *.pub to denote the public key being copied to the server.


newyuser1 - username used when creating the user on the Server, or use root

203.0.113.12 - server IP address, taken from the Main IP Address field in the "Public Network IPv4" section of the Peerobyte virtual machine or server control panel.

/home/newuser1/.ssh - user's home directory on the server as specified during creation, appended with the subdirectory /.ssh, where OpenSSH-related data is stored. For root user, it is usually /root directory. You can display a user's home directory on the server with the command: echo ~<username>.

/home/newuser1/.ssh/authorized_keys - user's home directory on the server, as specified during creation, appended with the subdirectory /.ssh and the filename authorized_keys, where authorized public keys will be stored.

If you have not connected to the server from this client before, you will see the following prompt:

		
The authenticity of host '203.0.113.12 (203.0.113.12)' can't be established. ECDSA key fingerprint is SHA256:tXw8aipJLyyL4KBAjCaBvHU7K//HzkXuTtdLij2yJ2U. Are you sure you want to continue connecting (yes/no/[fingerprint])?

Respond by typing yes on the keyboard and pressing the Enter key to confirm your intention to connect to an unfamiliar host.

When prompted:

		
[email protected]'s password:

Enter the user password that you assigned to the new user on the Server and press Enter. If you specified the root user name, you can get the password for it in the Password field of the "instance Details" section of the Peerobyte virtual machine or server control panel.

7. Connect to the remote server:

		
ssh [email protected]

Use the IP address from the Main IP Address field of the "Public Network IPv4" section of the Peerobyte virtual machine or server control panel. Specify the username of the user created on the Server or root.

When prompted:

		
$ ssh [email protected] Enter passphrase for key 'C:\Users\current_user/.ssh/id_rsa':

Enter the passphrase you used when creating the key pair and press Enter.

If the passphrase and all steps are correctly followed, you will be connected to the remote server for management, and you will see the corresponding prompt.

To disconnect from the server and return to the client terminal, you can enter the exit command and press Enter.

Detailed Steps for Connecting via OpenSSH:

  1. SSH Server Installation.
  2. Creating a New User.
  3. SSH Client Installation.
  4. Key Pair Generation.
  5. Transferring the Public Key to the Server.
  6. Connecting to the Server via SSH.