The socat utility is a relay for bidirectional data transfers between two independent data channels.
There are many different types of channels socat
can connect, including:
- Files
- Pipes
- Devices (serial line, pseudo-terminal, etc)
- Sockets (UNIX, IP4, IP6 - raw, UDP, TCP)
- SSL sockets
- Proxy CONNECT connections
- File descriptors (stdin, etc)
- The GNU line editor (readline)
- Programs
- Combinations of two of these
This tool is regarded as the advanced version of netcat. They do similar things, but socat
has more additional functionality, such as permitting multiple clients to listen on a port, or reusing connections.
Why do we need socat?
There are many ways to use socate
effectively. Here are a few examples:
- TCP port forwarder (one-shot or daemon)
- External socksifier
- Tool to attack weak firewalls (security and audit)
- Shell interface to Unix sockets
- IP6 relay
- Redirect TCP-oriented programs to a serial line
- Logically connect serial lines on different computers
- Establish a relatively secure environment (
su
andchroot
) for running client or server shell scripts with network connections
[ Download now: A system administrator's guide to automation. ]
How do we use socat?
The syntax for socat
is fairly simple:
socat [options] <address> <address>
You must provide the source and destination addresses for it to work. The syntax for these addresses is:
protocol:ip:port
Examples of using socat
Let's get started with some basic examples of using socat
for various connections.
1. Connect to TCP port 80 on the local or remote system:
# socat - TCP4:www.example.com:80
In this case, socat
transfers data between STDIO (-) and a TCP4 connection to port 80 on a host named www.example.com.
2. Use socat
as a TCP port forwarder:
For a single connection, enter:
# socat TCP4-LISTEN:81 TCP4:192.168.1.10:80
For multiple connections, use the fork
option as used in the examples below:
# socat TCP4-LISTEN:81,fork,reuseaddr TCP4:TCP4:192.168.1.10:80
This example listens on port 81, accepts connections, and forwards the connections to port 80 on the remote host.
# socat TCP-LISTEN:3307,reuseaddr,fork UNIX-CONNECT:/var/lib/mysql/mysql.sock
The above example listens on port 3307, accepts connections, and forwards the connections to a Unix socket on the remote host.
[ Learn how to manage your Linux environment for success. ]
3. Implement a simple network-based message collector:
# socat -u TCP4-LISTEN:3334,reuseaddr,fork OPEN:/tmp/test.log,creat,append
In this example, when a client connects to port 3334, a new child process is generated. All data sent by the clients is appended to the file /tmp/test.log
. If the file does not exist, socat
creates it. The option reuseaddr
allows an immediate restart of the server process.
4. Send a broadcast to the local network:
# socat - UDP4-DATAGRAM:224.255.0.1:6666,bind=:6666,ip-add-membership=224.255.0.1:eth0
In this case, socat
transfers data from stdin
to the specified multicast address using UDP over port 6666 for both the local and remote connections. The command also tells the interface eth0 to accept multicast packets for the given group.
Practical uses for socat
Socat
is a great tool for troubleshooting. It is also handy for easily making remote connections. Practically, I have used socat
for remote MySQL connections. In the example below, I demonstrate how I use socat
to connect my web application to a remote MySQL server by connecting over the local socket.
1. On my remote MySQL server, I enter:
# socat TCP-LISTEN:3307,reuseaddr,fork UNIX-CONNECT:/var/lib/mysql/mysql.sock &
This command starts socat
and configures it to listen by using port 3307.
2. On my webserver, I enter:
# socat UNIX-LISTEN:/var/lib/mysql/mysql.sock,fork,reuseaddr,unlink-early,user=mysql,group=mysql,mode=777 TCP:192.168.100.5:3307 &
The above command connects to the remote server 192.168.100.5 by using port 3307.
However, all communication will be done on the Unix socket /var/lib/mysql/mysql.sock
, and this makes it appear to be a local server.
Wrap up
socat
is a sophisticated utility and indeed an excellent tool for every sysadmin to get things done and for troubleshooting. Follow this link to read more examples of using socat.
[ Free online course: Red Hat Enterprise Linux technical overview. ]
Sobre o autor
I work as Unix/Linux Administrator with a passion for high availability systems and clusters. I am a student of performance and optimization of systems and DevOps. I have passion for anything IT related and most importantly automation, high availability, and security.
Mais como este
Navegue por canal
Automação
Últimas novidades em automação de TI para empresas de tecnologia, equipes e ambientes
Inteligência artificial
Descubra as atualizações nas plataformas que proporcionam aos clientes executar suas cargas de trabalho de IA em qualquer ambiente
Nuvem híbrida aberta
Veja como construímos um futuro mais flexível com a nuvem híbrida
Segurança
Veja as últimas novidades sobre como reduzimos riscos em ambientes e tecnologias
Edge computing
Saiba quais são as atualizações nas plataformas que simplificam as operações na borda
Infraestrutura
Saiba o que há de mais recente na plataforma Linux empresarial líder mundial
Aplicações
Conheça nossas soluções desenvolvidas para ajudar você a superar os desafios mais complexos de aplicações
Programas originais
Veja as histórias divertidas de criadores e líderes em tecnologia empresarial
Produtos
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Red Hat Cloud Services
- Veja todos os produtos
Ferramentas
- Treinamento e certificação
- Minha conta
- Suporte ao cliente
- Recursos para desenvolvedores
- Encontre um parceiro
- Red Hat Ecosystem Catalog
- Calculadora de valor Red Hat
- Documentação
Experimente, compre, venda
Comunicação
- Contate o setor de vendas
- Fale com o Atendimento ao Cliente
- Contate o setor de treinamento
- Redes sociais
Sobre a Red Hat
A Red Hat é a líder mundial em soluções empresariais open source como Linux, nuvem, containers e Kubernetes. Fornecemos soluções robustas que facilitam o trabalho em diversas plataformas e ambientes, do datacenter principal até a borda da rede.
Selecione um idioma
Red Hat legal and privacy links
- Sobre a Red Hat
- Oportunidades de emprego
- Eventos
- Escritórios
- Fale com a Red Hat
- Blog da Red Hat
- Diversidade, equidade e inclusão
- Cool Stuff Store
- Red Hat Summit