Remote Administration with PowerShell Core in Linux

1.5 hours
  • 4 Learning Objectives

About this Hands-on Lab

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
  1. Log into the MS Azure Portal using the provided username and password.

  2. Click on Virtual Machines and then click on the + Add button to create a new virtual machine.

  3. Under the resource group settings, click the dropdown menu and select the existing resource group.

  4. In the Virtual machine name box, enter a name for the virtual machine we are creating. In the video, we name the VM UbuntuTest1.

  5. Select the Region dropdown box and change the region to WestUS.

  6. Ensure that the Image is set to Ubuntu Server 18.04 LTS.

  7. Change the size of the virtual machine to B2ms and click Select.

  8. 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.

  9. Under Inbound Port Rules, ensure that Allow selected ports and SSH(22) are selected.

  10. Click on Next : Disks >.

  11. Under Disk Options – OS Disk Type, select the dropdown menu and choose Standard HDD.

  12. Click Next : Networking >

  13. Click Next : Management >

  14. Under Monitoring – Boot Diagnostics, select Off.

  15. Click Review + Create.

  16. Finally, click Create and wait for the VM to finish creation.

  17. 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.

  18. 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
  1. 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.

  2. 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>
  3. 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
  4. Register the Microsoft repository GPG keys.

    sudo dpkg -i packages-microsoft-prod.deb
  5. Update the list of products.

    sudo apt-get update
  6. Enable the universe repositories.

    sudo add-apt-repository universe
  7. Now we can install PowerShell.

    sudo apt-get install -y powershell
  8. Start PowerShell.

    pwsh
  9. Exit PowerShell and then exit the ssh session with the first VM.

    exit
    exit
  10. 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
  1. Log back in to the first VM with the public IP address using SSH.

    ssh <username>@<publicIPof1stVM>
  2. Install Ubuntu OpenSSH server.

    sudo apt install openssh-client
    sudo apt install openssh-server
  3. Edit the sshd_config file at /etc/ssh.

    sudo vi /etc/ssh/sshd_config
  4. In the sshd_config file, edit the PasswordAuthentication line, ensure it is uncommented and set to yes. 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
  1. Log back in to first virtual machine via ssh and enter PowerShell.

    sudo pwsh
    ssh <username>@<PublicIPofFirstVM>
  2. 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>
  3. Call the variable to see if PowerShell session has been created.

    $session
  4. Enter the PowerShell session to ensure connectivity and run a uname -a command.

    Enter-PSSession $session
    uname -a
    Exit-PSSession
  5. Invoke a PowerShell cmdlet to the loaded session and run a Get-Process cmdlet.

    Invoke-Command $session -ScriptBlock { Get-Process }
  6. Find the pwsh process on the second virtual machine session.

    Invoke-Command $session -ScriptBlock { Get-Process pwsh }
  7. Exit PowerShell and the terminal.

    exit
    exit

Additional Resources

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!