Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to close a socket by using some sort of shell command/program? I've seen plenty of examples of manipulating sockets using C and other languages but have been unable to find a program dedicated to it.

This is mostly out of curiosity rather than any real need. Although it might be useful for stress-testing my application or closing a tcp socket that I don't want to be open.

I am mainly interested in Linux/Unix, but a way to do this on Windows would be nice also.

share|improve this question
up vote 5 down vote accepted

You can close a socket in sh just like any other file: exec 42>&- where 42 is the file descriptor. What you can't do (except in a few shells that provide an extension for it) is open a socket. But of course that closes the socket in the shell, not in another process.

Closing a socket in a running process would disrupt its behavior in a way that the author of the program is not supposed to expect — it's like going in and modifying a piece of memory. Still, it can be done by connecting to the process with a debugger and making it execute a socket closing API call (that would be the close or shutdown system call on unix).

share|improve this answer
Just a note: it seems that bash can open TCP ports. Also as a sidenote, zsh doesn't support /dev/tcp – Jason Axelson Aug 31 '12 at 0:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.