VirtualBox – GreyAvatar https://greyavatar.info #Minimalist Thu, 16 Feb 2023 14:30:50 +0000 en hourly 1 https://wordpress.org/?v=7.0.2 Install VirtualBox on Ubuntu 22.04 https://greyavatar.info/install-virtualbox-on-ubuntu-22-04/ Thu, 16 Feb 2023 14:30:50 +0000 https://greyavatar.info/?p=128 Download VirtualBox’s Repo GPG Key

wget -O- https://www.virtualbox.org/download/oracle_vbox_2016.asc | sudo gpg --dearmor --yes --output /usr/share/keyrings/oracle-virtualbox-2016.gpg

Add VirtualBox Repo to Ubuntu 22.04

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/oracle-virtualbox-2016.gpg] http://download.virtualbox.org/virtualbox/debian $(lsb_release -cs) contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list

Run System Update

sudo apt update

Check the output of the following command, and look for … download.virtualbox …
If everything is correct, you should find a line in the output containing a string like the one I have made in bold.

Install VirtualBox
sudo apt install virtualbox-7.0
Install VirtualBox Extension Pack. To verify the exact version of the installed locally VirtualBox, you can use vboxmanage, a build-in VirtualBox’s command:
vboxmanage -v | cut -dr -f1

You must then download the Extension Pack with the same version.

wget https://download.virtualbox.org/virtualbox/7.0.6/Oracle_VM_VirtualBox_Extension_Pack-7.0.6.vbox-extpack

Install the extension

sudo vboxmanage extpack install Oracle_VM_VirtualBox_Extension_Pack-7.0.6.vbox-extpack

verify the installed VirtualBox’s extension pack version by running the following:

vboxmanage list extpacks

Add User to vboxusers Group

sudo usermod -a -G vboxusers $USER

Now perform a reboot. After login, check that you are in the vboxusers group with this command:

groups $USER

]]>
Cron to Automate Tasks https://greyavatar.info/cron-to-automate-tasks/ Mon, 30 Jan 2023 08:49:41 +0000 https://greyavatar.info/?p=85 Install Cron

sudo apt update

sudo apt install cron

You’ll need to make sure that cron will run in the background too:

sudo systemctl enable cron

Cron jobs are recorded and managed in a particular file known as crontab. Each user profile on the system can have its crontab where they can schedule jobs, which is stored under /var/spool/cron/crontabs/

To schedule a job, open your crontab for editing and add a task written as a cron expression.

minute hour day_of_month month day_of_week command_to_run

*: In cron expressions, an asterisk is a wildcard variable representing “all.”

Referring to a previous post (VirtualBox Virtual Machine Backup on Ubuntu Server Host), I’m going to list the three lines to automate the backup.

0 22 * * * /path/to/vm-stop.sh >/dev/null 2>&1
0 0 * * * /path/to/vm-export.sh >/dev/null 2>&1
0 2 * * * /path/to/vm-start.sh >/dev/null 2>&1

>/dev/null 2>&1: It means that stderr (2 – containing error messages from the executed command or script) is redirected (>&) to stdout (1 – the output of the command) and that the latter is being redirected to /dev/null (the null device). This way, you can suppress all messages issued by the executed command. In cron, this is often done to avoid being spammed by lots of irrelevant messages from service scripts.

Those expressions should be put in a crontab. A crontab is a particular file that holds the schedule of jobs cron will run. However, these are not intended to be edited directly. Instead, it’s recommended that you use the crontab command.

crontab -e

When you save the file is stored in the folder /var/spool/cron/crontabs/, so save it without changing the default path in the editor.

If you’d like to view the contents of your crontab, but not edit it, you can use the following command:

crontab -l

You can erase your crontab with the following command:

crontab -r

]]>
VirtualBox Virtual Machine Backup on Ubuntu Server Host https://greyavatar.info/virtualbox-virtual-machine-backup-on-ubuntu-server-host/ Mon, 30 Jan 2023 08:24:26 +0000 https://greyavatar.info/?p=78 This post will explain how to back up the VMs on an Ubuntu server. The goal is to make a cheap and straightforward system usable on a noncritical mission. Two old servers were dismissed at my working place, so I got the opportunity to prepare a testing environment for my colleague and a didactic exercise.

I’ll prepare three different scripts, one to turn off the VMs, one to run the backup, and one to start them before the next working day. The mission is not critical because the VMs are not used between 11 pm and 6 am of the next working day.

The VMs must be prepared to accept SSH connections from the host using a public/private key pair.

The first thing I need is to know the name of the VMs on the system. To do so, I use the command:

vboxmanage list vms

The name will be in quotes, as in “Ubuntu Server 20.04” or “VM01”

You want to back up VM01 to a .ova file and house it in an external drive mounted on /data. That command would be:

VBoxManage export "VM01" -o /data

Before you issue the command, the machine must be powered off.

The best way to do that is to power off the VM from within the guest. Because using the VBoxManage power-off command could cause data loss, which we want to avoid at all costs.

How do you pull this off? You have to do so via an SSH session like so:

ssh -t USER@HOST sudo poweroff

Where USER is the name of the remote admin user and HOST is the IP address of the VirtualBox guest VM. Of course, to make this work via a script, you’ll need to set up SSH key authentication. Make sure to copy your SSH key from the host to the guest with the command:

ssh-copy-id USER@HOST

The first script will power off the machine. Create the new script with the command:

nano vm-stop.sh

In that file, paste the following:

#!/bin/bash

ssh -t USER@HOST sudo poweroff

Where USER is the name of the remote admin user and HOST is the IP address of the VirtualBox guest VM.

If you need to send a passphrase automatically, you can write in the script something like this:

shpass -P passphrase -p your_passphrase ssh -p port -t USER@HOST sudo poweroff

Next, we’ll create the script to export the VM with the command:

nano vm-export.sh

In that file, paste the following

#!/bin/bash

today=$(date +"%Y-%m-%d")

VBoxManage export "VM01" -o /data/VM01-${today}.ova

The above command will append today’s date to the file name so that you won’t overwrite your previously exported OVAs.

Finally, we’ll create a script to start the virtual machine with the command:

nano vm-start.sh

In that file, paste the following contents (altering the contents to fit your needs):

!/bin/bash

VBoxManage startvm "VM01" --type headless

Give each of these files executable permissions with the commands:

chmod u+x vm-stop.sh

chmod u+x vm-export.sh

chmod u+x vm-start.sh

Should disaster strike, you can always import the last successful export of VM01 with a command like:

VBoxManage import /data/VM01-2023-4-6.ova

Now those three scripts can be run automatically with cron jobs. I’ll discuss automation in another post.

]]>
SSH Server Kali https://greyavatar.info/ssh-server-kali/ Fri, 27 Jan 2023 08:17:54 +0000 https://greyavatar.info/?p=71 In this post, I’ll share how to install the server ssh on a Kali machine and connect using a key pair from another.

This post is part of a series where I will set up a host with Oracle VirtualBox and automatically backup the VM. Connecting to the VM using ssh will be necessary to turn them off before the backup starts.

Let’s start

Install the server ssh on Kali.

sudo apt-get install ssh

Start the server ssh.

sudo service ssh start

Now test the connection using the ssh command ssh -p <port>. Because of the specific situation where I’m going to connect to the VM from the host, I don’t use the standard port but the port I configured for the NAT on VirtualBox.

I backup the default key as a precaution, but I will not use them to avoid MITM attacks.

mkdir /etc/ssh/default_keys

mv /etc/ssh/ssh_host_* /etc/ssh/default_keys/

After you have moved the default key you have to generate a new pair.

dpkg-reconfigure openssh-server

I verify that the hashes of the new keys differ from the hashes of the default ones. I run the command below from the /etc/ssh folder and /etc/ssh/default_keys

md5sum ssh_host_*

The next step is to edit the SSH server configuration file with the settings you need

nano /etc/ssh/sshd_config

Inside the file modify the two lines below and remove the #.

PasswordAuthentication no

PubkeyAuthentication yes

Now the commands to manage the server’s status, the service has to start at the boot of the VM.

If you only need to start the SSH service temporarily, use ssh.socket:

systemctl start ssh.socket

systemctl stop ssh.socket

To instead permanently enable the SSH service to start whenever the system is booted, use the following:

systemctl enable ssh.service

Then to use SSH immediately without having to reboot use:

systemctl start ssh.service

To check the status of the service, you can use the following:

systemctl status ssh.service

To stop the SSH service, you can use the following:

systemctl stop ssh.service

And to disable the SSH service, so it no longer starts at boot:

systemctl disable ssh.service

]]>