Skip to content
Go back

Socat: The Swiss Army Knife of Networking

· Updated:
By SumGuy 5 min read
Socat: The Swiss Army Knife of Networking

Socat (SOcket CAT) is a powerful command-line tool that establishes bidirectional data channels between various sources and destinations. It acts as a relay, enabling data to flow between processes, files, devices, and network sockets. Think of it as a multi-purpose adapter that connects disparate communication channels.

Core Uses of Socat

Socat Command Structure

socat [OPTIONS] <ADDRESS1> <ADDRESS2>

OPTIONS: Control socat’s behavior (e.g., logging, timeouts, etc.).

socat TCP-LISTEN:8080,fork TCP:www.example.com:80

This listens on port 8080 and forwards connections to www.example.com[www.example.com](https://www.example.com) on port 80.

socat -d -d PTY,link=/dev/ttyS10 TCP:192.168.1.100:2000

This creates a pseudo-terminal (/dev/ttyS10) that communicates over TCP.

socat -x -v TCP-LISTEN:8080,fork SYSTEM:"tcpdump -s 0 -w capture.pcap"

Captures network traffic on port 8080 and saves it to a capture.pcap file.

socat -u EXEC:"producer_app",stderr EXEC:"consumer_app"

Pipes the standard error output of producer_app to the standard input of consumer_app.

Flow Control

Socat lets you control the rate at which data flows between addresses, which is essential when connecting systems with different processing speeds or when simulating network conditions.

socat -u TCP-LISTEN:9000,fork OPEN:/dev/null # Discard incoming data
socat -U EXEC:"generate_data.sh" TCP:192.168.1.10:8888 # Send script output to a remote host
socat -b 1024 TCP4-LISTEN:8080,fork TCP4:www.example.com:80 # 1KB buffer

2. Data Transformation

The system address type is a powerful tool for transforming data on the fly using external commands.

socat TCP-LISTEN:8080,fork SYSTEM:"sed 's/foo/bar/g'" # Replace 'foo' with 'bar' in incoming data

3. Logging

Socat provides detailed logging options to help troubleshoot connections and track data flow.

socat -v -lf socat.log TCP-LISTEN:8080,fork TCP:www.example.com:80

4. Timeouts

Set timeouts to gracefully handle connection failures and idle connections.

socat -T 10 -t 60 TCP-LISTEN:8080,fork TCP:www.example.com:80

5. Advanced Address Types

Socat supports a wide array of address types beyond just TCP and files:

Example: OpenSSL Encryption

socat OPENSSL-LISTEN:4433,cert=server.crt,key=server.key,verify=0,fork TCP:localhost:80

This creates a secure, encrypted tunnel to localhost on port 80, using the specified certificate and key.

Tips and Gotchas

Real-World Gotcha: The fork Option and File Descriptor Limits

Here’s a scenario that’ll bite you at 2 AM. You set up socat as a TCP relay with fork to handle multiple connections, things work fine in testing, and then in production it falls over with cryptic errors. Nine times out of ten, you’ve hit your system’s open file descriptor limit.

Every forked child process inherits open sockets from the parent, and socat holds those descriptors open longer than you’d expect. On a default Linux system, the soft limit per process is 1024. If you’re relaying a moderately busy service — say, a container health check endpoint getting hammered — you’ll chew through those fast.

Check your current limits:

Terminal window
ulimit -n
# typical output: 1024

For a long-running socat relay, bump it before launching:

Terminal window
ulimit -n 65535
socat TCP-LISTEN:8080,fork,reuseaddr TCP:backend-host:8080

Or do it properly with a systemd unit so it survives reboots:

/etc/systemd/system/socat-relay.service
[Unit]
Description=Socat TCP relay
After=network.target
[Service]
ExecStart=/usr/bin/socat TCP-LISTEN:8080,fork,reuseaddr TCP:backend-host:8080
Restart=always
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target

Enable and start it:

Terminal window
systemctl daemon-reload
systemctl enable --now socat-relay

The reuseaddr option on the listener is equally important — without it, restarting socat leaves the port in TIME_WAIT and you get “address already in use” for up to 60 seconds. Nobody wants to stare at that while trying to roll a fix. Always include reuseaddr on persistent listeners.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Set the Timezone in Ubuntu with timedatectl
Next Post
ss Is the New netstat (And It's Better)

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts