26

I've recently learned about the /dev/udp and /dev/tcp pseudo-devices here. Are they specific to some GNU/Linux distributions or can I find them on other unix systems?

Are they standardized in some way?

So far, I've been able to use them successfuly on OS X, Arch Linux and CentOS.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Antoine
  • 1,021
  • 1
  • 12
  • 13

2 Answers2

34

This is a feature of the shell and not the operating system.

So, for example,on Solaris 10 with ksh88 as the shell:

% cat < /dev/tcp/localhost/22
ksh: /dev/tcp/localhost/22: cannot open

However if we switch to bash:

% bash
bash-3.2$ cat < /dev/tcp/localhost/22
SSH-2.0-Sun_SSH_1.1.5

So bash interprets the /dev/tcp but ksh88 didn't.

On Solaris 11 with ksh93 as the shell:

% cat < /dev/tcp/localhost/22
SSH-2.0-Sun_SSH_2.2

So we can see it's very dependent on the shell in use.

Stephen Harris
  • 42,369
  • 5
  • 94
  • 123
  • 6
    Also depends on compile-time options -- not all bash builds will have it enabled. – Charles Duffy Sep 20 '16 at 22:00
  • @CharlesDuffy True enough; also true of ksh (ksh-88 tried to autodetect, but I'm not sure it worked properly). – Stephen Harris Sep 20 '16 at 23:47
  • 1
    Regarding the first sentence, [POSIX does standardize some basic shell features](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html) (although `/dev/tcp/...` isn't one of them). – Tanz87 Aug 04 '19 at 00:31
5

To add on, from the Bash Info node:

Bash handles several filenames specially when they are used in redirections, as described in the following table. If the operating system on which Bash is running provides these special files, bash will use them; otherwise it will emulate them internally with the behavior described below.

'/dev/fd/FD' If FD is a valid integer, file descriptor FD is duplicated.

'/dev/stdin' File descriptor 0 is duplicated.

'/dev/stdout' File descriptor 1 is duplicated.

'/dev/stderr' File descriptor 2 is duplicated.

'/dev/tcp/HOST/PORT' If HOST is a valid hostname or Internet address, and PORT is an integer port number or service name, Bash attempts to open the corresponding TCP socket.

'/dev/udp/HOST/PORT' If HOST is a valid hostname or Internet address, and PORT is an integer port number or service name, Bash attempts to open the corresponding UDP socket.

muru
  • 69,900
  • 13
  • 192
  • 292
extremeaxe5
  • 1,133
  • 9
  • 19