Linux Multipurpose Relay Socat Command Tutorial with Examples

Linux provides different philosophy and use cases from system point of view. `socat` is very interesting command which provides us the ability to redirect input and outputs from different type of system resources like network, file, command, socket etc.

Install For Debian, Ubuntu, Mint and Kali

socat can be install dpkg based distributions like below by using apt command.

$ sudo apt install socat -y
Install For Debian, Ubuntu, Mint and Kali
Install For Debian, Ubuntu, Mint and Kali

Install For Fedora, CentOS, RHEL

We can install for Fedora, CentOS and RHEL like below.

$ sudo yum install socat


Syntax of socat is very simple. We need to just provide the input and output with related options.

  • INPUT_TYPE specifies the file, stream or network socket we will read
  • OUTPUT_TYPE specifies file, stream or network socket we will write
  • OPTIONS specifies some configuration about INPUT or OUTPUT


Type refer to the file, command, stream or network. INPUT and OUTPUT can be different types. For example if we want to use a system command as type we need to specify SYSTEM as INPUT_TYPE . List of types

  • TCP
  • UDP
  • STDIO or –
  • PTY
  • PIPE


Options are used to provide parameters and details about INPUT types. OPTIONS are added after INPUT by separating with : . for example in order to specify a PIPE path we will use following line.


Write To Standard Output

We will start with simple example. We will run a system command with SYSTEM and providing command ls and than write output to the standard output.

$ socat SYSTEM:ls -
Write To Standard Output
Write To Standard Output

Read From Standard Input

We can also read from standard input too. In this example we will read from standard input and write into a file named test. We will use STDIO to specify standard input.

$ socat STDIO FILE:/home/ismail/test,create

As we can see we have provided the file name as option and the create statement which will create a new file if it is not exist.

LEARN MORE  Linux Watch Command Tutorial With Examples

Open Network Socket and Listen Port

We can also use network sockets with socat .We will open TCP and IP version 4 port for listening port number 1234. This will be forked process where new process will be created. The output  will be a file named capture and append newly arrived data to the end of file.

$ socat TCP4-LISTEN:1234,reuseaddr,fork gopen:/home/ismail/capture,seek-end=0,append

Append To A File

As we have all ready used but I want to express the append . append option will add to the end of the given file like below.

$ socat FILE:/tmp/test1 FILE:/tmp/test:append

Surf In The Web Over Standard Output

This may be seem a bit weird but as we can use different type of protocols and sockets. We can connect a web page and retrieve web pages printing to the standard output. In this example we will connect to the TCP port 80 and print to the standard output. We will provide GET / from standard input to get pages.

$ socat -,crnl
Surf In The Web Over Standard Output
Surf In The Web Over Standard Output


Leave a Reply