Skip to content

socat command in Linux

socat is a versatile networking tool for data transfer and manipulation in Linux systems. It can establish two bidirectional byte streams and transfer data between them. With support for various protocols and options, socat is a powerful tool for network troubleshooting, port forwarding, and creating network connections. It can also act as a proxy or relay for network services.

Terminal window
socat [options] <address1> <address2>
OptionDescription
-dDisplay more debug messages
-verboseBe more verbose
-vPrint version information and exit
-hShow help
-VPrint version and exit
-xPrint hex dump of all data
-uUse UDP instead of TCP
-TUse TTY (terminal) mode
-bSet buffer size
ParameterDescription
address1Description of address1 parameter
address2Description of address2 parameter
Terminal window
socat TCP-LISTEN:1234 -

Creates a TCP listener on port 1234.

Terminal window
socat - TCP:example.com:80

Connects to a TCP server at example.com on port 80.

Terminal window
socat -u FILE:/path/to/local/file TCP:example.com:12345

Transfers a file over TCP to a remote server at example.com on port 12345.

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

Forwards local port 8080 to the remote host example.com on port 80.

Terminal window
socat TCP-LISTEN:1234,reuseaddr,fork SYSTEM:'tee output.log'

Creates a TCP listener on port 1234 with logging to a file named output.log.

Terminal window
socat TCP4-LISTEN:1234,reuseaddr,fork OPENSSL:example.com:443,verify=0

Establishes a secure SSL tunnel from local port 1234 to the remote host example.com on port 443, with certificate verification disabled.

Terminal window
socat UDP-LISTEN:1234,fork TCP:example.com:80

Redirects incoming UDP traffic on port 1234 to a TCP connection with the remote server at example.com on port 80.

Terminal window
socat - UNIX-CONNECT:/path/to/socket

Connects to a Unix socket located at /path/to/socket.

To use the socat command in Linux, execute the following command:

Terminal window
socat --version

The main purpose of socat in bash is to establish two bidirectional byte streams and transfer data between them.

Terminal window
socat TCP4-LISTEN:4444,fork EXEC:/bin/bash

How can I create a simple TCP server using socat?

Section titled “How can I create a simple TCP server using socat?”

To create a simple TCP server using socat, you can use the following command:

Terminal window
socat TCP4-LISTEN:1234,reuseaddr,fork EXEC:/bin/cat

How do I transfer a file using socat in Linux?

Section titled “How do I transfer a file using socat in Linux?”

To transfer a file using socat in Linux, you can use the following command:

Terminal window
socat FILE:localfile.txt TCP4:remotehost:port

Can I use socat for port forwarding in Linux?

Section titled “Can I use socat for port forwarding in Linux?”

Yes, you can use socat for port forwarding in Linux. Here’s an example command:

Terminal window
socat TCP-LISTEN:8080,fork TCP:localhost:80

How do I create a secure SSL/TLS connection with socat?

Section titled “How do I create a secure SSL/TLS connection with socat?”

To create a secure SSL/TLS connection with socat, you can use the following command:

Terminal window
socat - OPENSSL:www.example.com:443,cert=client.pem,cafile=ca-certs.pem

How can I use socat to redirect output to a file in Linux?

Section titled “How can I use socat to redirect output to a file in Linux?”

You can use socat to redirect output to a file in Linux by running the following command:

Terminal window
socat -u EXEC:'command',pty,stderr > output.txt

Can I use socat to connect to a serial port in Linux?

Section titled “Can I use socat to connect to a serial port in Linux?”

Yes, you can use socat to connect to a serial port in Linux. Here’s an example command:

Terminal window
socat -d -d pty,raw,echo=0 pty,raw,echo=0

How do I create a simple UDP server using socat?

Section titled “How do I create a simple UDP server using socat?”

To create a simple UDP server using socat, you can use the following command:

Terminal window
socat -v UDP-LISTEN:1234,fork STDOUT
  • Creating virtual serial connections
  • Network debugging
  • Proxying and forwarding data
  • Port scanning
  • SSL/TLS encryption
  • Tunneling
  • File transfer
  • Shell redirection