Setting up COM port forwarding with VirtualBox
As a VirtualBox user, you might wonder how to configure virtual serial ports on VirtualBox and if there’s a native method to do this.
Basically, you can use VirtualBox to add virtual COM ports to your guest OS. In the native scenario, once you enable a virtual serial port, your guest operating system is presented with a UART device allowing the reception and transmission of serial data. All you need to do is configure VirtualBox serial port passthrough. However, it should be mentioned that the configuration procedure largely depends on which OS your host runs.
To create a VirtualBox virtual serial port, you can either use the VBoxManage command or the Settings tab. But keep in mind that one of these methods lets you configure no more than four virtual COM interfaces for each guest OS.
In view of this, you will need to adjust the Port Mode and Port Number, where the Port Number is the COM port seen by the VM and the Port Mode is the COM port status. It can be “Disconnected”, “Connected to Host Device”, or “Connected to Host Pipe”:
Disconnected indicates that the device can be recognized by the guest system but will work like no cable is connected.
Connected to Host Device means that the VM’s port is linked to the COM port residing on the host. In Windows, this port will traditionally have a name like COM1. On Linux, it can be called, for example, /dev/ttyS0.
In this case, all data going through the virtual port will be forwarded by VirtualBox to the physical port and device attached to it.
Connected to Host Pipe is used when the Oracle VM VirtualBox is set to establish a connection between the virtual COM port and one of the host’s software pipes.
Depending on the host operating system this means the following:
On Windows, the data is transmitted over a named pipe. The pipe name is registered as \\.\pipe\ with serving for the machine identification.
On macOS, it will be a local domain socket. As for the socket filename, it should be chosen to allow the VirtualBox user to have certain privileges for creating and writing it. Commonly the tmp is chosen.
On Linux, some multiple tools connect to a local domain socket or develop one in the server mode. Socat is a widely used one which is available across distributions.
If you want to create a direct link between two guest systems, you should consider configuring one of them to develop a socket or pipe and the other one to connect to it.
Raw File: The virtual serial port data can be saved to a file, which is especially helpful for diagnostic purposes. The user can select any file if they have enough privileges to write and create.
TCP Socket: This is used when there's a need to redirect serial data over the TCP/IP protocol. It can take on the role of a client or act as a server. With this option, you can connect directly to a remote computer from a guest OS over TCP.
TCP Server: to use this option, you need to uncheck the current Pipe/Socket checkbox and provide the port number in the address field. You can select 23 or 2023 as the port number. But if you use a UNIX-like system be sure to choose the port number that is more than 2024.
TCP Client: when it’s required to establish a virtual null-modem connection over LAN or the Internet, on the other end there should be a connection over TCP with specified “hostname: port” in the Path/Address field. After you check the box "Connect to Existing Pipe/Socket", the TCP socket will be able to work as a client.
As is seen from this guide, you can configure four COM ports for each guest OS. As for port numbers, you can select multiple ones.
In this brief tutorial, we’ve considered the simplest and most common ways of using VirtualBox to share COM ports and connect to the host’s devices. As you can see, with the dedicated software Serial to Ethernet Connector, you can easily access local peripherals in your VirtualBox virtual machine like they were attached directly to the guest system.