Henry Ryder, under CC License Attribution

Pushing a Serial Port Over Your Network

Using nothing but socat and a text editor in Linux you too can bounce a serial connection over your local network! For White Hat Wednesday we’ll explain how.

If you’ll be setting up a persistent connection you’ll either need inittab for a classical System V initialization or upstart’s settings files in /etc/init. You can find an explanation of socat on System V inittab at ServerFault and Upstart’s generic instructions on the Upstart website.

What you’ll be adding to the files will be a socat command to bounce the current hardware serial port to a TCP/IP port hosted on whichever system it’s connected to. At that point it should be reachable from anything on the same network.

It bears saying that you shouldn’t be making this available to the wider Internet in any way, shape, or form and the system will require a firewall or NAT network blocking the ports. There are security certificates you can use but they’re beyond the scope of this brief explanation.

On the server side a working command (for port ttyS5) would be:
socat tcp-l:54322,reuseaddr,fork file:/dev/ttyS5,nonblock,waitlock=/var/run/ttyS5.lock

On the client side a working command (to create connected port ttyV5) would be:
socat pty,link=/dev/ttyV5,waitslave tcp:modem-server:54322
Remember to remove “modem-server” on that line and replace it with your actual target server’s (the machine you set up the previous command on) IP address or name.

Those commands will take ttyS5 from your server and abstract it over the intervening network to ttyV5 on your client system. This works equally well to set up virtual machines with uncooperative serial port hardware.