Saturday, April 9, 2022

Ubuntu/Linux/Hyper-V: Creating an Ubuntu 20.04 VM can be accessed from Hyper-V's Host Computer

I was assigned a project where a shell script had to be run remotely on an Ubunto 20.04 Virtual Machine (VM). The remote script failed on an Azure VM so I fell back on Hyper-V running on Windows 11. The way I traditionally configured an Ubuntu 20.04 VM did not permit SSH access from the host computer. These are the steps required to set up an Ubuntu 20.04 that can be configured to accept SSH connections from the host computer.

Create VM (Basic Ubuntu Install)

Create VM pre-installing Ubuntu from an Image

1) Download the Ubuntu 20.04 LTS from Download Ubuntu Desktop on the host computer. The format of the image download will be ISO.

2) Launch Hyper-V Manager on the host computer and make sure that the host computer name is selected in the left panel:

3) Select Action | New| Virtual Machine:

4)  Navigate click Next in the New Virtual Machine Wizard under the Specify Name and Location panel is displayed:

5) Enter the name of the virtual machine to be created (the name above is devops02-ubuntu20.04) and click on Next:

By default, Generation 2 VMs cannot use an ISO operating system image for installation. This will be addressed at a later step in the installation process.

6) From the Specify Generation panel select the Generation 2 radio button and click Next:

7) From the Configure Networking panel select Bridge (which allows external access to say the internet and provides access to the internal network, a.k.a. the LAN) which will enable access to the VM from the host computer (the VM will be assigned a local IP address) and click Next:

8) Configure the hard drives and click Next:

9) Specify the location of the Ubuntu install media and click on next:

10) Click Finish on the final setup screen:

Configure Ubuntu to Install from ISO

1) From Hyper-V Manager highlight the VM being setup and from the Actions panel on the right click on Settings:

2) From Settings dialog select Hardware | Security from the pane on the left:

3) Click on OK.

Installing Ubuntu on the VM

1) From Hyper-V Manager, double click on the VM to be set up in order to establish a connection to the VM (opens the connection dialog):

2) From the per-VM connection dialog click on Start:

3) Clicking on Start displays the standard Ubuntu install wizard.

4) Click on Continue and perform the steps required to complete the installation of Ubuntu.

Verify Static IP Address

The previous steps ensured that a VM was assigned a static IP address. In order to verify that this static IP address was assigned, run ifconfig from a terminal on the VM. The installation of ifconfig is presented in the post, Ubuntu/Linux: Retrieving a Computer's IP Address using ifconfig). 

Below is a screenshot showing the output from ifconfig when run from the VM:

The output from running ifconfig (above) shows a static IP address has been assigned to the VM.

Saturday, April 2, 2022

Ubuntu/Linux: Configuring a host to support ssh Connections (Installing OpenSSH Server)

By default, an Ubuntu 20.04 instance does not contain an SSH server and hence there is no ssh access to the machine. To remedy this, install OpenSSH Server by:

1) Install the OpenSSH Server package:

sudo apt-get install -y openssh-server

2) Enable the service using systemctl enable:

sudo systemctl enable ssh

3) Start the service using the systemctrl start command:

sudo systemctl start ssh

4) On a different host, verify that the port for ssh (port 22) is open using the technique described in Ubuntu/Linux: How to tell if a Port is Open on a Remote Host:

echo > /dev/tcp/ && echo "Port 22 (SSH) is open!"

When the above command is invoked and OpenSSH has been installed successfully the following is displayed:

Remember the above command is run on the client and not the host (remote computer) where OpenSSH server will run.

Ubuntu/Linux: How to tell if a Port is Open on a Remote Host

While trying to get ssh working to a remote host, I encountered the "Connection refused" error message on Ubuntu.  The first step in debugging is to make sure the port is open on the remote host which can be done as follows:

echo > /dev/tcp/<remote host>/<port> && echo "The port is open"

Checking to see if ssh (port 20) is open on a host ( is simply:

To verify script snippet works I did tested it against HTTPS (port 443) on a well know host (

Ubuntu/Linux: Retrieving a Computer's IP Address using ifconfig

Consider the case where a developer wants to ssh from a host computer to a Ubuntu client running in Hyper-V. The most fundamental way to access the client machine via ssh is using its IP address. Linux's ifconfig utility is short for interface config.

The ifconfig utility is part of the net-tools package. To install this package on Ubutu use apt-get as follows:

sudo apt-get update -y
sudo apt-get install -y net-tools

An example of ifconfig being invoked is as follows where the computer's IP is shown to be 10.0.37:

Ubuntu/Linux: Disable sudo Password Prompt

When running scripts as sudo, it is convenient not to be prompted for a user's password. Password prompts defeat automation so this post will demonstrate how to disable the password prompt associated with the sudo command.

The command sudo date is often used to determine if invoking sudo will prompt the user to enter their password (as follows):

Above, it can clearly be seen how the sudo before the date command causes a password prompt for the password of user devops. 

The sudo command allows commands to be elevated and run as root. In order to use the sudo command, a user must be part of the sudo group. The following post demonstrates how to add a user to the sudo group: Ubuntu: Adding a user to the sudo group.

To disable the sudo caused password prompt invoke the command

sudo visudo

Running the above command does require a user to enter their password in response to sudo (hopefully for the last time):

The visudo command is an editor for the /etc/sudoers file. visudo validates the sudoers file's format on save. The man page for the sodoers file can be found at Sudoers Manual.

visudo brings the sudoers file up in an editor:

To suppress the password for a specif member of the sudo group, add the following text under the line %sudo  ALL=(ALL:ALL) ALL:

<user name here> ALL=(ALL) NOPASSWD: ALL

For the case of a user named, devops, the following line would be added in the editor:


The reason for putting the new line after the line containing %sudo  ALL=(ALL:ALL) ALL is explained in the sodoer man page (referenced above):

The sudoer file being edited by visudo will look as follows:

Use ctrl-s to save the file and cttrl-x to exit the editor.

Once the sudoer file has been edited correctly and saved, a user should no longer be prompted to enter a password when invoking the sudo command.

Ubuntu/Linux: Adding a user to the sudo group

Members of the sudo group can execute any command as root user using the sudo command:

sudo date

The command above invokes the date command with root privileges.

In order to add a user to the sudo group, invoke the following from a terminal:

sudo usermod -aG sudo <username>

The leading sudo runs the usermod command as root. Depending on how a user is configured, the sudo command may prompt the user to enter a password.

To add a user named, devops, to the sudo group invoke the following command:

sudo usermod -aG sudo devops