Introduction
This article outlines the process of setting up a serial connection between a MacOS host and a Ubuntu Server 20.04 guest virtual machine (VM) using VirtualBox and the socat command. This connection is essential for data transfer between the two systems.
Prerequisites
Before proceeding, ensure the following:
- Both the host and guest systems have a stable internet connection.
- VirtualBox is installed on the host system.
- Ubuntu Server 20.04 is installed as a guest VM using VirtualBox.
Setting up Serial Ports with socat
To create two virtual serial ports, use the following socat command:
socat -d -d -T 10 - < /dev/tty.usbmodem0 38400 & socat -d -d -T 11 < /dev/tty.usbmodem1 38400
This command creates two serial ports, numbered 10 and 11, and assigns them to the first available USB serial port (/dev/tty.usbmodem0) and the second available USB serial port (/dev/tty.usbmodem1), respectively. Both ports are set to a baud rate of 38400.
Configuring VirtualBox
Next, configure the guest VM to use the serial ports:
- Start the Ubuntu Server 20.04 VM in VirtualBox.
- Open the settings for the VM.
- Navigate to the "Serial Ports" tab.
- Add two new serial ports, assigning them the numbers 1 and 2.
- Set the input and output settings for each serial port as follows:
Port number: 1 Device: /dev/ttyS0 Host port: /dev/ttyUSB0 Input settings: None Output settings: None
Port number: 2 Device: /dev/ttyS1 Host port: /dev/ttyUSB1 Input settings: None Output settings: None
Configuring Ubuntu Server 20.04
Finally, configure the Ubuntu Server 20.04 guest system to use the serial ports:
- Log in to the Ubuntu Server 20.04 VM via SSH or the console.
- Install the necessary packages:
sudo apt update
sudo apt install socat
Now, create a systemd service file to start socat at boot:
sudo nano /etc/systemd/system/socat.service
Add the following content to the file:
[Unit] Description=Serial communication with socat After=network-online.target[Service] User=root WorkingDirectory=/ ExecStart=/usr/bin/socat - UNIX-CONNECT:/dev/ttyS0:38400,raw,echo=0 STDIN,raw,echo=0:11:38400,raw,echo=0 Restart=on-failure
[Install] WantedBy=multi-user.target
Save and close the file.
Reload the systemd daemon:
sudo systemctl daemon-reload
Start the socat service:
sudo systemctl start socat
Check the status of the socat service:
sudo systemctl status socat
This article outlined the process of setting up a serial connection between a MacOS host and a Ubuntu Server 20.04 guest VM using VirtualBox and the socat command. By following the steps outlined in this article, you can create two virtual serial ports, configure VirtualBox and Ubuntu Server 20.04 to use these ports, and establish a reliable serial connection between the two systems.