DEV Community

Prateek Navani
Prateek Navani

Posted on

A simple explanation of what a virtual machine actually is

A virtual machine is a computer that does not exist, running on a computer that does.
That sounds like a riddle, but it is a fair description. If you have ever opened a cloud server, run Windows on a Mac, or used almost any website, you have touched one. This is the plain-language version, with no jargon left unexplained.

What is a virtual machine?

A virtual machine, or VM, is a computer made entirely of software, running inside a real computer. It has its own operating system, its own share of CPU and memory, its own storage and its own network connection. The real machine is called the host, and the VM is called the guest.
Log into one and you cannot tell the difference. There is a disk to format, an operating system to install, an IP address and a login prompt. It behaves like a physical machine because, from the inside, it believes it is one.
How does one physical computer run many of them?
Through a piece of software called a hypervisor. It sits between the hardware and the VMs, divides up the CPU, memory, storage and network, and keeps each VM separate from the others.
There are two main kinds:
Type 1 (bare metal): the hypervisor runs directly on the server's hardware. This is what data centres use, and VMware ESXi, Microsoft Hyper-V and Xen are examples
Type 2 (hosted): the hypervisor runs as an app on top of a normal operating system. This is what you use on a laptop, and VirtualBox is an example

Why do virtual machines exist at all?

To stop servers from sitting mostly empty, and to keep different systems from interfering with each other.
Before virtualisation, one server often ran one application, and most of its capacity went unused. VMs let many isolated systems share one physical machine. The benefits are practical:
Isolation: a crash or a hack in one VM does not spill into the others
Efficiency: one powerful server does the work of many small ones
Flexibility: each VM can run its own operating system, including a different one from the host
Copyability: you can snapshot a VM, clone it or move it, since the whole machine is stored as a file

What can you actually do with a virtual machine?

Quite a lot, and you may already be doing some of it.
Host a website or an application in the cloud
Test software safely, without risking your main computer
Run Windows on a Mac or Linux machine, or the other way round
Keep development, testing and production environments separate
Rent a cloud GPU, since many cloud GPU offerings are VMs with a GPU attached

What is the difference between a VM and a VPS?

A VM is the technology, and a VPS is a VM sold as a hosting product. That is the whole vps vs vm answer in one line: one describes how it works, the other describes how it is packaged and sold.

VM
VPS
What it is
A software-made computer on a hypervisor
A VM rented as a hosting product
Who uses the term
Engineers, cloud platforms
Hosting providers and their customers
What you usually get
A machine you configure yourself
An OS, fixed resources and root access
Typical billing
Often metered by the second, minute or hour
Often a flat monthly price

Is a cloud instance a VM or a VPS?
Technically, a VM. Cloud providers call them instances, cloud servers or virtual servers, and the name changes from company to company. If you can pick its size, install an operating system and log in as an administrator, you are almost certainly looking at a VM, however it is labelled.

How is a VM different from a container?

A VM virtualises the hardware and runs a full operating system. A container shares the host's operating system and packages only the application and what it needs.

Virtual machine
Container
Runs
A full guest operating system
The app, sharing the host's OS kernel
Typical image size
Several GB, commonly 5 to 30GB
Commonly 50 to 200MB
Start-up
Slower
Faster
Isolation
Stronger, enforced by the hypervisor
Lighter, enforced by the OS
Operating system
Any OS per VM
Must match the host's kernel type

Do you have to choose between them?

No, and many teams use both. A common pattern runs containers inside VMs: the VM provides the strong security boundary, and the containers provide fast, tidy packaging for the application.

What are the downsides of a virtual machine?

Nothing is free, and VMs have costs.
They are heavier. Each VM carries a whole operating system, which uses more memory and storage than a container
They start slower than containers, since an operating system has to boot
They are harder to move between platforms. Moving a VM between different hypervisors can need a format conversion
You look after the operating system. Patching, updates and security on the guest are your job

What do the numbers on a VM plan actually mean?

When you pick a VM, you choose a size. The labels are simpler than they look.
vCPU: a virtual processor, usually one thread of a physical core, handed to your VM by the hypervisor
RAM: the memory set aside for your VM while it runs
Disk: the storage attached to it, which often keeps billing even when the VM is off
Bandwidth: how much data can move in and out
A bigger VM is not automatically a better one. Start with the smallest size that runs your workload comfortably, and resize when the numbers say you need to.

How do you create your first virtual machine?

The steps are similar whether you do it on a laptop or in the cloud.
Decide where it will run: a hosted hypervisor on your laptop, or a cloud provider
Pick a size: vCPUs, memory and disk
Choose an operating system image
Start it and connect, through a console or a secure remote login
When you are finished, shut it down or delete it, so you do not pay for something you forgot
The last step is the one people skip. A forgotten VM is the cheapest mistake to make and one of the most common.

Where this leaves you

A virtual machine is a software computer running inside a real one, kept separate by a hypervisor. A VPS is that same thing sold as hosting, and a container is a lighter cousin that skips the full operating system. Once you hold those three ideas apart, most of the cloud vocabulary stops sounding like a foreign language.

A few quick questions people keep asking about this
Is a virtual machine secure?
It is a strong boundary. Escaping from a VM usually requires a flaw in the hypervisor itself, which is rare. You still need to secure what runs inside the VM, since a badly configured guest is just as exposed as a physical server.
Can I run a virtual machine on my own laptop?
Yes. A hosted hypervisor such as VirtualBox runs on an ordinary laptop. You will need enough memory to share between your own system and the VM, and the more you give it, the slower your main machine feels.
Do I need a VM or a container?
If you need a full operating system, strong isolation or a different OS from the host, use a VM. If you are packaging and shipping an application quickly, a container is often lighter. Plenty of setups use both.

Top comments (0)