[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


500: Simply stop port listening

Posted on 2006-07-21
Medium Priority
Last Modified: 2009-04-07
Is there command that I can type in the command prompt to stop a port from listening?? Not permanently.
I have a script, which when run, opens a few ports for listening....but it doesn't close them ever and I can't change the script so that's why I want to do it from the cammand line.

Many thanks in advance
Question by:smotbd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 28

Expert Comment

ID: 17152850
provided that you are using ptables:

iptables -I INPUT -p tcp -m tcp --dport <the_port> -j DROP

and to remove it
iptables iD INPUT 1

this will still leave the port listening, but it will prevent connections to it.
anoter way would be to open some bogus program to listen on those ports, but that might raise errors in the script if it hadn't been written to deal with such issues.

Author Comment

ID: 17153670
I don't and won't use iptables sorry.

Is there not a simple command or script that can be run?? It's only a setting somewhere!

LVL 28

Expert Comment

ID: 17153817
well, if you are so sure that it is only a setting somewhere, what is thet setting? or where is somewhere?

you cannot have a OS setting that says that an applicaiton cannot bind to a specific port.
you either do that from the application or you block the access to that port for other applications. you don't want to do either of that.
and may I know:
- why is it a problem that the script binds to some ports?
- why don't you want to use iptables? or other kernal-firewalling module/app?
- why don't you want to modify the script?

maybe the problem is somewhere else and thus the solution is not even close to denying access to a port.
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

LVL 40

Expert Comment

ID: 17154142
If you open a port it is listening, if you close a port it isn't

If you can't change the regime on opening/closing then blocking the the only other option.
There is no other option to enable/disable listening.
LVL 24

Expert Comment

ID: 17161392
may be you should post your script here?

Author Comment

ID: 17161634
The script is a PHP script which simply acts as a server listening on particular ports.
I start this PHP script via the url on a web browser..... If I click on the cross on my web browser, the script stops running. I thought then that the ports would stop listening since the script has stopped. The application has stopped yet the ports are still listening. How?
So I concluded this: when the application is ran, the functions socket_create/listen must set a value of some sort to set these ports to listening as oppose to not listening!! So then, when I stop the script nothing changes. I know that socket_close or what have you will close this sockeet.....but I just wondered if there was a command that one could enter in to the command line to stop these ports set to listening.

After all, computers, of course, are just electronic machines. They don't know about Linux and
menus, they don't know about programs or instructions, and they don't even know about 1s and 0s. All
that is really going on is that voltage is being measured at various places on an integrated circuit. I thought that one could do anything under linux and all I want to do is stop a port listening!!

Anyway,, it's just a question out of curiosity  more than anything....but if I'm talking nonsence then I apologise!!
LVL 24

Accepted Solution

slyong earned 375 total points
ID: 17161676
I would suggest you detect the browser close event and close the socket properly.  There are some information here: http://www.linuxforums.org/forum/misc/1871-terminating-open-ports.html that state using lsof -i you can find out the process associated with the port.  Not sure if that will work with PHP.

Author Comment

ID: 17176478
yeh i do need to close ports properly............................
 lsof -i detected the ports left open....and their pid so I killed them thanks!!

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question