socat — Socket relay and conversion
Linux / Network and Firewall / socat.html
Connects two data channels so files, pipes, sockets and serial lines can relay to each other.
Basic Information
| Command Name | socat |
|---|---|
| One-line Introduction | Connects two data channels so files, pipes, sockets and serial lines can relay to each other. |
| Provided by | socat project (packaged by distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/socat.1.html |
| Supported Operating Systems | Linux, macOS, BSD |
| Installation Status | Package required (apt install socat) |
| Category | Linux networking (tags: Address, port and firewall management (tags: socket, relay, proxy, convert)) |
What is this command?
socat takes two addresses and relays bytes between them in both directions.
Any mix of TCP, UDP, UNIX sockets, files and programs can be joined, which makes quick proxies easy.
What problems does it solve?
- Forward a local port to a remote service.
- Expose a UNIX socket over TCP.
- Relay a serial device onto the network.
- Stand up a throwaway test server.
Key features
Port forwarding
Connections arriving on one address are passed to another.
- When to use: When reaching a service behind a firewall.
- What you get: Data flows through unchanged.
- Limitations: It lasts only while the process runs.
Socket exposure
Bridge a UNIX socket to a TCP address.
- When to use: When testing a container socket from outside.
- What you get: The socket service becomes reachable remotely.
- Limitations: There is no authentication, so restrict exposure.
File and program channels
Files and programs can be used as addresses.
- When to use: When you need canned responses for a test.
- What you get: Fixed data is exchanged.
- Limitations: Program errors propagate to the connection.
Installation and usage
Check whether it is installed
socat -V
sudo apt install socat
Real-world examples
Forward a port
Relay local 8080 to remote port 80.
socat TCP-LISTEN:8080,fork TCP:example.com:80
Expose a socket
Bridge a UNIX socket to TCP.
socat TCP-LISTEN:9000,fork UNIX-CONNECT:/run/app.sock
Fake server
Serve a fixed string.
socat TCP-LISTEN:9999,reuseaddr,fork EXEC:'/bin/echo hello'