socat (3/3)
The last part of socat: virtual serial ports (PTY + TERMIOS), TLS done properly (OPENSSL group), retries, TUN tunnels, POSIX message queues, value types, environment variables, signals and the helper tools. Lab: srv 192.168.66.10, cli 192.168.66.20.
46.1 Where it runs
| Platform | Install |
|---|---|
| Linux | apt / dnf install socat |
| macOS | brew install socat |
| Windows | Not built in: WSL or Docker |
46.2 Virtual serial port (PTY group)
- Step 1
1.Old tool on the laptop only opens a serial port
- Step 2
2.socat PTY,link=/tmp/vserial0 creates a virtual serial port (/dev/pts/0)
- Step 3
3.Data from it travels over TCP to console server 192.168.66.10:4001
- Step 4
4.Console server sends it down the serial cable to router R1
- Step 5
5.R1's reply ('R1 uptime 12 days') comes back on the same path
- link=path: fixed name for the pseudo terminal.
- wait-slave: do not open the TCP side until a program opens the port (first ss output is empty).
- sitout-eio=30: wait 30 s when the port closes; pty-interval: poll interval of wait-slave.
socat -d -d PTY,link=/tmp/vserial0,rawer,b9600,wait-slave,sitout-eio=30 TCP:192.168.66.10:4001 &ls -l /tmp/vserial0ss -tn dst 192.168.66.10(echo 'show version'; sleep 1) | socat -t 1 - /tmp/vserial0,b9600,rawerss -tn dst 192.168.66.10$ ls -l /tmp/vserial0lrwxrwxrwx 1 root root 10 Nov 8 18:13 /tmp/vserial0 -> /dev/pts/0$ ss -tn dst 192.168.66.10State Recv-Q Send-Q Local Address:Port Peer Address:Port$ (echo 'show version'; sleep 1) | socat -t 1 - /tmp/vserial0,b9600,rawerR1 console readyR1# show versionR1 uptime 12 days$ ss -tn dst 192.168.66.10State Recv-Q Send-Q Local Address:Port Peer Address:PortESTAB 0 0 192.168.66.20:56676 192.168.66.10:400146.3 TERMIOS options (serial line settings)
| Option | What it does |
|---|---|
| b9600 / b115200 | Line speed (baud); ispeed= ospeed= separately |
| rawer / cfmakeraw | Raw mode, echo off (raw is obsolete) |
| echo=0 icanon=0 | Echo and line mode off |
| cs8 parenb cstopb | Data bits, parity, stop bits |
| crtscts / ixon ixoff | Hardware / software flow control |
| icrnl onlcr opost | CR and LF translation |
| vmin= vtime= | When a read returns |
| iuclc / olcuc / xcase | Old upper/lower case conversions |
$ stty -F /tmp/vserial0speed 9600 baud; line = 0;-brkint -icrnl -imaxbel-opost -onlcr-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke- Only a few of the 87 TERMIOS options are needed in practice: speed, rawer, and data bits/parity/flow control from the device manual.
- On real hardware the same options go on /dev/ttyUSB0 (Linux) or /dev/cu.usbserial (macOS).
- If your terminal behaves strangely afterwards, run stty sane.
46.4 TLS with the OPENSSL group
- Step 1
1.cli socat OPENSSL:192.168.66.10:4443 sends ClientHello
- Step 2
2.srv (OPENSSL-LISTEN) sends its certificate: CN srv.lab, self-signed
- Step 3
3.Check 1: signed by a trusted CA? (cafile=)
- Step 4
4.Check 2: name matches? (commonname=srv.lab)
- Step 5
5.Both pass: encrypted data flows (TLSv1.3, TLS_AES_256_GCM_SHA384)
socat - OPENSSL:192.168.66.10:4443socat - OPENSSL:192.168.66.10:4443,cafile=/tmp/srv.crtsocat - OPENSSL:192.168.66.10:4443,cafile=/tmp/srv.crt,commonname=srv.labsocat -d -d - OPENSSL:192.168.66.10:4443,cafile=/tmp/srv.crt,commonname=srv.labsocat - OPENSSL:192.168.66.10:4443,verify=0socat - OPENSSL:192.168.66.10:4444,verify=0,min-version=TLS1.3$ socat - OPENSSL:192.168.66.10:4443!2026/11/08 18:13:35 socat[65] E SSL_connect(): error:0A000086:SSL routines::certificate verify failedexit=1$ socat - OPENSSL:192.168.66.10:4443,cafile=/tmp/srv.crt!2026/11/08 18:13:37 socat[72] E certificate is valid but its commonName does not match hostname "192.168.66.10"exit=1$ socat - OPENSSL:192.168.66.10:4443,cafile=/tmp/srv.crt,commonname=srv.labhello over TLSexit=0$ socat -d -d - OPENSSL:...,cafile=/tmp/srv.crt,commonname=srv.lab2026/11/08 18:13:41 socat[86] N SSL proto version used: TLSv1.32026/11/08 18:13:41 socat[86] N SSL connection using TLS_AES_256_GCM_SHA384$ socat - OPENSSL:192.168.66.10:4444,verify=0,min-version=TLS1.3!2026/11/08 18:13:44 socat[102] E SSL_connect(): error:0A00042E:SSL routines::tlsv1 alert protocol versionexit=1- verify=1 is the default; verify=0 also delivered data but checks nothing: testing only.
- Port 4444 allows at most TLS 1.2, so min-version=TLS1.3 fails the handshake.
- The PDF's min-proto-version is an unknown option in socat 1.8 (real name: openssl-min-proto-version, alias min-version).
- cert= key= give the server identity; cafile= capath= set trust; snihost= / no-sni=1 control SNI; cipher= and compress= are best left default; method= is deprecated.
46.5 RETRY group
- retry=3: three more attempts after the first; interval=1: one second between them.
- forever: keep retrying until the server comes up.
- Not inherited by forked children.
$ time socat - TCP:192.168.66.10:5000,retry=3,interval=1!2026/11/08 18:13:51 socat[118] E connect(5, AF=2 192.168.66.10:5000, 16): Connection refusedreal 0m3.011sexit=1$ time socat - TCP:192.168.66.10:5000,forever,interval=1finally upreal 0m2.018sexit=046.6 TUN tunnel and INTERFACE options
- Step 1
1.Both sides: TUN address creates lab0 (cli 10.255.0.2, srv 10.255.0.1)
- Step 2
2.cli pings 10.255.0.1: ICMP goes into lab0
- Step 3
3.socat wraps it in UDP 192.168.66.20 -> 192.168.66.10:11443
- Step 4
4.srv socat unwraps it into its lab0
- Step 5
5.Echo reply returns on the same path
# srvsocat UDP4-LISTEN:11443 TUN:10.255.0.1/24,tun-name=lab0,iff-up,iff-no-pi &# clisocat UDP4:192.168.66.10:11443 TUN:10.255.0.2/24,tun-name=lab0,iff-up,iff-no-pi &ping -c 3 10.255.0.1# srvtcpdump -n -i eth0 udp port 11443tcpdump -n -i lab0 icmpcli$ ping -c 3 10.255.0.164 bytes from 10.255.0.1: icmp_seq=1 ttl=64 time=0.815 ms3 packets transmitted, 3 received, 0% packet loss, time 2033mssrv$ tcpdump -n -i eth0 udp port 1144318:14:13.163542 IP 192.168.66.20.42654 > 192.168.66.10.11443: UDP, length 84srv$ tcpdump -n -i lab0 icmp18:14:13.163706 IP 10.255.0.2 > 10.255.0.1: ICMP echo request, id 54906, seq 1, length 64srv$ socat -d -d UDP-LISTEN:11444 TUN:10.255.0.1/24,tun-name=lab0,iff-up,iff-no-pi!2026/11/08 18:23:23 socat[25] E xioopen_ipdgram_listen(): unknown address family 0- tun-name names the interface, iff-up brings it up, iff-no-pi removes the extra PI header: both ends need the same values.
- Real finding: in socat 1.8 plain UDP-LISTEN gave 'unknown address family 0', so write UDP4-LISTEN.
- tun-type=tun carries layer-3 IP packets, tun-type=tap carries layer-2 Ethernet frames.
- INTERFACE flags: iff-promisc (stayed on only while socat ran), iff-allmulti, iff-noarp, iff-pointopoint; retrieve-vlan puts the VLAN tag back.
46.7 POSIX message queues (experimental)
echo 'config saved' | socat --experimental -u - POSIXMQ-SEND:/n046q,posixmq-priority=1echo 'link down' | socat --experimental -u - POSIXMQ-SEND:/n046q,posixmq-priority=9cat /dev/mqueue/n046qsocat --experimental -u POSIXMQ-RECEIVE:/n046q - # run twice$ cat /dev/mqueue/n046qQSIZE:23 NOTIFY:0 SIGNO:0 NOTIFY_PID:0$ socat --experimental -u POSIXMQ-RECEIVE:/n046q -config saved$ socat --experimental -u POSIXMQ-RECEIVE:/n046q -link downNormally the higher-priority message should come first, but in this socat version 'config saved' (priority 1) came first.
46.8 Aliases and value types
| Written | Means |
|---|---|
| TCP-L | TCP-LISTEN |
| TCP4-CONNECT | TCP4 |
| sp= | sourceport= |
| crlf | crnl |
| FILE / create | OPEN / creat |
| SSL | OPENSSL |
| ttl=0x40 | 64 (hex) |
| ttl=010 | 8 (octal, leading zero) |
| -T 1.5 | 1.5 seconds (time values are decimal seconds) |
$ echo hi | socat -t 1 - TCP4-CONNECT:192.168.66.10:7777,sp=40001,crlfport 400010000000 h i \r \n0000004srv$ grep -o 'ttl [0-9]*' /tmp/ttl.txtttl 64ttl 8cli$ time socat -T 1.5 SYSTEM:'sleep 5' TCP:192.168.66.10:7779real 0m1.508s- VSOCK CID is a 32-bit context id: -1U = any CID (for bind), 2 = the host. VSOCK-CONNECT:2:1234 from inside a VM reaches the host.
46.9 Environment variables, exit status, signals, helpers
- EXEC/SYSTEM children get SOCAT_VERSION, SOCAT_PID, SOCAT_PEERADDR, SOCAT_PEERPORT, SOCAT_SOCKADDR, SOCAT_SOCKPORT.
- Exit status 0 on success, 1 on an error like Connection refused.
- kill -USR1 on a running socat logs the transfer statistics so far.
- filan shows a process's open FDs (inside socat fd 1 is a unix socket); procan prints process id, parent, group, user ids.
$ socat - TCP:192.168.66.10:7000; echo $?SOCAT_VERSION=1.8.0.0SOCAT_PID=169 SOCAT_PPID=163SOCAT_PEERADDR=192.168.66.20 SOCAT_PEERPORT=36222SOCAT_SOCKADDR=192.168.66.10 SOCAT_SOCKPORT=7000exit=0$ kill -USR1 $(pgrep -n -x socat)2026/11/08 18:15:13 socat[280] I STATISTICS: left to right: 63971 packets(s), 524050432 byte(s)$ socat -u SYSTEM:'filan -s' - 0 chrdev /dev/null 1 unix <anon> 2 pipe46.10 Comparison: socat vs netcat
| Point | netcat | socat |
|---|---|---|
| Model | One socket <-> your terminal | Any address <-> any address |
| Addresses | TCP / UDP (Unix) | TCP, UDP, TLS, serial/PTY, TUN, files, programs, MQ |
| Options per address | No | Yes (retry, termios, ttl, range...) |
| Best for | Quick tests | Tunnels, gateways, serial bridges |
46.11 Common mistakes
- Writing ttl=010 and expecting 10: a leading zero means octal, so TTL is 8.
- Using verify=0 in production: encryption without authentication, a man-in-the-middle is accepted.
- Connecting by IP to a cert issued for a name: add commonname=.
- Plain UDP-LISTEN with TUN in socat 1.8: use UDP4-LISTEN.
46.12 Interview tips
- socat vs netcat: netcat is simple TCP/UDP read-write; socat relays two generic addresses with options.
- Secure TLS test: OPENSSL:host:port,cafile=...,commonname=...,min-version=TLS1.2.
- Virtual serial port: PTY,link=/tmp/vserial0,rawer,b9600 + TCP to a console server.
- TUN over UDP gives a simple layer-3 tunnel; tap would carry layer-2 frames.