This hands-on lab introduces the pratcice of ssh remote administation using PowerShell Core for Linux. In this lab, we cover the following topics:
– The creation of multiple Linux virtual machines in Azure.
– The installation of PowerShell and its prerequisites on said virtual machines.
– The installaion and configuration of ssh on the virtual machines to allow for remote ssh connections.
– The creation of ssh remote PowerShell sessions between two Linux virtual machines.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create Two Linux Virtual Machines in the Azure Portal
Log into the MS Azure Portal using the provided username and password.
Click on Virtual Machines and then click on the + Add button to create a new virtual machine.
Under the resource group settings, click the dropdown menu and select the existing resource group.
In the Virtual machine name box, enter a name for the virtual machine we are creating. In the video, we name the VM
UbuntuTest1
.Select the Region dropdown box and change the region to WestUS.
Ensure that the Image is set to Ubuntu Server 18.04 LTS.
Change the size of the virtual machine to B2ms and click Select.
Under the Administrator account settings, click Password for Authentication Type and enter a username and password (x2) which we will use to log in to the virtual machine.
Under Inbound Port Rules, ensure that Allow selected ports and SSH(22) are selected.
Click on Next : Disks >.
Under Disk Options – OS Disk Type, select the dropdown menu and choose Standard HDD.
Click Next : Networking >
Click Next : Management >
Under Monitoring – Boot Diagnostics, select Off.
Click Review + Create.
Finally, click Create and wait for the VM to finish creation.
In the meantime, repeat steps 2 through 16, only name the second virtual machine to something else. In this lab, we named the second VM
UbuntuTest2
.Click Home and then click Virtual Machines to ensure that both virtual machines have been created and are present in the list.
- Install PowerShell and All Prerequisites on Both Linux Virtual Machines
In the Virtual Machine list, click on the name of the first virtual machine. Off to the right of the screen is the public IP address for the VM. Copy this IP address.
Using the Azure Cloud Shell in Bash mode, connect to the first virtual machine. If there is not storage, create a storage account by clicking Show Advanced Settings, changing the region to West US, using the existing accounts, and creating a new file share with all lowercase leters. In this example, we used
filesharetest
.ssh <username>@<publicipaddress>
To install PowerShell, we need to first install the prerequisites. First, download the Microsoft repository GPG keys.
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
Register the Microsoft repository GPG keys.
sudo dpkg -i packages-microsoft-prod.deb
Update the list of products.
sudo apt-get update
Enable the
universe
repositories.sudo add-apt-repository universe
Now we can install PowerShell.
sudo apt-get install -y powershell
Start PowerShell.
pwsh
Exit PowerShell and then exit the ssh session with the first VM.
exit exit
Repeat steps 1 through 10 for the second virtual machine. In our lab, the second VM is named
UbuntuTest2
.
- Install OpenSSH Client and Server on Both VMs
Log back in to the first VM with the public IP address using SSH.
ssh <username>@<publicIPof1stVM>
Install Ubuntu OpenSSH server.
sudo apt install openssh-client sudo apt install openssh-server
Edit the
sshd_config
file at/etc/ssh
.sudo vi /etc/ssh/sshd_config
In the
sshd_config
file, edit thePasswordAuthentication
line, ensure it is uncommented and set toyes
. Also, add a PowerShell subsystem entry.PasswordAuthentication yes
Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile
5. Save the file in vi by pressing `ESC`, and then `:wq` 6. Restart the sshd service.
sudo service sshd restart
7. Exit the ssh session.
exit
8. Repeat steps 1 through 7 for the second virtual machine.
- Working with PowerShell SSH Remoting
Log back in to first virtual machine via ssh and enter PowerShell.
sudo pwsh ssh <username>@<PublicIPofFirstVM>
Create a new PowerShell session with the second virtual machine using a variable and the
New-PSSession
cmdlet.$session = New-PSSession -HostName <2nd VM Public IP Address> -Username <AdminUserfor2ndVM>
Call the variable to see if PowerShell session has been created.
$session
Enter the PowerShell session to ensure connectivity and run a
uname -a
command.Enter-PSSession $session uname -a Exit-PSSession
Invoke a PowerShell cmdlet to the loaded session and run a
Get-Process
cmdlet.Invoke-Command $session -ScriptBlock { Get-Process }
Find the
pwsh
process on the second virtual machine session.Invoke-Command $session -ScriptBlock { Get-Process pwsh }
Exit PowerShell and the terminal.
exit exit