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
| Platform | How to get it |
|---|---|
| Linux | apt install socat / dnf install socat |
| macOS | brew install socat |
| Windows | Not native: WSL or Docker |
44.2 How it works: Address 1 and Address 2
- Step 1
1.Relay 192.168.64.5 runs: TCP4-LISTEN:8080,reuseaddr,fork TCP4:192.168.64.20:80
- Step 2
2.Client 192.168.64.10 sends GET / to the relay on port 8080
- Step 3
3.fork: a child process handles this client
- Step 4
4.Child opens a new TCP connection to web server 192.168.64.20:80 and copies the bytes
- Step 5
5.200 OK comes back to the relay, which delivers it to the client
- Step 6
6.Web server only sees the relay's IP: 'web sees 192.168.64.5'
44.3 Address syntax
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-insensitive44.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.
# 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$ 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.544.5 Logging and help
| Option | What it does |
|---|---|
| -V | Version and compiled features (1.8.0.0 in the lab) |
| -h / -hh / -hhh | Help, options, aliases with groups |
| -d0 ... -d4 (-dddd) | From only errors up to full debug |
| -lf file / -ly / -ls | Log to a file / syslog / stderr (default) |
| -lp name | Program name in the log |
| -lu / -lh | Microsecond timestamps / hostname in the log |
| -D | FD info before transfer |
$ 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 044.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.
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$ 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 v144.7 Files, pipes and programs
| Address | What it is |
|---|---|
| CREATE:file / OPEN:file / GOPEN:file | New file (write) / open() a file or device / file or UNIX socket |
| PIPE | Unnamed pipe: writes back what it reads = echo server |
| PIPE:name | Named pipe (FIFO) |
| STDIO or - | Keyboard and screen (fd 0 and 1) |
| FD:3, STDIN, STDOUT, STDERR | Already open file descriptors |
| READLINE | Line editing + history |
| PTY,link=path | Virtual serial port |
| EXEC:cmd / SYSTEM:cmd / SHELL:cmd | No shell (execvp) / via sh -c / via $SHELL |
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$ socat - TCP4:192.168.64.20:13000Sun Nov 8 17:56:15 UTC 2026$ echo hello socat | socat - TCP4:192.168.64.20:7000HELLO SOCAT44.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.
# 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:9900web$ 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 SCTP44.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.
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$ 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.3044.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.
- Step 1
1.Web: socat UDP4-LISTEN:11443 TUN:10.255.0.1/24,iff-up
- Step 2
2.Client: socat UDP4:192.168.64.20:11443 TUN:10.255.0.2/24,iff-up
- Step 3
3.Client pings 10.255.0.1: ICMP enters tun0
- Step 4
4.socat wraps it in UDP 192.168.64.10 -> 192.168.64.20:11443
- Step 5
5.Web socat unwraps it into its tun0; reply returns the same way
- Step 6
6.eth0 on the web server sees only UDP 11443 packets
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
| Group | Examples | Use |
|---|---|---|
| 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-close | Remove old socket file before a restart |
| OPEN (open() flags) | creat, excl, trunc, append, sync, nofollow | File create / append / truncate behaviour |
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$ 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.lognew44.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.