Launching a CentOS7 Virtual Machine on Windows10

I recently started a new project, for which I decided I needed a new virtual machine on which to do my development. Here, I share the steps I took to set up the machine.

Prerequisites

Before you start, you’ll need the following software to be installed:

  1. Windows10 OS on your host machine (I have only tried this with Windows10, it may well work with Windows7 or newer versions too)
  2. VirtualBox (for Windows)
  3. PuTTy
  4. PuTTygen

Step 1: Download a Virtual Image

First up, download a virtual image. I used osboxes as they come pre-configured.

  1. On the osboxes web page, scroll down to “CentOS 7.9.2004” (or whichever version of CentOS you want) and make sure the ‘VirtualBox’ tab is selected.
  2. Click the ‘Download’ button next to ‘VirtualBox (VDI) 64bit”.
  3. While the image is downloading, click on the ‘Info’ tab.
  4. Copy all the information under the tab to a local file (it contains the username and password for the virtual machine).

Step 2: Unpack the Virtual Image

Once the image has downloaded:

  1. Copy the zipped directory to somewhere suitable (e.g. a directory called ‘VMs’).
  2. Right click on the zipped directory and extract it.
  3. Then right click again and select Express zip -> Extract to folder.
  4. Inside the folder that is extracted, you should find a file with a .vdi extension. This is the image.

Step 3: Create new Virtual Machine in VirtualBox

  1. Open VirtualBox.
  2. Click the icon labelled ‘New’.
  3. For ‘Name’, enter a name for the virtual machine, e.g. ‘CentOS7’.
  4. For ‘Type’, select ‘Linux’.
  5. For ‘Version’, select ‘Red Hat (64-bit)’.
  6. Click ‘Next’.
  7. Use the slider to select the memory required (or enter it into the box). The amount you need will depend on how much processing the virtual machine is going to be doing. I chose 8192 Gb for mine, as I want it to be able to handle multiple memory-intensive processes.
  8. Click ‘Next’.
  9. For ‘Hard disk’, select ‘Use an existing virtual hard disk file’.
  10. Click the folder icon and navigate to where you saved the virtual machine image (.vdi file).
  11. Select the file and click ‘Open’.
  12. Click ‘Create’.
  13. Start the virtual machine by clicking the green arrow at the top labelled ‘Start’.
  14. A terminal will launch, and the virtual machine will boot up. Once booted, you will be able to use the username and password recorded in Step 1 to login to the GUI.

Step 4: Add Port Forwarding for SSH

Adding port forwarding can enable you to map a port on your host machine (in this case Windows10) to a port on the virtual machine. This is useful for SSH, as we can map a port on the host machine to port 22 (the SSH port) on the virtual machine, and then we can SSH to the virtual machine using the location 127.0.0.1:<host port>.

  1. In VirtualBox, select the virtual machine you just created, from the list on the left-hand side.
  2. Click ‘Settings’.
  3. Click ‘Network’.
  4. Select the tab for ‘Adapter 1’.
  5. Click ‘Advanced’.
  6. Click ‘Port Forwarding’.
  7. Click on the green plus on the top right to add a new forwarder.
  8. Set ‘Name’ to ‘SSH’.
  9. Set ‘Protocol’ to ‘TCP’.
  10. Set ‘Host Port’ to any available port on your host machine. For example, I chose ‘3333’.
  11. Set ‘Guest Port’ to 22.
  12. Click ‘OK’.
  13. Click ‘OK’.

Step 5: Set up PuTTy SSH

  1. Open PuTTy.
  2. Select ‘Session’ on the left-hand side, if not already selected.
  3. For ‘Host Name (or IP address)’, enter ‘osboxes@127.0.0.1’.
  4. For ‘Port’, enter the host port you selected in Step 4.
  5. For ‘Connection type’, select ‘SSH’.
  6. Under ‘Saved Sessions’, type a name for your virtual machine session, e.g. ‘osbox’.
  7. Click ‘Save’.
  8. On the left-hand side, click ‘Window’.
  9. In the box next to ‘Lines of scrollback’, enter ‘10000’. This will give you plenty of output scrollback in your PuTTy terminal window.
  10. Under ‘Window’ on the left-hand side, click ‘Appearance’.
  11. If you wish, you can change the font and font size here. I like to use ‘Lucida Console, 10-point’.
  12. On the left-hand side, select ‘Session’.
  13. Click ‘Save’.
  14. Click ‘Open’.
  15. You may get a pop-up warning to say that the server’s host key is not cached in the registry. That’s OK, because this is a brand new virtual machine that PuTTy has never seen before. Click ‘OK’.
  16. You should see a PuTTy terminal open, and a prompt for your password will appear.
  17. Enter the password you recorded from the info tab in Step 1.
  18. You should now be SSH’d into your virtual machine, straight into the home directory for the default user.
  19. To end the session, you can either click on the ‘X’ on the top right corner, or type ‘Ctrl-d’.

Step 6: Create an SSH key

While you can SSH to your virtual machine using the username and password, it can be more convenient (and secure) to use an SSH key to do this instead.

  1. SSH to your virtual machine by opening PuTTy and selecting the saved session that you created in Step 5. Then click ‘Open’.
  2. Login to the virtual machine using your username and password (from Step 1).
  3. Run:
    $ ssh-keygen 
  4. Hit enter for the key to be saved in the .ssh directory in your home directory.
  5. Enter a passphase if you wish. Or hit enter to create an SSH key without a passphase.
  6. Then run:
    $ cd .ssh 
    $ cat id_rsa 
    
  7. Copy the key by selecting the text and paste it to a new plain text file on your Windows host. Save the file with no file extension.
  8. Then run:
    $ touch authorized_keys
    $ cat id_rsa.pub > authorized_keys
    $ chmod 0600 authorized_keys
    
    The authorized_keys file contains public keys for SSH private keys that are authorised to be used to SSH to the virtual machine for your username.
  9. Right-click on PuTTy in the task bar and select ‘Run PuTTygen’.
  10. Click ‘Conversions’ in top nav.
  11. Click ‘Import key’.
  12. Navigate to where you saved the private key file and select it.
  13. Click ‘Open’.
  14. Enter a passphase (optional – only if you set a passphase on the key when creating it).
  15. Click ‘Save private key’.
  16. Click ‘OK’ if a warning pops up.
  17. Save the PuTTy private key where the original private key file is saved. It will have a ‘.ppk’ extension.
  18. Close PuTTygen.
  19. Right click on PuTTy in task bar and select ‘PuTTy’.
  20. Click on the name for the saved configuration for the new vm (e.g. osbox) and click ‘Load’.
  21. On the left-hand side, click ‘Connection’ -> ‘SSH’ -> ‘Auth’.
  22. Click the box next to ‘Allow agent forwarding’.
  23. Click ‘Browse’ and select the PuTTY key just created. Click ‘Open’ to select the file.
  24. In PuTTY, on the left-hand side, click ‘Session’.
  25. Click ‘Save’.
  26. Click ‘Open’. If successful, the login should now happen with the ssh key. You will need to enter your ssh key passphase if you set one, otherwise it will log you straight in.

Conclusion

You should now have a CentOS7 virtual machine running on a Windows10 host, that you can access either via the VirtualBox terminal, or via SSH using PuTTy and an SSH key.

If you want to find out how to add a static IP and internet access to your virtual machine, read my follow-on post, Launching a CentOS7 Virtual Machine on Windows10: Part 2: Networking.

Thanks for reading!