1. Connect to a Remote Port with the socat command
The simplest use of the socat command is connecting to a remote TCP port. Think of it as a more flexible version of netcat. You pipe data in both directions without fussing with connection handling.
To connect to a web server and grab its HTTP response:
socat - TCP4:example.com:80
Type your HTTP request:
GET / HTTP/1.1
Host: example.com
The - means standard input and output. TCP4:example.com:80 is the remote address. Socat reads from your terminal, sends it over the wire, and prints the response back to your screen.
This works for any TCP service. SSH, databases, custom protocols. If it speaks TCP, socat can talk to it.
2. Forward TCP Ports with the socat command
Port forwarding is where socat really shines. You can redirect traffic from one port to another, on the same machine or across machines.
Simple local port forward:
socat TCP-LISTEN:8080,fork TCP4:localhost:80
This listens on port 8080 and forwards every connection to port 80 on localhost. The fork option makes socat handle each connection in a separate process, so multiple clients can connect at once.
I use this constantly when testing web apps in development. Run your app on port 3000 but need it on 8080 for testing? Socat handles it in one line. No reverse proxy config needed.
3. Transfer Files with the socat command
Need to send a file between two machines without setting up FTP, SCP, or a web server? Socat can do raw file transfers over any protocol.
On the receiving machine:
socat TCP4-LISTEN:4444 OPEN:received_file.txt,creat,append
On the sending machine:
socat OPEN:myfile.txt TCP4:192.168.1.100:4444
Socat opens the file, reads its contents, and pipes them straight to the TCP connection. On the other end, it writes the data to a new file. No encryption or authentication baked in, so keep this on trusted networks.
For a quick test, try sending a file to yourself over localhost. It works and it is fast.
4. Create a Local Echo Server with the socat command
Sometimes you just want to test what your app is sending over the network. An echo server with socat is the fastest way to do that.
socat TCP4-LISTEN:9999,fork EXEC:cat
This listens on port 9999. Every connection gets handed to cat, which echoes back whatever it receives. It is a raw echo server in 20 characters.
I use this when debugging HTTP clients, WebSocket connections, or any custom protocol. Point your app at port 9999, and socat shows you exactly what it sends, byte for byte.
5. Connect to UNIX Sockets with the socat command
UNIX domain sockets are everywhere on Linux. Docker uses them. Systemd uses them. Your database probably uses one too. Socat can bridge between UNIX sockets and TCP, which is incredibly handy.
Forward a UNIX socket to a TCP port:
socat TCP4-LISTEN:2375,fork UNIX-CONNECT:/var/run/docker.sock
This exposes the Docker socket over TCP on port 2375. Now you can point remote Docker clients at this machine. Socat transparently translates between the two socket types.
You can also go the other direction, connecting a local program that expects a UNIX socket to a remote TCP service. Socat does not care which side is which. It just moves bytes.
6. Set Up a Simple SSL Proxy with the socat command
Adding TLS to a plain TCP connection does not require a full reverse proxy setup. Socat has built-in OpenSSL support for both client and server roles.
Create an SSL-terminating listener:
socat OPENSSL-LISTEN:443,cert=server.pem,verify=0,fork TCP4:localhost:80
This accepts SSL connections on port 443, decrypts them, and forwards the plain traffic to a local HTTP server on port 80. The verify=0 flag disables client certificate verification for testing.
For the client side, connect to a remote SSL service:
socat - OPENSSL:example.com:443
This is how I test TLS endpoints without a browser or curl. The socat command handles the handshake, and I type raw HTTP over the encrypted channel.
7. Read and Write to Serial Ports with the socat command
Serial ports are still relevant for embedded devices, routers, and industrial hardware. Socat treats serial devices like any other address, making it easy to bridge serial to TCP.
Expose a serial port over the network:
socat TCP4-LISTEN:5000,fork /dev/ttyUSB0,raw,echo=0
This lets you connect to a serial device remotely. Plug in your USB-to-serial adapter, run socat, and access the device from another machine on the network.
For testing serial apps without real hardware, create a virtual serial pair:
socat PTY,link=/tmp/vmodem0 PTY,link=/tmp/vmodem1
This creates two linked virtual serial ports. Write to one, read from the other. I used this trick to debug a home automation controller before the actual hardware arrived.
8. Create a UDP Multicast Listener with the socat command
UDP multicast is used by service discovery protocols like mDNS and by streaming applications. Socat can join a multicast group and dump traffic.
socat UDP4-RECVFROM:5353,ip-add-membership=224.0.0.251:0.0.0.0,fork -
This listens for mDNS traffic on port 5353. Every multicast packet gets printed to your terminal. It is a raw view into what devices on your network are advertising.
For sending data to a multicast group:
echo "hello" | socat - UDP4-DATAGRAM:224.0.0.251:5353
Socat handles the group membership and TTL settings under the hood. You just point it at the multicast address and go.
Why the socat command Belongs in Your Toolbox
The socat command is one of those tools you do not use every day, but when you need it, nothing else cuts it. It replaces a dozen specialty tools for port forwarding, file transfer, socket bridging, and protocol testing.
It is already installed on most Linux distributions. If it is not there, install it with apt install socat or yum install socat. The binary is small and has no major dependencies.
Start with the examples above. Try the echo server first. Then the port forward. Once you understand the address syntax, everything else falls into place.
For more Linux networking tools, check out our tcpdump command tutorial and the strace command guide for debugging. If you monitor system performance, the htop features article is worth a look too.
The official socat man page has the full address reference. The Red Hat socat guide covers enterprise use cases. For the source code, the socat GitHub repository has the latest development version.