First things to do on a fresh virtualbox guest

This is for a Debian 9 system; might also work on newer versions and Ubuntu.

Installing ‘guest additions’

You need to install the ‘build essentials’ first:

apt-get -y install build-essential module-assistant
m-a prepare

‘Insert’ the guest addtions CD. The I usually copy the contents over into the root directory and run it from there.

mount /media/cdrom0
cp -r /media/cdrom0 /root/
cd /root/cdrom0
./VBoxLinuxAdditions.run

Setup access to the ‘shared’ directory

The user needs to be in the ‘vboxsf’ group:

usermod -a -G vboxsf [USERNAME]

I also usually create a symlink in the user’s home directory for simplicity:

ln -s /media/sf_[SHARE NAME] /home/[USERNAME]/[SHARE NAME]

Here’s everything wrapped up in a script:

#!/bin/bash
if [[ $EUID -ne 0 ]]
then
	echo "This script must run as root user"
	exit 1
fi

echo "Installing stuff needed for virtualbox guest additions installation"

apt-get -y install build-essential module-assistant
m-a prepare

echo "Setting up permissions for shared directory"
echo "Shared directory name: (just hit return to skip this step if you don't have one)"
read dirname

if [[ ! -z "$dirname" ]]
then
	echo "Your username:"
	read username
	
	echo "Adding user to vboxsf group"
	usermod -a -G vboxsf $username
	
	link=/home/$username/$dirname
	if [[ ! -L ${link} ]]
	then
		echo "Creating symlink $link to shared directory"
		ln -s /media/sf_$dirname $link
		chown $username:$username $link
	else
		echo "$link already exists."
fi

echo "Installing vbox guest additions (if CD is inserted)"
if mount /media/cdrom0
then
	cp -r /media/cdrom0 /root/
	cd /root/cdrom0
	./VBoxLinuxAdditions.run
	cd ..
	rm -rf /root/cdrom0
	echo "Finished. Hit return to reboot."
	read xxx
	shutdown -r now
else
	echo "vbox guest additions CD not found."
	echo "Finished."