Work With Your Virtual Machine

This is step 4 of 7 of Hands-On Project 1 for both Azure and AWS. In this step, you do some routine work with your VM, such as displaying machine characteristics, updating software packages, and starting and stopping the virtual machine.

Back to Project

For this exercise, we assume that you are connected to your virtual machine via SSH and have a terminal window open.

Run a Few Linux Commands to Get Acquainted

When you create a brand new virtual machine, it’s useful to check out what is installed on the machine. It is likely that you will be adding software to configure the machine for a specific purpose. So, let’s start by running a few Linux commands.

How much memory is installed, and how much is used?:

free -h --si

How many CPU cores do we have and what are other properties of the CPU?

lscpu

What storage devices do we have and how much storage is available?

df -h

Which version of the operating system we are running:

cat /etc/os-release

What processes are running right now? (after this, use Ctrl-C to terminate)

top

Check out what environment variables we have in the terminal, and then look specifically at the PATH variable:

printenv
printenv PATH

Check that we have the ability to set a PATH variable at login (Ctrl-X to exit without saving)

cd ~
nano .bashrc

It is very common to have to add directories to the PATH environment variable, and nano is an easy way to get it done.

Install and Maintain Software with apt

Try looking at the IP address of our virtual machine:

ifconfig

Oops! The image we used to create the virtual machine does not include the ifconfig command. But, we do get a clue how to install it:

Enter the apt package manager, which is one of two common package managers for Ubuntu Linux. The other one is apt-get, which is older and actually more complete. We will be using apt in this series because it does everything that we need.

The software for Ubuntu Linux systems is constantly being updated, with new versions of packages added to internet repositories daily. To completely maintain your Ubuntu system, you want to update your installed packages frequently. Likewise, when you want to install a new package, it’s a good idea to make sure that all the packages currently installed are up to date. This can help steer clear of dependency/prerequisite problems when you install new packages. Here is the command sequence that just about everyone is using:

Update your cached package list with the latest version available for all known package repositories:

sudo apt update

Note that the sudo command elevates your user to the root user and executes the command that comes after it. Most of the commands related to the apt package manager require elevation to root in order to successfully execute.

If a large number of packages are out of date, upgrade the versions of all installed packages to the latest version:

sudo apt upgrade

Note that this does not install any new packages – it just brings the ones you have up to date. If you are prompted, reply yes to the prompt.

Now let’s install the net-tools package:

sudo apt install net-tools

If your are prompted, reply yes to the prompt. Note that you can include -y on the install command to automatically reply yes to the prompt:

sudo apt install -y net-tools

Now we can query our IP address (more precisely, we are showing the configuration of all the network interfaces on the virtual machine):

ifconfig

Let’s do one more install, just for practice. Check out the version of Python that got installed with the base operating system:

python -V

Oops! On Ubuntu Linux, the base Python install is version-specific (Python 2 vs. Python 3). It’s nice to have a command called ‘python’ so you don’t need to specify the version when you run it. Luckily, Ubuntu has a nice package called python-is-python3 that patches this issue:

sudo apt install python-is-python3

Now we can check which version of Python is installed with the command we tried before:

python -V

Stopping and Restarting Your Virtual Machine/EC2 Instance

Whenever your VM is running, you are incurring the full charges for the use of it. You can significantly cut your expenses if you stop the Vm when you are not using it. When you stop the virtual machine, you will not be charged for the VM itself, but you will still be charged for the disk, and on AWS the Elastic IP address. The disk and IP address charges are quite low however, and if you consistently stop and restart your VMs, you can create larger, more expensive VMs and still not go broke. As an example, the minimum-sized VM on your Azure for Students account has 2 cores, 8GB RAM, and costs $70 per month. But that is still only about 10 cents per hour. If you only use the VM a few hours at a time, you can keep the VM alive and fully configured, and use what you need for well under $10 per month.

To stop your virtual machine:

  1. On Azure, navigate to Portal Home –> Virtual Machines. Select the VM at the left, then click on Stop at the top. Notice that after the VM stops, the Status becomes Stopped (deallocated). Deallocate means that you are not being charged the full rate for use of the virtual machine.
  2. On AWS, navigate to Console Home –> EC2 –> Instances. Select the instance at the left, then select Instance State –> Stop Instance to stop it. Note that if you associated an Elastic IP address to the instance, that same IP address will be used when you restart.