socat (2/3)
socat address options are written after an address with commas (TYPE:params,opt1,opt2=value). Every option belongs to a group (REG, PROCESS, SOCKET, IP4, TCP, RANGE, FORK...) that decides on which address types it is valid; if the group does not match, socat refuses to start.
45.1 Why is it used?
- Read or follow files from any position (seek, ignoreeof).
- Run the child program safely: different user, folder, umask (su, chdir).
- Tune the socket: source address/port, TTL, TOS, timeouts, MSS, keepalive.
- Control who may connect and how many children run (range, max-children).
- Lab: 192.168.65.0/24 with srv .10, cli .20, out .30.
45.2 How it works: range, fork, su
- Step 1
1.socat listens on 192.168.65.10:7003
- Step 2
2.Client 192.168.65.20 connects: TCP handshake completes
- Step 3
3.range check passes: fork creates a child running as nobody
- Step 4
4.Child sends 'welcome' to the client
- Step 5
5.Outsider 192.168.65.30 connects: handshake completes, range check fails
- Step 6
6.socat closes it at once: 'refusing connection ... due to range option'
45.3 Files: REG, BLK and PIPE options
- seek=N: start N bytes from the start; seek-cur=N from the current position; seek-end=N from the end.
- seek-end=0 + ignoreeof works like tail -f: only new lines appear.
- ftruncate=N truncates the file; secrm, immutable, fs-append, noatime... set Linux chattr attributes (some need root).
- PIPE group: f-setpipe-sz=N (alias setpipe) sets a named pipe's buffer size.
cat /tmp/app.logsocat -u OPEN:/tmp/app.log,seek=11 -# follow: seek-end + ignoreeof(timeout 4 socat -u OPEN:/tmp/app.log,seek-end=0,ignoreeof - > /tmp/follow.txt) &sleep 1echo 'line4 link down' >> /tmp/app.logwait; cat /tmp/follow.txt$ cat /tmp/app.logline1 bootline2 dhcp okline3 dns ok$ socat -u OPEN:/tmp/app.log,seek=11 -line2 dhcp okline3 dns ok$ wait; cat /tmp/follow.txt2026/11/08 18:02:16 socat[32] W exiting on signal 15line4 link down45.4 General and PROCESS options
- General (all addresses): chdir=DIR starts the program in that folder; umask=0077 makes new files owner-only.
- su=nobody: after accept the child switches to the nobody user and groups.
- setuid / setgid change only user or group; -early versions do it before the address is opened; su-d looks up before chroot.
- chroot / chroot-early lock the process in a folder; netns=NAME opens the address inside a network namespace.
- READLINE group: history=FILE, noprompt, noecho=REGEX (hide passwords), prompt=STR.
socat -u SYSTEM:pwd,chdir=/etc -socat -u SYSTEM:'echo hi' OPEN:/tmp/u.txt,creat,umask=0077ls -l /tmp/u.txt# srvsocat TCP-LISTEN:7000,reuseaddr,fork SYSTEM:id,su=nobody# clisocat - TCP:192.168.65.10:7000$ socat -u SYSTEM:pwd,chdir=/etc -/etc$ ls -l /tmp/u.txt-rw------- 1 root root 3 Nov 8 18:02 /tmp/u.txt$ socat - TCP:192.168.65.10:7000uid=65534(nobody) gid=65534(nobody) groups=65534(nobody)45.5 APPLICATION options (data level)
- readbytes=N: stop after N bytes, even from endless /dev/urandom.
- crnl: add CR at the end of every line (shows as \r in od -c); cr: use CR only.
- lockfile=F / waitlock=F: stop two socat instances from running together; escape=0x0f: escape character.
socat -u /dev/urandom,readbytes=1048576 TCP:192.168.65.10:9000wc -c /tmp/out.bin # srvecho hello | socat - SYSTEM:'od -c',crnlecho hello | socat - SYSTEM:'od -c'$ wc -c /tmp/out.bin1048576 /tmp/out.bin$ echo hello | socat - SYSTEM:'od -c',crnl0000000 h e l l o \r \n0000007$ echo hello | socat - SYSTEM:'od -c'0000000 h e l l o \n000000645.6 SOCKET and IP4 options
- bind=IP ties the socket to a local address; sourceport=P fixes the client source port; lowport picks a privileged port 640-1023.
- connect-timeout=2: give up on a dead host after exactly 2 s.
- ttl=1 and tos=0x10 change the IP header (seen in the server's tcpdump).
- Others: so-bindtodevice, broadcast, keepalive, linger, rcvbuf/sndbuf, rcvtimeo, reuseaddr/reuseport, setsockopt-*.
socat - TCP:192.168.65.10:7001,bind=192.168.65.20,sourceport=40000time socat - TCP:192.168.65.99:80,connect-timeout=2echo hi | socat -u - UDP:192.168.65.10:5000,ttl=1,tos=0x10# srvtcpdump -n -v -c1 -i eth0 udp port 5000$ socat - TCP:192.168.65.10:7001,bind=192.168.65.20,sourceport=40000peer 192.168.65.20 port 40000$ time socat - TCP:192.168.65.99:80,connect-timeout=22026/11/08 18:02:50 socat[99] E connecting to AF=2 192.168.65.99:80: Operation timed outreal 0m 2.00s$ tcpdump -n -v -c1 -i eth0 udp port 5000IP (tos 0x10, ttl 1, id 60786, offset 0, flags [DF], proto UDP (17), length 31) 192.168.65.20.54366 > 192.168.65.10.5000: UDP, length 3- Step 1
1.cli 192.168.65.20 sends UDP with ttl=1
- Step 2
2.To srv 192.168.65.10 on the same LAN: no router, packet received (ttl 1)
- Step 3
3.To 10.20.0.5 in another network: packet goes to router 192.168.65.1
- Step 4
4.Router decrements TTL 1 -> 0: TTL exceeded, packet dropped
- IP options: ip-recvttl / ip-recvtos / ip-pktinfo show extra info in the -d -d log; ip-add-membership joins a multicast group; mtudiscover=2 always sets DF; ip-transparent for a transparent proxy.
- Resolver: ai-* change getaddrinfo, res-* apply only to DNS lookups (only res-debug and res-retry exist in the lab's Alpine build).
45.7 IP6 option: ipv6only
| Listener | IPv4 client | IPv6 client |
|---|---|---|
| TCP6-LISTEN:7006,ipv6only=0 | Accepted, shown as mapped ::ffff:c0a8:4114 (= 192.168.65.20) | Accepted |
| TCP6-LISTEN:7007,ipv6only=1 | Connection refused | Accepted |
$ socat - TCP4:192.168.65.10:7006got [0000:0000:0000:0000:0000:ffff:c0a8:4114]$ socat - TCP4:192.168.65.10:70072026/11/08 18:03:09 socat[114] E connect(5, AF=2 192.168.65.10:7007, 16): Connection refused$ socat - TCP6:[fd00:65::10]:7007got [fd00:0065:0000:0000:0000:0000:0000:0020]45.8 TCP, UDP-Lite, SCTP, DCCP options
- mss=1200: proposes the MSS in the SYN (normal on Ethernet is 1460).
- keepalive + keepidle=10, keepintvl=5, keepcnt=3: first probe after 10 s idle, every 5 s, drop after 3 missed.
- nodelay: turns off Nagle for small messages; syncnt limits SYN retries; defer-accept accepts only when data arrives.
- udplite-send/recv-cscov: bytes covered by the UDP-Lite checksum; sctp-nodelay, sctp-maxseg; dccp-set-ccid picks congestion control.
# srv: capture only the SYNtcpdump -n -c1 -i eth0 'tcp dst port 7002' and 'tcp[tcpflags] & tcp-syn != 0'# cli: mss + keepalivesocat -u SYSTEM:'sleep 20' TCP:192.168.65.10:7002,mss=1200,keepalive,keepidle=10,keepintvl=5,keepcnt=318:03:19.989953 IP 192.168.65.20.53382 > 192.168.65.10.7002: Flags [S], seq 1554642686, win 64800, options [mss 1200,sackOK,TS val 1983773860 ecr 0,nop,wscale 10], length 045.9 Proxy options
- socksport=P (default 1080), socksuser=U for SOCKS4/4A.
- proxyport=P (default 8080), http-version, ignorecr, resolve for PROXY (HTTP CONNECT).
- proxy-authorization=user:pass is visible in the process list and is only base64; proxy-authorization-file=FILE is safer.
45.10 RANGE, LISTEN and FORK options
- range=192.168.65.20/32 (or 10.0.0.0:255.0.0.0, [2001:db8::]/32); tcpwrap / allow-table / deny-table use libwrap.
- max-children=5 limits parallel children; accept-timeout=2 stops the listener after 2 s with no client; backlog=N sets the listen queue.
- FORK: by default only stdout goes to the client; stderr also sends errors; pty gives the child a real terminal.
- EXEC: path=DIRS and login; FORK also: nofork, pipes, fdin/fdout, sigint (forward Ctrl-C to the child).
socat -d -d TCP4-LISTEN:7003,reuseaddr,fork,max-children=5,range=192.168.65.20/32 SYSTEM:'echo welcome' 2> /tmp/range.logtime socat TCP-LISTEN:7004,accept-timeout=2 -socat TCP-LISTEN:7005,reuseaddr,fork SYSTEM:'ls /nope'socat TCP-LISTEN:7008,reuseaddr,fork SYSTEM:'ls /nope',stderrsocat - EXEC:ttysocat - EXEC:tty,ptycli$ socat - TCP:192.168.65.10:7003welcomeout$ socat - TCP:192.168.65.10:7003; echo exit=$?exit=0$ grep -i -E 'refus|range' /tmp/range.log2026/11/08 18:03:34 socat[100] W refusing connection from AF=2 192.168.65.30:46140 due to range option$ time socat TCP-LISTEN:7004,accept-timeout=2 -2026/11/08 18:03:41 socat[123] W accept: Operation timed outreal 0m 2.01s$ socat - TCP:192.168.65.10:7008ls: /nope: No such file or directory$ socat - EXEC:ttynot a tty$ socat - EXEC:tty,pty/dev/pts/045.11 Common mistakes
- Treating range as a firewall: the handshake completes first and socat closes after accept; the outsider sees open-then-close, not refused. Use firewall rules for real blocking.
- Using an option from the wrong group: socat does not start.
- Putting proxy-authorization on the command line: password visible in ps.
- Expecting errors from a SYSTEM child without the stderr option.
45.12 Interview tips
- Safe port forwarder: TCP-LISTEN with fork, reuseaddr, range, su=nobody, max-children, plus connect-timeout to the backend; debug with -d -d.
- Only 10.0.0.0/8, one process per client, drop root: range=10.0.0.0/8, fork, su=nobody.
- ttl=1 keeps traffic on the local LAN.
- ipv6only=0 makes one IPv6 listener dual-stack (IPv4 seen as ::ffff:a.b.c.d).