SkillPilotAI

socat (1/3)

socat (SOcket CAT) always opens two addresses and relays bytes between them in both directions. An address can be a TCP/UDP port, a file, a program, a TLS session, a UNIX socket or a TUN device, so a relay, gateway or tunnel is a single command.

44.1 What it does, where it runs

PlatformHow to get it
Linuxapt install socat / dnf install socat
macOSbrew install socat
WindowsNot native: WSL or Docker

44.2 How it works: Address 1 and Address 2

socat TCP relay (lab 192.168.64.0/24)
  1. 1.Relay 192.168.64.5 runs: TCP4-LISTEN:8080,reuseaddr,fork TCP4:192.168.64.20:80

  2. 2.Client 192.168.64.10 sends GET / to the relay on port 8080

  3. 3.fork: a child process handles this client

  4. 4.Child opens a new TCP connection to web server 192.168.64.20:80 and copies the bytes

  5. 5.200 OK comes back to the relay, which delivers it to the client

  6. 6.Web server only sees the relay's IP: 'web sees 192.168.64.5'

44.3 Address syntax

Output
TYPE:param:param,option,option=value  TCP-LISTEN:8080,reuseaddr,fork  TCP:192.168.64.20:80-              STDIO (keyboard + screen)3              number = FD:3/tmp/a.txt     starts with / = GOPENSTDIN!!STDOUT  dual: read!!write'SYSTEM:echo "a,b"'  quotes protect : , !!keywords and options: case-insensitive

44.4 TCP relay (port forward)

  • TCP4-LISTEN accepts only IPv4; TCP6-LISTEN only IPv6.
  • reuseaddr: no 'address in use' error on restart.
  • fork: a separate child process for every client (without it socat serves one client and exits).
  • ACCEPT-FD uses a socket that systemd already opened.
Terminal
# relay 192.168.64.5socat -d -d -lp relay \  TCP4-LISTEN:8080,reuseaddr,fork \  TCP4:192.168.64.20:80ss -ltnp | grep 8080# client 192.168.64.10curl -s http://192.168.64.5:8080
Output
$ ss -ltnp | grep 8080LISTEN 0      5            0.0.0.0:8080       0.0.0.0:*    users:(("socat",pid=8,fd=5))$ curl -s http://192.168.64.5:8080web sees 192.168.64.5

44.5 Logging and help

OptionWhat it does
-VVersion and compiled features (1.8.0.0 in the lab)
-h / -hh / -hhhHelp, options, aliases with groups
-d0 ... -d4 (-dddd)From only errors up to full debug
-lf file / -ly / -lsLog to a file / syslog / stderr (default)
-lp nameProgram name in the log
-lu / -lhMicrosecond timestamps / hostname in the log
-DFD info before transfer
Output
$ socat -V | head -2socat by Gerhard Rieger and contributors - see www.dest-unreach.orgsocat version 1.8.0.0 on 16 Nov 2023 13:03:44$ cat /tmp/relay.log2026/11/08 17:55:26 relay[8] N listening on AF=2 0.0.0.0:80802026/11/08 17:55:31 relay[8] N accepting connection from AF=2 192.168.64.10:45594 on AF=2 192.168.64.5:80802026/11/08 17:55:31 relay[8] N forked off child process 222026/11/08 17:55:31 relay[22] N opening connection to AF=2 192.168.64.20:802026/11/08 17:55:31 relay[22] N successfully connected from local address AF=2 192.168.64.5:481782026/11/08 17:55:31 relay[22] N starting data transfer loop with FDs [6,6] and [5,5]2026/11/08 17:55:31 relay[22] N socket 2 (fd 5) is at EOF2026/11/08 17:55:31 relay[22] N exiting with status 0

44.6 Data dumps and transfer behaviour

  • -v: copy the traffic as text to stderr; > block = left to right (client request), < block = right to left (response). See traffic without tcpdump.
  • -x: the same as hex.
  • -u: unidirectional (left to right only); -U: reverse direction.
  • -T 2: idle timeout, important for UDP because UDP has no EOF (lab: exit after 2.00 s).
  • -t: wait after EOF (0.5 s default); -b: block size (8192); --statistics: count bytes/packets; -r / -R: raw dump to a file.
Terminal
socat -v TCP4-LISTEN:8081,reuseaddr TCP4:192.168.64.20:80echo hello | socat -x - TCP4:192.168.64.20:7777# web: receive onlysocat -u TCP4-LISTEN:9000,reuseaddr CREATE:/tmp/upload.txt# client: send onlyecho 'report v1' | socat -u - TCP4:192.168.64.20:9000
Output
$ cat /tmp/v.log> 2026/11/08 17:55:40.000682712  length=80 from=0 to=79GET / HTTP/1.1\rHost: 192.168.64.5:8081\rUser-Agent: curl/8.7.1\rAccept: */*\r\r< 2026/11/08 17:55:40.000705031  length=39 from=0 to=38HTTP/1.0 200 OK web sees 192.168.64.5$ echo hello | socat -x - TCP4:192.168.64.20:7777> 2026/11/08 17:55:49.000660461  length=6 from=0 to=5 68 65 6c 6c 6f 0ahello< 2026/11/08 17:55:49.000673069  length=6 from=0 to=5 68 65 6c 6c 6f 0aweb$ cat /tmp/upload.txtreport v1

44.7 Files, pipes and programs

AddressWhat it is
CREATE:file / OPEN:file / GOPEN:fileNew file (write) / open() a file or device / file or UNIX socket
PIPEUnnamed pipe: writes back what it reads = echo server
PIPE:nameNamed pipe (FIFO)
STDIO or -Keyboard and screen (fd 0 and 1)
FD:3, STDIN, STDOUT, STDERRAlready open file descriptors
READLINELine editing + history
PTY,link=pathVirtual serial port
EXEC:cmd / SYSTEM:cmd / SHELL:cmdNo shell (execvp) / via sh -c / via $SHELL
Terminal
socat TCP4-LISTEN:7777,reuseaddr,fork PIPEsocat TCP4-LISTEN:13000,reuseaddr,fork EXEC:datesocat TCP4-LISTEN:7000,reuseaddr,fork SYSTEM:'tr a-z A-Z'# clientsocat - TCP4:192.168.64.20:13000echo hello socat | socat - TCP4:192.168.64.20:7000
Output
$ socat - TCP4:192.168.64.20:13000Sun Nov  8 17:56:15 UTC 2026$ echo hello socat | socat - TCP4:192.168.64.20:7000HELLO SOCAT

44.8 UDP, SCTP, DCCP, VSOCK

  • UDP-RECV: merges packets from all senders (syslog-style collector).
  • UDP-RECVFROM: replies to each sender; UDP-SENDTO: send to one fixed peer; UDP-LISTEN: first packet, then connect; UDP-DATAGRAM: broadcast/multicast.
  • Every type has 4 and 6 versions; UDPLITE-* use UDP-Lite (checksum covers only part of the packet).
  • SCTP4-LISTEN / SCTP4-CONNECT: message-based transport common in telecom; DCCP follows the same pattern; VSOCK connects a VM and its host without IP.
Terminal
# web: log collector + date serversocat -u UDP4-RECV:5140 OPEN:/tmp/remote.log,creat,appendsocat UDP4-RECVFROM:5000,fork EXEC:date# cli (and relay too)echo 'cli: disk 91% full' | socat -u - UDP4-SENDTO:192.168.64.20:5140echo x | socat -T 1 - UDP4:192.168.64.20:5000# SCTPsocat SCTP4-LISTEN:9900,reuseaddr,fork SYSTEM:'echo hello over SCTP'sleep 1 | socat - SCTP4-CONNECT:192.168.64.20:9900
Output
web$ cat /tmp/remote.logcli: disk 91% fullrelay: link upcli$ echo x | socat -T 1 - UDP4:192.168.64.20:5000Sun Nov  8 17:56:56 UTC 2026cli$ sleep 1 | socat - SCTP4-CONNECT:192.168.64.20:9900hello over SCTP

44.9 TLS and proxies

  • OPENSSL-LISTEN with cert= makes a TLS server; OPENSSL:host:port is a TLS client that verifies the server certificate by default.
  • Without the CA: certificate verify failed. With cafile= and commonname= the handshake succeeds; tcpdump never sees the plaintext.
  • OPENSSL-DTLS-CLIENT / -SERVER do the same over UDP.
  • PROXY:proxy:host:port,proxyport=3128 tunnels via HTTP CONNECT; SOCKS4, SOCKS4A (server resolves the name), SOCKS5-CONNECT work through a SOCKS server.
Terminal
socat OPENSSL-LISTEN:4443,reuseaddr,fork,cert=/tmp/server.pem,verify=0 SYSTEM:'echo hello over TLS'socat - OPENSSL:192.168.64.20:4443socat - OPENSSL:192.168.64.20:4443,cafile=/tmp/ca.pem,commonname=web.lab# via HTTP proxy 192.168.64.30socat TCP4-LISTEN:8888,reuseaddr,fork PROXY:192.168.64.30:192.168.64.20:80,proxyport=3128curl -s http://127.0.0.1:8888
Output
$ socat - OPENSSL:192.168.64.20:44432026/11/08 17:57:22 socat[133] E SSL_connect(): error:0A000086:SSL routines::certificate verify failed$ socat - OPENSSL:192.168.64.20:4443,cafile=/tmp/ca.pem,commonname=web.labhello over TLSweb$ grep -c 'hello over TLS' /tmp/tls.txt0$ curl -s http://127.0.0.1:8888web sees 192.168.64.30

44.10 UNIX sockets and the TUN tunnel

  • UNIX-LISTEN:/tmp/app.sock,mode=0660,unlink-early creates a socket file with rw-rw---- permission.
  • TCP4-LISTEN:2375 + UNIX-CONNECT exposes a local socket on the network: with the Docker socket this is risky.
  • ABSTRACT-LISTEN has no file; ss shows it with an @ sign.
  • TUN:10.255.0.1/24,iff-up creates a virtual interface tun0; pairing it with UDP gives a tiny VPN.
socat TUN tunnel over UDP 11443
  1. 1.Web: socat UDP4-LISTEN:11443 TUN:10.255.0.1/24,iff-up

  2. 2.Client: socat UDP4:192.168.64.20:11443 TUN:10.255.0.2/24,iff-up

  3. 3.Client pings 10.255.0.1: ICMP enters tun0

  4. 4.socat wraps it in UDP 192.168.64.10 -> 192.168.64.20:11443

  5. 5.Web socat unwraps it into its tun0; reply returns the same way

  6. 6.eth0 on the web server sees only UDP 11443 packets

Output
cli$ ip -br addr show tun0tun0             UNKNOWN        10.255.0.2/24 fe80::39ae:7ab1:87f3:e560/64cli$ ping -c 3 10.255.0.164 bytes from 10.255.0.1: icmp_seq=1 ttl=64 time=1.72 ms3 packets transmitted, 3 received, 0% packet loss, time 2002msweb$ cat /tmp/tun.txt17:58:45.872764 IP 192.168.64.10.43095 > 192.168.64.20.11443: UDP, length 8817:58:45.873026 IP 192.168.64.20.11443 > 192.168.64.10.43095: UDP, length 88web$ ss -xl | grep -E 'n044demo|app.sock'u_str LISTEN 0      5      /tmp/app.sock 822458            * 0u_str LISTEN 0      5          @n044demo 823557            * 0
  • IP4-SENDTO:host:253 sends a raw IP packet (tcpdump: ip-proto-253); INTERFACE:eth0 reads raw frames; SOCKET-* take domain, protocol and address in hex; POSIXMQ-* need --experimental.

44.11 Address options: FD, NAMED, OPEN groups

GroupExamplesUse
FD (any address)user= group= mode=, flock-ex, append, shut-down, null-eof, ioctl-*Permissions, locks, how the write side closes
NAMED (file system entry)unlink-early, perm-early, unlink-closeRemove old socket file before a restart
OPEN (open() flags)creat, excl, trunc, append, sync, nofollowFile create / append / truncate behaviour
Terminal
socat -u TCP4-LISTEN:9001,reuseaddr,fork OPEN:/tmp/app.log,creat,appendecho x | socat -u - OPEN:/tmp/app.log,creat,exclecho new | socat -u - OPEN:/tmp/app.log,trunc; cat /tmp/app.log
Output
$ cat /tmp/app.logline 1line 2$ echo x | socat -u - OPEN:/tmp/app.log,creat,excl2026/11/08 17:56:33 socat[96] E open("/tmp/app.log", 0301, 0666): File exists$ echo new | socat -u - OPEN:/tmp/app.log,trunc; cat /tmp/app.lognew

44.12 Common mistakes

  • Forgetting fork: the relay serves one client and exits.
  • No -T on UDP relays: UDP never sends EOF, so children never exit.
  • Exposing a UNIX socket (e.g. Docker's) over TCP without access control.
  • Thinking socat is just a new netcat: it connects any address to any address.

44.13 Interview tips

  • Port forward: socat TCP-LISTEN:8080,reuseaddr,fork TCP:192.168.64.20:80.
  • fork = a child per client; reuseaddr = port free immediately on restart.
  • Debug with -d -d (logs) and -v / -x (traffic).
  • DNS relay: UDP4-LISTEN:5353,reuseaddr,fork UDP4:192.168.1.1:53 plus -T 5.