Skip to main content

Remote Administration with PowerShell Core in Linux

Hands-On Lab

 

Photo of

Training Architect

Length

01:30:00

Difficulty

Intermediate

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.
What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Remote Administration with PowerShell Core in Linux

Introduction

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.

Connecting to the Lab

  1. Log in to the Azure Portal using the credentials provided on the lab instructions page.

Create Two Linux Virtual Machines in the Azure Portal

  1. Click Virtual machines.

  2. Click + Add.

  3. Use the Resource group combo box to select the existing resource group.

  4. In the Virtual machine name box, enter a name of ubuntutest1.

  5. Use the Region combo box to select the (US) West US region.

  6. Under Size, click Change size.

  7. Select B2ms from the list of size options.

  8. Click Select.

  9. Set the Authentication type to Password.

  1. Enter a Username of psadmin in the box provided.

  2. Enter a password in the Password and Confirm password boxes. Remember this password.

  3. At the bottom of the window, click Next: Disks.

  4. Use the OS disk type combo box to select a disk type of Standard HDD.

  5. Click Next: Networking.

  6. Click Next: Management.

  7. Set Boot diagnostics to Off.

  8. Click Review + create.

  9. Click Create.

  10. Click Home in the top-left corner.

  11. Repeat steps 1 - 19 to create a second virtual machine. The only difference is to give the second virtual machine a name of ubuntutest2.

  12. Click Virtual machines and verify the presence of two virtual machines.

Install PowerShell and All Prerequisites on Both Linux Virtual Machines

  1. Click ubuntutest1.

  2. Copy the Public IP address to the clipboard. This will be referred to VM1_PUBLIC_IP for the rest of the lab.

  3. Open Cloud Shell by clicking the button in the title bar.

  4. In the terminal window, click Bash.

  5. If there's a warning about no storage mounted, click Show advanced settings.

  6. Set the Could Shell region to West US.

  7. Under Storage account, select Use existing.

  8. Under File Share, select Create new.

  9. Enter "filesharetest" without quotes into the box under File share.

  1. Click Create storage.

  2. Connect to the first virtual machine using ssh.

    ssh psadmin@VM1_PUBLIC_IP
  3. Enter the password provided during the previous task when setting up the virtual machine.

  4. Download the Microsoft repository GPG keys.

    wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
  5. Register the keys.

    sudo dpkg -i packages-microsoft-prod.deb
  6. Update products.

    sudo apt-get update
  7. Verify repositories are installed.

    sudo add-apt-repository universe
  8. Install PowerShell.

    sudo apt-get install -y powershell
  9. Start PowerShell to verify it works.

    pwsh
  10. Exit PowerShell.

    exit
  11. Exit the virtual machine.

    exit
  12. In the top window for the portal, on the left-hand side, click ubuntutest2.

  13. Copy the Public IP address to the clipboard. This will be referred to VM2_PUBLIC_IP for the rest of the lab.

  14. Repeat steps 11-20 for the second virtual machine, using VM2_PUBLIC_IP.

Install OpenSSH Client and Server on Both VMs

  1. Connect to the first virtual machine using ssh.

    ssh psadmin@VM1_PUBLIC_IP
  2. Install openssh-client on the virtual machine.

    sudo apt install openssh-client
  3. Install openssh-server.

    sudo apt install openssh-server
  4. Edit the sshd_config file.

    sudo vi /etc/ssh/sshd_config
  5. Find the line that starts with Subsystem sftp and add two extra lines after it.

  6. Insert the following line into the newly-created open space.

    Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile
  7. Save the changes and exit the editor.

  8. Restart the service.

    sudo service sshd restart
  9. Exit the first virtual machine.

    exit
  1. Connect to the second virtual machine using ssh.

    ssh psadmin@VM2_PUBLIC_IP
  2. Repeat steps 2-9 on the second virtual machine.

Working with PowerShell SSH Remoting

  1. Log back into the first virtual machine using ssh.

    ssh psadmin@VM1_PUBLIC_IP
  2. Start PowerShell.

    sudo pwsh
  3. Create a new variable.

    $session = NewPSSession -HostName VM2_PUBLIC_IP -UserName psadmin
  4. Provide the password created in the initial task.

  5. Check the value of the $session variable.

    $session
  6. Enter the remote session and note the change in the prompt.

    Enter-PSSession $session
  7. Check our login information.

    uname -a
  8. Exit this session.

    Exit-PSSession
  9. Work with the second VM without logging into the session.

    Invoke-Command $session { Get-Process }
  1. Search for a PowerShell process on the second VM.

    Invoke-Command $session { Get-Process pwsh }
  2. Search for an ssh process on the second VM.

    Invoke-Command $session { Get-Process ssh }
  3. Exit PowerShell and the machine.

    exit
    exit
  4. Log back into the first VM.

    ssh psadmin@VM1_PUBLIC_IP
  5. Start PowerShell.

    pwsh
  6. List the processes.

    Get-Process
  7. Search for specific processes.

    get-process ssh
    get-process sshd
    get-process pwsh
  8. Exit PowerShell and the virtual machine.

    exit
    exit

Conclusion

Congratulations, you've completed this hands-on lab!