Skip to content

Instantly share code, notes, and snippets.

@dipaish
Last active November 21, 2024 23:52
Show Gist options
  • Save dipaish/2d73b102a7eb389e7681e486ea4d83db to your computer and use it in GitHub Desktop.
Save dipaish/2d73b102a7eb389e7681e486ea4d83db to your computer and use it in GitHub Desktop.
Virtualization & Docker Basics

Virtualization Basics - Tasks

Install Virtual Box

  1. You can either run choco install virtualbox -y (for Windows users) or download the installer directly from the VirtualBox website.
  2. Select the appropriate installer file based on your operating system (Windows or macOS) and follow the instructions to complete the installation.

Creating a Virtual Machine

  1. Obtain an Operating System Image

    • To create a virtual machine, you need an image of an operating system. An image is a file containing the operating system's complete contents including its configuration and file system. In this case, we will use the Ubuntu Desktop image.
  2. Download Ubuntu Desktop Image

    • Download the Desktop Install Image for Ubuntu 22.04 LTS. You can get the image from the following link: Ubuntu 22.04 LTS (Jammy Jellyfish).
    • Click on "Download Ubuntu 22.04 LTS (Jammy Jellyfish)" to download the file.
  3. Set Up the Virtual Machine
    Using the downloaded Ubuntu image, set up a new virtual machine with the following configuration:

    • Name: Firstname_Lastname_UbuntuSer
    • Base Memory (RAM): 2048 MB
    • Storage:
      • Type: Normal (VDI)
      • Virtual Size: 20 GB
      • Allocation: Dynamically allocated differencing storage
  4. Do the User management or package management tasks on this virtual machine.

Install Guest Additions in Ubuntu

  1. Update the package list and install updates by running
sudo apt update && sudo apt upgrade -y
  1. Before installing Guest Additions, you need to install some required packages
sudo apt install build-essential dkms linux-headers-$(uname -r) -y
  1. Insert Guest Additions CD Image

    1. In the VirtualBox menu, go to Devices -> Insert Guest Additions CD Image.
    2. If asked to download the ISO, confirm the download.
  2. Mount and Install Guest Additions

    1. If the CD image doesn’t auto-mount, you can manually mount it:
    sudo mount /dev/cdrom /mnt
    1. Navigate to the mounted directory:
    cd /mnt
    1. Run the Guest Additions installer:
    sudo sh VBoxLinuxAdditions.run
  3. Reboot the system

    sudo reboot
  4. Verify the installation of guest additions

After reboot, you should see improved screen resolution, clipboard sharing and seamless mouse integration which verifies the installation of guest additions.

Docker Basics Tasks

Note: Make sure that your Docker Desktop is running.

  1. Open Visual Studio Code & install the WSL extensions and Docker extension in the Visual Studio Code.
  2. Do the following tasks using Visual Studio Code:
    1. Use Docker run command to run a docker container based on hello-world image
    2. Use Docker run command to run a docker container based on alpine image
    3. Use Docker run command to run a docker container based on alpine image and get access to the container shell.
      1. Use mkdir command to make some directories.
      2. Use exit command to exit from the container’s shell.
    4. Run a static website in a container using an existing image “static-site”.
      1. docker run --name static-site-2 -e AUTHOR="Your Name" -d -p 8888:80 dockersamples/static-site
      2. Type localhost:8888 in your browser.
    5. List all images on your system.
    6. List all containers that are currently running in your system
    7. List all containers both running & the ones that are stopped
    8. Stop one of your running containers.
    9. Remove one of your containers.

Understanding SSH Keys

SSH (Secure Shell) keys are a pair of cryptographic keys used to authenticate and secure connections between computers. They provide a more secure alternative to password-based authentication. They are typically used to establish a secure connection between a client and a server. SSH keys come in pairs: a public key (stored on the server/device that you want to connect to) and a private key (kept by the client). When connecting, the server verifies that the private key matches the stored public key.

Generate SSH key pairs

You don't need to install any additional tools to generate SSH keys in PowerShell on a Windows machine. Windows 10 and Windows Server 2019 and later come with a built-in SSH client and key generation tool called ssh-keygen.To generate SSH key pairs and connect from the host machine (Windows) to the guest operating system(Ubuntu) running on VirtualBox, follow the steps below:

  1. Open PowerShell and generate SSH Key pair.
ssh-keygen -t ed25519
  • ssh-keygen: It is the command to generate SSH keys.
  • -t ed25519: ED25519 is a public-key signature system that is fast and secure. It is considered more efficient and secure than older algorithms like RSA or DSA.
  1. You can set a passphrase (Optional).You can set a passphrase for extra security. If you choose to set a passphrase, you will need to enter it every time you use the private key.

After completing the above steps, you will have a pair of keys.

By default, the generated key pairs will be saved in your user profile directory under ~/.ssh (e.g., C:\Users\YourUsername.ssh). You will have two keys:

  • The private key will be in the file id_ed25519. The private key is a secret and it must be kept secure. It is used to decrypt messages or authenticate connections.
  • The public key will be in id_ed25519.pub. The public key is designed to be shared. It's used by others to verify the authenticity of messages signed with the corresponding private key.

SSH from Host Machine to Ubuntu VM

Step 1: Install openssh-server in the Ubuntu VM.

sudo apt update
sudo apt install -y openssh-server

*Verify that the SSH service is running:

sudo systemctl status ssh

If the SSH service is not active, start and enable it:

sudo systemctl start ssh
sudo systemctl enable ssh

Step 2: Set the VirtualBox network to Bridge Mode

  1. Shut down your Ubuntu VM if it's running. 2.Open VirtualBox and select your Ubuntu VM.
  2. Click on Settings.
  3. Navigate to the Network section.
  4. In the Adapter tab, select Bridged Adapter from the dropdown list.
  5. Choose your host machine's active network interface from the Name dropdown.
  6. Click OK to save the changes.

Note: Bridged Mode allows the VM to appear as a separate device on the same network as your host, making it easier to connect via SSH.

Step 3: Find the VM's IP Address

ip a

hostname -I

Step 4: SSH from Host Machine to Ubuntu VM

  • Replace username with your Ubuntu VM username.
  • If prompted, type yes to accept the SSH key fingerprint,

To login to a remote machine using SSH key pair

You ned to copy or upload your public key from a host machine to the remote machine. In this case your host machine is Windows 11 and the remote machine is Ubuntu running on virtual box. Before running the following command, check the IP address of your remote machine (Ubuntu in this case).

Step 1: Copy your public key to the remote machine using scp command.

scp C:\path\to\your\public\id_ed25519.pub username@ubuntu_ip_address:/home/username/

Step 2: Log in to your Ubuntu guest machine via SSH

ssh username@ubuntu_ip_address

Step 3: Append the public key to the authorized_keys file:

Note: It might be that the ~/.ssh folder does not exist. In that case, please create the ~/.ssh folder

mkdir ~/.ssh
cat ~/id_ed25519.pub >> ~/.ssh/authorized_keys

Step 4: Login to the SSH server using a public key for authentication

You will use the ssh command with the -i option to specify the path to your private key.

ssh -i /path/to/private/privatekey username@ubuntu_ip_address

Visual Studio Code Remote Explorer

This extension for Visual Studio Code simplifies remote machine usage. You can create a list of your remote machines and easily choose the one you want to connect to. You need to install the following extensions:

  • Remote Explorer
  • Remote SSH

Follow the steps below to add a remote machine

  1. To add a remote machine, click on the "Open a Remote Window Icon" and select Connect to Host (Remote SSH)
  2. Select Configure SSH Hosts.. and select C:\Users\username.ssh\config. In this file, you will add your remote machine info.
Host ubuntuVM
    HostName 192.168.8.143
    User deepakkc
    IdentityFile C:\Users\deepak1\.ssh\deep

  • Host ubuntuVM: It is a label used to identify the remote machine you want to connect to. It is a nickname for the remote machine.
  • HostName 192.168.8.143: This is the ip address of the remote machine you want to connect to.
  • User deepakkc: It specifies the name of the user that should be used when connecting to the remote machine.
  • IdentityFile C:\Users\deepak1.ssh\id_ed25519 : This line points to the location of the private key file to be used for authentication when connecting to the remote host. In this case, the private key file is located at "C:\Users\deepak1.ssh\deep." The SSH client will use this private key for authentication, assuming the corresponding public key has been added to the authorized_keys file on the remote host.
  1. After you have configured your SSH host, Click on the "Open a Remote Window Icon" and select Connect to Host (Remote SSH), you will get to see the list of your remote machines (Labels), select the one that you want to connect to. In this case, you will select ubuntuVM .
@clovisngu
Copy link

done

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