Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Networking

Bash /dev/tcp redirection

Reference for Bash /dev/tcp and /dev/udp: open network connections with ordinary redirections, test ports, send requests and read replies without nc or curl.

Bash can open network sockets by itself. When a redirection target looks like /dev/tcp/HOST/PORT (or /dev/udp/HOST/PORT), Bash does not open a file: it connects a TCP (or UDP) socket and uses it as the file descriptor. No nc, telnet or curl is needed, which makes it useful on minimal servers and containers.

These paths do not exist on disk. The feature is part of Bash itself (it must be compiled in, as on most Linux distributions); other shells such as dash or sh on Debian do not support it.

Terminal
exec 3<>/dev/tcp/HOST/PORT   # open a read/write connection on fd 3
exec 3>&-                    # close it

Try it

A small local server stands in for a remote service:

Bash A request and a reply over /dev/tcp

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Testing whether a port is open

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
exec FD<>/dev/tcp/HOST/PORT        # read and write
command >/dev/tcp/HOST/PORT        # send only
command </dev/tcp/HOST/PORT        # receive only
exec FD>&-                         # close the write side
exec FD<&-                         # close the read side
Part Description
HOST A host name or IP address (IPv4 or IPv6). Names are resolved by the normal resolver.
PORT A port number or a service name from /etc/services.
FD A file descriptor number, 3 to 9 are safe choices. Bash 4.1+ can pick one for you with : exec {fd}<>/dev/tcp/....
/dev/udp/HOST/PORT The same for UDP datagrams (no connection, no delivery guarantee).

Errors: if the connection fails, the redirection fails with Connection refused (or No route to host, or Connection timed out) and the command returns status 1. exec with a failing redirection can end a non-interactive shell, so wrap tests in a subshell: ( exec 3<>/dev/tcp/H/P ) 2>/dev/null.

Usage notes

  • Port checks in scripts. timeout 3 bash -c '</dev/tcp/HOST/PORT' returns 0 if the port accepts a connection and 1 otherwise; the timeout protects against hosts that silently drop packets.
  • Close the descriptor. An open connection lives until you close it with exec 3>&- or the shell exits. Closing the write side tells the server you are done sending.
  • Protocols with \r\n. HTTP, SMTP and similar protocols expect carriage return plus newline, so use printf 'text\r\n', not echo.
  • Only plain TCP. There is no TLS. For HTTPS use openssl s_client or curl.
  • Reading until the server hangs up. cat <&3 finishes when the server closes the connection; for HTTP/1.1 keep-alive connections it can hang, so ask for Connection: close or use HTTP/1.0.
  • Security. The same feature is used by reverse-shell one-liners. Treat unknown scripts containing /dev/tcp carefully, and note that restricted shells and set -r disable it.
  • Portability. It is a Bash feature, not POSIX. In #!/bin/sh scripts on Debian or Ubuntu it fails with "No such file or directory".