Site icon Treehouse Blog

How to Build a Linux Server on a Virtual Machine Host

We’re prepping a workshop series for our Pro students on deploying web apps to production, and we wanted a simple, safe way for students to follow along at home, without having to create an account on AWS / DigitalOcean / etc. So we figured out a setup that mimics a Linux server running out on the Internet somewherewhich is where you’d typically deploy web apps tobut uses VirtualBox to run the server as a virtual machine on your development system. This is a great way to try things out because it lets you copy the server to try out different configurations, take snapshots you can roll back to if you make a mistake, and more. These directions are available as a video workshop to our Pro students, but we thought we’d share the basic ingredients here as well so you can experiment with Linux servers yourself!

We’re going to create a virtual machine to run a Linux server on. Think of it as a computer within a computer. Your desktop or laptop is going to be the host system, and we’re going to create a guest system to run Linux on.

1. Download VirtualBox & Ubuntu Server

First we need to download and install VirtualBox itself, followed by a Linux installer.

2. Set Up a Virtual Machine Host

Now we need to create and configure a virtual machine within VirtualBox.

3. Install a Ubuntu Linux Server

Now you have a virtual machine, but its virtual hard drive is empty. There’s no operating system for it to boot with. If it were a physical computer, we’d pop in a CD or other installation media, which would allow the machine to boot and install an operating system to its hard drive. We’re going to do the virtual equivalent of that now.

At this point the installation will be complete! Choose “Continue” to reboot the virtual machine. (There’s no need to “eject” the virtual installation media.) When the virtual machine reboots, it’ll load the Ubuntu OS itself. You’ll be prompted for a login; enter the user name and password you created while installing Ubuntu. You’re now logged in to your new virtual server!

Read also: How to Install Rails 5 on Linux

4. Connect to the Server Via SSH

The window on your screen right now emulates a monitor that’s connected to your virtual machine. What you type on your keyboard emulates a keyboard that’s connected directly to your virtual machine. But to connect to servers out on the Internet, you would use the Secure SHell program, or ssh. ssh connects you to a terminal on a remote computer, and it encrypts everything you do so no one can eavesdrop on the passwords and commands you’re sending. From now on, we’re going to want to connect via SSH. Let’s set that up now.

SSH usually listens for network traffic on port 22, and the SSH on our virtual server will be no different. We can tell VirtualBox to open a port on our local computer, and send all network traffic that it receives on that port, to a port on our virtual server. So we’re going to open port 2222 on our host machine, and forward all traffic to port 22 on our virtual machine. When we use the ssh port to connect to port 2222 on the host, we’ll wind up talking to the SSH service on the virtual machine.

The SSH service may not be installed on your virtual Linux server yet. To install it:

The last step will be to try connecting from your host machine to the virtual machine via SSH. We’re going to direct our SSH client program to connect from our computer, back to port 2222 on that same computer. We can connect to the same computer we’re running on by using the special IP address The traffic will be forwarded to port 22 of our virtual machine, and it should connect.

Readers running Mac or Linux as their host operating systems should already have the ssh client program installed. Open a terminal on your host machine, and run this command (substituting the user name you set up when installing Ubuntu for “yourlogin”):

ssh yourlogin@ -p 2222

Windows users may need to download PuTTY, a free SSH client app. Follow these directions to establish a connection, using “localhost” as the host name, “SSH” as the protocol, and “2222” as the port. You’ll be prompted to enter a user name later, as you log in.

Regardless of whether you’re connecting via the ssh program or PuTTY, you’ll see a warning saying something like “the SSH server isn’t recognized”, which is normal, since this is our first time connecting. Type “yes” to confirm that it’s OK to connect.

Then type the login (if prompted for one) and password that you set up when installing Ubuntu. You should be taken to a system prompt, where you can start running Linux commands to your heart’s content.

Congratulations! You have a virtual Linux server running right there on your computer! The sky’s the limit from here. If you’d like some ideas, Treehouse Pro and Techdegree subscribers can check out our workshops on deploying web apps to production, and check out the Treehouse 7-day free trial. Have fun with your new server!

Start learning to code today with a free trial on Treehouse.

Exit mobile version