How to use /dev/tcp and /dev/udp sockets in Linux ?

Today we will look at an interesting feature to use linux TCP and UDP port checking process.

Below two virtual device map provided by Linux.
/dev/tcp/$host/$port
/dev/udp/$host/$port

1. How to use /dev/tcp and /dev/udp vitual paths ?

### Checking DNS 53 port on my local system.
$ echo > /dev/tcp/localhost/53 && echo GOOD || echo "NOT Good"
-bash: connect: Connection refused
-bash: /dev/tcp/localhost/53: Connection refused
NOT Good

### Checking ssh 22 port on my local system
$ echo > /dev/tcp/localhost/22 && echo GOOD || echo "NOT GOOD"
GOOD
$ 

Similarly we can use it to verify UDP port connections.

$ echo > /dev/udp/127.0.0.1/22 && echo GOOD || echo "Not Good"
GOOD

2. Why i see all UDP connections good and TCP connection rejected ?
UDP or User DataGram Protocol is a connectionless protocol, it does not wait for reply packet, hence all packets on UDP is successful.
Hence the tip is to use TCP vitual path i.e. /dev/tcp/ to verify if the port is open and receiving connections.

$ echo > /dev/udp/127.0.0.1/19 && echo GOOD || echo "Not Good"
GOOD
$ echo > /dev/udp/127.0.0.1/18 && echo GOOD || echo "Not Good"
GOOD

$ echo > /dev/tcp/127.0.0.1/18 && echo GOOD || echo "Not Good"
-bash: connect: Connection refused
-bash: /dev/tcp/127.0.0.1/18: Connection refused
Not Good

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments