3

I've been using Virtualbox for a while now, but the only way I've learned to interact with it is through the graphical interface.

Is there a way for me to send bash commands to the vm through the commandline so that I can write scripts from the host that interact with the guest?

e.g.

$ sendMessage vmName "echo hello"

I am willing to try new virtualization software.

I am currently on a Mac and I would like to run a Linux guest this way.

Edit:

I want to run possibly malicious code to see its behavior. Ideally, I want to disable networking on the guest so that I can be sure the guest is completely isolated.

I was hoping the guest and the host could communicate purely through stdin/stdout.

2
  • What exactly are you trying to accomplish? Would an ssh server running on the guest do what you want?
    – jw013
    Feb 15, 2012 at 1:03
  • What are you trying to do that isn't possible by just SSHing to the VM from your host? Feb 15, 2012 at 1:04

3 Answers 3

2

Using shared filesystems

You can use VirtualBox's support for exposting a host directory within the guest. Put the code you want to test inside your shared directly, boot the VM, and use the console to navigate to the shared directory and run the code. You don't need any networking, the exposure to your host is only in that one shared directory, and you can use snapshots to revert any changes to the guest filesystem.

Using a virtual serial port

You can configure a guest serial port so that you can interact with the guest from the host without any networking configured.

In VirtualBox, go to Settings -> Ports, enable the first serial port, and set the port mode to "Host Pipe" (and make sure "Create Pipe" is checked). Enter a path in the appropriate field (e.g., /tmp/hostserial). This path is the Unix socket that will be exposed on your host.

Boot your guest. Now your job is to get something inside the guest talking to the seiral port. Try this:

agetty -l /bin/bash -n ttyS0 115200 vt100

This will start up a bash shell on ttyS0, the first serial port. On your OS X host, make sure you have netcat installed and try this:

nc -U /tmp/hostserial

You'll find yourself talking to bash on your guest. This isn't exactly what you want, but maybe it's helpful anyway.

I'd go with the first option using a shared directory, myself.

1

You don't need any special tools, or a different VM system to do this. ssh already does what you want:

$ ssh vmName "echo hello"

Substitute the VM's host name or IP for vmName.

If you use a host name, it will have to be resolvable somehow. If the guest has a static IP it's easiest to add an entry for it in /etc/hosts. If your guest uses DHCP, I recommend switching to dnsmasq for the DHCP server if you aren't running that already, because it also acts as a DNS server and automatically builds DNS entries for every DHCP lease.

You will also want to set up key-based SSH authentication so you don't need to type the password every time.

Now, that particular command won't do much that's useful. You'll just see hello echoed to the host's terminal, just as if you'd typed echo hello locally. To see what's going on, say something like this instead:

$ ssh vmName 'echo $HOSTNAME'

(Note the use of single quotes to prevent the local shell from interpolating HOSTNAME before running the command.)

ssh is very powerful. You can do all kinds of tricks this way, such as piping data from a local process to the remote machine through the SSH tunnel and redirecting the remote program's output to the local machine.

0

I have a Arch Linux virtualbox server running on my main server at home. I start it headless (no graphics head) and then ssh into the box from my laptop or the main server.

nohup vboxheadless --startvm ArchLinux >/tmp/ArchLinux.headless.out 2>&1 &

I've set up the VM to boot and automatically set up the networking. You can also set up a VNC listener to connect using that. But I find ssh more convenient for sending commands to the VM.

2
  • That sounds pretty cool, and it might end up being what I do. I don't have much experience with ssh, but the vm can tell the difference right? i.e. a process running under root on the host would be able tell if a command was sent through ssh or typed in directly?
    – math4tots
    Feb 15, 2012 at 1:29
  • There are environment variables that are set by sshd, such as SSH_CLIENT, SSH_CONNECTION and SSH_TTY where you can tell that it's a ssh connection. Beyond that, there is no difference. How I've done it most consistently is to set up a small DNS zone for my home network and assigned IP addresses for each of my hosts. Then I don't have to worry about new IPs from DHCP that are not set in /etc/hosts or ~/.ssh/config (my router doesn't resolve reverse DHCP addresses, IP->hostname). Any host on the network can get access to the VM guest, including my wife's macbook.
    – Arcege
    Feb 15, 2012 at 2:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .