socat: Linux / UNIX TCP Port Forwarder

How do I configure UNIX or Linux system to act as TCP port forwarder without using firewall? How do I install socat ( SOcket CAT ) multipurpose relay for bidirectional data transfer under Linux?

You can use the utility called socat (SOcket CAT). This is just like the Netcat but with security in mind (e.g., it support chrooting) and works over various protocols and through a files, pipes, devices, TCP sockets, Unix sockets, a client for SOCKS4, proxy CONNECT, or SSL etc.

From the man page:

Socat is a command line based utility that establishes two bidirectional byte streams and transfers data between them. Because the streams can be constructed from a large set of different types of data sinks and sources (see address types), and because lots of address options may be applied to the streams, socat can be used for many different purposes.

socat Usage:

  1. TCP port forwarder
  2. External socksifier
  3. Attacking weak firewalls (security testing)
  4. A shell interface to UNIX sockets
  5. IP6 relay
  6. For redirecting TCP oriented programs to a serial line
  7. Logically connect serial lines on different computers
  8. Security testing and research
  9. Establish a relatively secure environment (su and chroot) for running client or server shell scripts with network connections etc
WARNING! These examples may open your computer ports and sockets to other Internet users. You must have a good understanding of TCP/IP and UNIX networking to use this tool.

Install socat Under Debian / Ubuntu Linux

Type the following command:
$ sudo apt-get update && sudo apt-get install socat

Source Code Installation

Visit the official website and grab the latest version:
# cd /opt
# wget

Untar and install the same:
# tar -zxvf socat-
# cd socat-
# ./configure
# make
# make install


To redirect all port 80 conenctions to ip, enter:
# socat TCP-LISTEN:80,fork TCP:
All TCP4 connections to port 80 will be redirected to This is just like netcat. You can terminate connection by pressing [CTRL+C] i.e. ^C.

Connect To Remote SSH Server

You can connect to the remote ssh server called server1 and use pty for communication between socat and ssh, makes it ssh’s controlling tty (ctty), and makes this pty the owner of a new process group (setsid), so ssh accepts the password from socat.
$ (sleep 5; echo YOURSSHPASSWORDHERE; sleep 5; echo date; sleep 1) |socat - EXEC:'ssh -l userName',pty,setsid,ctty

Get Information About Haproxy

The following will give you information about the running HAProxy process such as pid, uptime and much more:
# echo "show info" | socat unix-connect:/var/tmp/haproxy stdio

TCP port forwarder, each side bound to another local IP address (bind)

This example handles an almost arbitrary number of parallel or consecutive connections by fork’ing a new process after each accept() . It provides a little security by su’ing to user nobody after forking; it only permits connections from the private 10 network (range); due to reuseaddr, it allows immediate restart after master process’s termination, even if some child sockets are not completely shut down. With -lmlocal2, socat logs to stderr until successfully reaching the accept loop. Further logging is directed to syslog with facility local2:
# socat -d -d -lmlocal2 \
TCP4-LISTEN:80,bind=myaddr1,su=nobody,fork,range=,reuseaddr \,bind=myaddr2

Further readings:

  • socat man page (see Examples section)
  • socat project.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • Steven Mar 16, 2010 @ 7:29

    nice stuff

  • xose Mar 20, 2010 @ 22:42

    also try ncat : , included in nmap.

  • Rabie K Apr 15, 2010 @ 15:03


    I am trying to redirect connection to port 8980 to execute a telnet command to a local machine ( port 23 )
    by issuing the following command :
    In Machine 1 :
    socat TCP-LISTEN:8980,fork EXEC:/myscript,reuseaddr

    myscript contains
    telnet //local Server 2

    I am sending Binary data from the client to Server 2 via Server 1.
    so it happens that I have some characters in Hex translated to special characters in ASCII like open brackets or Commas etc.. and that closed the socket between the two machines.

    Does anybody had this issue before ? if yes it there a workaround done to fix it ?

    Any help is appreciated

  • desert rose Feb 1, 2013 @ 8:07


    I am trying to redirect connection multiple port (range 2048 to 2200 ) to execute a single local server
    by issuing the following command :
    # socat TCP-LISTEN:2048,fork TCP:

    can anybody tell me what can i do ? , and help me to socat command to execute multiple port( range 2048 to 2200) over single TCP IP…
    thanks in advance..

  • Juanito Feb 13, 2013 @ 11:53

    I´ve been trying to forward packets coming from port 4001 to a new multicast address with different port and finally I found the solution that perhaps someone can find useful: (My local IP is

     socat udp4-recvfrom:4001,ip-add-membership=,fork udp-datagram:
    >udp4-recvfrom:4001 - Handle everything coming in from port 4001
    >ip-add-membership= - Creates a subscription to the multicast for the local interface
    >udp-datagram: - forwards the packets matching with incoming port 4001 to the multicast destination (can be unicast) on port 5001

    If you get the following error message “…Network is unreachable” you need to create an static route to the multicast destination. The comands may change on each Linux distribution, for CentOS:

    [root@localhost bin]# ip route add [multicast destination] via [local interface IP] dev [local interface name]
    [root@localhost bin]# ip route add via dev eth0

    Local interface name can be checked by using:

    #ifconfig -a

    I hope it makes you save time. It worked on my computer but if you find some errors please report).

    • Daren Matthews Apr 8, 2014 @ 18:00

      That is nice – very much appreciated

  • Manoz Karmacharya Feb 12, 2015 @ 18:10

    I want to connect to internetworking device in vmware workstation (in linux) with serial port i.e use socket (named pipe). Need help

  • Wellington Torrejais da Silva Dec 25, 2016 @ 1:23


  • Maikel Apr 2, 2017 @ 16:39

    Hi there,
    Thank you for the useful blogpost. I’ve got one question:
    While socat TCP-LISTEN:22,fork TCP: is running and checking status with ss -lnt, I’m getting the following output:

    State Recv-Q Send-Q Local Address:Port Peer Address:Port
    LISTEN 0 5 *:22 *:*
    LISTEN 0 128 :::443 :::*
    LISTEN 0 128 :::9000 :::*
    LISTEN 0 128 :::80 :::*

    Do you know the reason for Send-Q being so low compare to the rest and how to change it?
    Thank you.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum