Skip to content

Instantly share code, notes, and snippets.

@nouredd2
Last active August 3, 2023 19:40
Show Gist options
  • Save nouredd2/be9373f3b6a6b573a0293005eae9cf0a to your computer and use it in GitHub Desktop.
Save nouredd2/be9373f3b6a6b573a0293005eae9cf0a to your computer and use it in GitHub Desktop.
Introduction to ssh X forwarding and kvm

Introduction to ssh X forwarding and kvm

The goal of this gist is to provide an introduction to setting up port forwarding on your machine, and create a virtual machine on the loki server. To start off, let's make it easier for us to access the server using some ssh configuration tricks.

The contents of this documents are:

Changing ssh config for easy access

ssh can be configured to use specific options and credentials when accessing a specific server. This will be helpful in our case since it will set up an alias for the full server name and create a shorthand we can use to initiate a session.

On your own machine, from a terminal window, edit the file ~/.ssh/config (create the file if it is not already there). When dealing with files in Unix systems, the directory ~/ stands for your home directory, which should be something like /home/your_username. Add the following lines to the config file:

Host loki
  HostName loki.perform.illinois.edu
  User achsu3

Save the file and exit, the next time you need to login to the server, you can directly use:

ssh loki

and it will drop you right into a shell on the server (assuming authentication works).

Enabling X forwarding

Enabling X forwarding is a way for us to gain GUI access to the server when on a remote connection. The server is already configured to perform this, so we only need to setup the local machine.

  • Download and install XQuartz on your MacOs machine.

    • At the end of the installation you will have an XQuartz application installed, which you can use to launch a small set of applications.
  • After installation is complete, start a new terminal window and instead of using just ssh loki, use

ssh -Y loki

The -Y flag will tell ssh to enable X forwarding for this session. To test things out, on the remote session, try to launch the clock application using

xclock

It might take a while the first time, but you should see a clock application appear on your screen. For this project, we are interested in the virtual machines manager called virt-manager, so to launch it use

virt-manager

and the virtual machine management interface should appear on your screen.

Creating and Installing a virtual machine

On the loki server, we use virt-manager to manager virtual machine. In what follows, we will download the Ubuntu server image, create a new virtual machine and install Ubuntu on that machine.

Download the Ubuntu server image

Let's start by creating a directory to store the images we're going to use for the virtual machines.

mkdir -p ~/myimages

This will create a new directory under your home directory. Then let's download the latest version of the Ubuntu server there

cd myimages
wget http://releases.ubuntu.com/18.04.1/ubuntu-18.04.1-live-server-amd64.iso

After the download completes, you should have a copy of the Ubuntu 18.04.1 server image in that directory. You can check out the contents of the directory using

ls

Creating a new virtual machine

Let's go ahead and create our new virtual machine. First launch the manager using

virt-manager

Select File > New Virtual Machine or click on the new virtual machine icon (the machine with a yellow thingy on it) and the new machine menu will pop up.

  • From that menu, select Local install media and click Forward.
  • On the Use ISO image bullet, click the Browse button next to it.
    • This will drop you into the Choose Storage Volume menu.
  • Click on the green + button on the lower left corner to create a new storage pool.
    • Choose a name for the storage pool, let's assume it's perform for now, and keep the type as dir and then move Forward to the next menu.
    • On the next menu, set the Target Path to the location in which you downloaded the server image, using the instructions above, that should be /home/achsu3/myimages/. You can also use the Browse button to look it up using the GUI.
    • Once you hit finish, you will have an extra menu item added on the left hand side that will contain the new storage pool we created called perform.
      • If you click on that one, you should see your server image pop up.
      • Select that one and click Choose Volume.
  • You'll be back to the first menu then, select Forward to move on to creating the machine.
  • Next up, we'll select the memory and CPU options for the machine,
    • Let's go with 2 GB of RAM and 4 CPUs, so that should be 2048 for Memory (RAM) and 4 for CPUs.
  • Next we will select the size of the machine's disk. Let's keep the default value of 20 GB, but instead of the default Create a disk image for the virtual machine option, select the second bullet Select or create custom storage and click Manage.
    • This will drop us back to the same menu we were using to choose the server image.
    • Again, select the perform storage pool that we created initially and then click on the green + icon next to the Volumes label on the right hand side.
    • Create a new volume by entering its name from, keep the format to the default qcow2 and click Finish.
    • Choose the new volume and click Forward.
    • Edit the name of the virtual machine in the final step to whatever is easy and click on Finish.
  • The virtual machine will automatically start and drop you into the Ubuntu install menu. It should be easy sailing from now. Follow the on screen instructions to install Ubuntu, it should not take long before you have access to the new virtual machine. The nice thing about virtual machines is that if you don't like what you are doing or if something goes wrong, you can always blow the machine off and create a new one with no repercussions on the server (well except loosing any data you had on that machine, but that should not be an issue in our case).

First thing to do after installation

The first thing we want to do after installing the virtual machine is to make sure that the ssh server on that machine is installed and running. After the installation completes and you are logged into the virtual machine, use

sudo apt update
sudo apt install -y openssh-server

The outcome of this command will either install the ssh server or tell you that it is already installed and running. After that ssh should be up and running the virtual machine. Note that this machine can only be accessed from the loki server for now. There are ways we can play with the ssh configuration to allow us access to the virtual machine from anywhere loki can be accessed, but that is a story for a different time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment