Skip to content

Instantly share code, notes, and snippets.

@snb
Created January 24, 2010 01:46
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • 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

VirtualBox headless with serial console

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 of 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

TODO

Enable serial console in Linux

These instructions apply at least 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

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

Useful VBoxManage commands

Start headless

VBoxManage startvm lenny -type headless

If rebooting the VM is necessary

VBoxManage controlvm lenny 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 lenny "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/Protocol" TCP
VBoxManage setextradata lenny "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/GuestPort" 22
VBoxManage setextradata lenny "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/HostPort" 2222

Sources

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