Skip to content

Instantly share code, notes, and snippets.

@snb
Created January 24, 2010 01:46
Show Gist options
  • Save snb/284940 to your computer and use it in GitHub Desktop.
Save snb/284940 to your computer and use it in GitHub Desktop.
virtualbox serial console

Setting up VirtualBox to run Debian and FreeBSD VMs headlessly, with the system console accessible over a virtual serial port.

Configure VirtualBox

First set up a serial port in VirtualBox. In the Details section for the virtual machine, click Serial Ports, check Enable Serial Port, set Port Mode to Host Pipe, check Create Pipe and put e.g. /tmp/freebsd8vm-socket in Port/File Path.

Screen shot from VirtualBox serial ports configuration

Now start the virtual machine. Use socat to connect the domain socket created by VirtualBox to a pty.

socat UNIX-CONNECT:/tmp/freebsd8vm-socket PTY,link=/tmp/freebsd8vm-pty &

It should be possible to now connect to the serial port with screen. If you disconnect the screen session the pty will be removed, so the above socat command will need to be run again.

screen /tmp/freebsd8vm-pty

Enable serial console in FreeBSD

This worked for me on FreeBSD 8.0-RELEASE. It should work similarly on other releases, but device names might change or something.

Tell the boot loader to use the serial console, which also makes the kernel do so. To make the console more responsive, we tell it to use a higher speed connection than the default 9600bps.

echo 'console="comconsole"' >> /boot/loader.conf
echo 'comconsole_speed="38400"' >> /boot/loader.conf

Edit /etc/ttys and change off to on, dialup to vt100, and the speed to 38400 on the entry for ttyu0. The relevant line should look like this after editing.

ttyu0   "/usr/libexec/getty std.38400"  vt100   on  secure

Reboot for /etc/ttys and /boot/loader.conf be re-read.

Enable serial console in Linux

These instructions apply to Debian Lenny, but probably will at least mostly work on other Linux systems as well.

Uncomment/modify this line in /etc/inittab for serial console.

T0:23:respawn:/sbin/getty -L ttyS0 38400 vt100

Tell init to re-read inittab, which will make the serial console start working immediately.

telinit q

Now we have to tell grub and the kernel (via grub) to use a serial console as well. Add the following to /boot/grub/menu.lst.

# Serial console
serial --unit=0 --speed=38400 --word=8 --parity=no --stop=1
terminal --timeout=10 serial console

Then find the kernel boot options line, which should look like this

# kopt=root=/dev/hda1 ro

and add arguments to use a serial console.

# kopt=root=/dev/hda1 ro console=tty0 console=ttyS0,38400n8

Now update the Debian automagic kernel sections

update-grub

I also added noapic to the kernel options to prevent the system from periodically freezing at boot

# kopt=root=/dev/hda1 ro console=tty0 console=ttyS0,38400n8 noapic

It will be necessary to reboot so you can see that grub and the kernel are using serial.

Useful VBoxManage commands

Start the virtual machine from the command line, with no graphical UI

VBoxManage startvm freebsd8 -type headless

If rebooting the VM is necessary

VBoxManage controlvm freebsd8 reset

It is handy to be able to ssh to the VM. When using the default NAT network configuration for VirtualBox, the only way to make network connections from the host to the guest system is with port forwarding. The following commands will forward port 2222 on the host to 22 on the guest VM. Unfortunately this makes the host listen on 0.0.0.0, and I have not found a way to make it only listen locally.

VBoxManage setextradata freebsd8 "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/Protocol" TCP
VBoxManage setextradata freebsd8 "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/GuestPort" 22
VBoxManage setextradata freebsd8 "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/HostPort" 2222

References

http://www.freebsd.org/doc/handbook/serialconsole-setup.html http://www.howtoforge.com/setting_up_a_serial_console http://download.virtualbox.org/virtualbox/3.1.2/UserManual.pdf

@vinod85
Copy link

vinod85 commented May 7, 2014

Thanks, It worked for me.

@guineawheek
Copy link

If you want only local SSH access, you can add a network adapter to the VM and set it to use a 'Host-only Adapter.', and set a local virtual network interface to use. Under File -> Preferences -> Network -> Host-Only Network, you can fiddle with the virtual interfaces and enable a DHCP server. Now when the VM starts up, you can SSH into it by accessing it by its host-only adapter IP address without exposing it to the network.

@NickHibma
Copy link

Great tutorial. Thanks for taking the time to write this up! I used the UNIX domain socket directly through socat

socat -,icanon=0,echo=0,isig=0 $VM_UART1_PIPE

but you cannot get out of that without killall socat from a different terminal. With your solution I can hide that fact somewhat.

Question: With VBox 5 I seem to no longer be able to boot up FreeBSD with only a serial console, disabling boot messages on VGA completely. It gets stuck right after starting rc, halfway through an echo command strangely enough. Any experience with that?

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