Sunday, 11 September 2011

How to share files instantly between virtual machines and host

It's pretty common to need to copy files between a virtual machine and the host. This can be drivers or installers from the host into the virtual machine or it could be in order to get some data out of the virtual machine and onto the host.

There are several solutions to sharing files, like network file systems, third-party file hosting, or even the virtfs paravirtualized file system supported by KVM. But my favorite ad-hoc file sharing tool is Python's built-in webserver.

The one-liner that shares files over HTTP

To share some files, simply change into the directory subtree you want to export and start the web server:

$ cd path/to/shared/files && python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

The directory hierarchy at path/to/shared/files is now available over HTTP on all IPs for the machine on port 8000. The web server generates index listings for directories so it is easy to browse.

To access the host from a virtual machine:

  • User networking (slirp): http://10.0.2.2:8000/
  • NAT networking: the default gateway from ip route show | grep ^default, for example http://192.168.122.1:8000/
  • Bridged or routed networking: the IP address of the host

To access the virtual machine from the host:

  • NAT, bridged, or routed networking: the virtual machine's IP address
  • User networking (slirp): forward a port with the hostfwd_add QEMU monitor command or -net user,hostfwd= option documented in the QEMU man page

Advantages

There are a couple of reasons why I like Python's built-in web server:

  • No need to install software since Linux and Mac typically already have Python installed.
  • No privileges are required to run the web server.
  • Works for both physical and virtual machines.
  • HTTP is supported by desktop file managers, browsers, and the command-line with wget or curl.
  • Network booting and installation is possible straight from the web server. Be warned that RHEL installs over HTTP have been known to fail with SimpleHTTPServer but I have not encountered problems with other software.

Security tips

This handy HTTP server is only suitable for trusted networks for a couple of reasons:

  • Encryption is not supported so data travels in plain text and can be tampered with in flight.
  • The directory listing feature means you should only export directory subtrees that contain no sensitive data.
  • SimpleHTTPServer is not a production web server and there could be obvious bugs.

Conclusion

Python's built-in web server is one of my favorite tricks that not many people seem to know. I hope this handy command comes in useful to you!

8 comments:

  1. Nice article. I have been using it for sometime. Really helpful when you want to share something quick. One tip which i wan to share is, for some reason port 8000 not available, you can specify a custom port like the following,

    -- python -m SimpleHTTPServer 8080

    ReplyDelete
  2. when i start SimpleHTTPServer on guest, then i use firefox to access the file in guest. it does not work. On the contrary, it can work.

    zwu

    ReplyDelete
  3. zwu: It sounds like your networking configuration prevents firefox on the host from connecting to SimpleHTTPServer in the guest. Or did you forget to include the port number in the URL (http://my-vm:8000/)?

    Please post more details if you need help.

    ReplyDelete
  4. Great ! , I spent a day in figuring out how I can share some files between host and guest. For some reason samba was not working for me. This worked like a charm and possibilities of this is beyond just guest and host. Thanks for sharing.

    ReplyDelete
  5. Very handy guide.
    I was looking for something like this.

    Thank You for Posting

    Peoplecnc

    ReplyDelete
  6. Thank you so much! I deleted dnsmasq-base on host, so ran a live CD in virtualbox, downloaded packages I needed, started this webserver, copied the files and I am back in business. Thank you so much for this tip!

    ReplyDelete
  7. It helped me in 2016, so thank you very much for that.

    ReplyDelete
  8. I can't figure out what is keeping my virtual machine from accessing this port. Ping works from the virtual machine and I can access the files locally on the host using the web browser.

    ReplyDelete