$linuxjunkies
>

socat(1)

socat is a relay for bidirectional data transfer between two independent data channels.

UbuntuDebianFedoraArch

Synopsis

socat [options] <address> <address>

Description

socat establishes two bidirectional byte streams and transfers data between them. It acts as a universal connector for network sockets, files, devices, and pipes. Common uses include port forwarding, SSL/TLS wrapping, and bridging different protocol types.

Each address specifies a data channel using an address type (TCP, UDP, FILE, EXEC, UNIX socket, etc.) and type-specific parameters. socat reads from one address and writes to the other, then reverses the direction, creating a full duplex connection.

Common options

FlagWhat it does
-dincrease verbosity; use multiple times for more detail (-dd, -ddd)
-vwrite transferred data to stderr as text
-xwrite transferred data to stderr in hexdump format
-b <size>set the buffer size for data transfer (default 8192)
-t <timeout>set timeout in seconds for all connections
-T <timeout>set timeout for each individual read/write operation
-uunidirectional mode; data flows only from first to second address
-lysyslog logging instead of stderr
-L <file>log to specified file instead of stderr
-gdo not exit on first address EOF (useful for servers)

Examples

Create a simple port forwarder: listen on port 8080 and relay to example.com:80

socat TCP-LISTEN:8080,reuseaddr TCP:example.com:80

Interactive SSH relay: connect stdin/stdout to a remote SSH server

socat - TCP:example.com:22

Terminate SSL/TLS on port 443 and forward plaintext to local port 80

socat TCP-LISTEN:443,reuseaddr,cert=/path/to/cert.pem,key=/path/to/key.pem TCP:localhost:80

Create a reverse shell listener: incoming TCP connections get a bash shell

socat TCP-LISTEN:5555,reuseaddr EXEC:/bin/bash,pty,stderr

Transparent proxy with verbose logging of all transferred data

socat -v TCP:192.168.1.100:3306 TCP-LISTEN:3306,reuseaddr

Relay from a Unix socket to another Unix socket (useful for Docker forwarding)

socat UNIX-LISTEN:/tmp/relay.sock,reuseaddr UNIX:/var/run/docker.sock

SSH port forwarder with debug output to troubleshoot connection issues

socat -d -d TCP-LISTEN:2222,reuseaddr TCP:10.0.0.5:22

Bridge a serial device to a remote TCP service without line editing

socat /dev/ttyUSB0,raw,echo=0 TCP:10.20.30.40:9001

Related commands