How to manually close a port

45

Solution 1

You can't close an open socket just like this. Ideally, you would just kill the process that has established the connection.

Check your connections with lsof (netstat won't show the process), filtering the output with whatever connection status you want:

lsof -i
lsof -i | grep LISTEN
lsof -i | grep ESTABLISHED

Or, to get the port, e.g. 17500:

lsof -i:17500

Then, just kill the process. For example:

$ lsof -i | grep "Skype"
Skype     438 werner    9u  IPv4 0xffffff801dd0c640      0t0  UDP localhost:52218
Skype     438 werner   42u  IPv4 0xffffff80231a7a08      0t0  TCP *:29429 (LISTEN)
Skype     438 werner   43u  IPv4 0xffffff8022e18a40      0t0  UDP *:29429

Kill Skype:

killall Skype

Note though that this won't prevent the connections from being made – something you have to specify in your Firewall preferences.

Solution 2

Can also use the use the fuser or netstat commands.

fuser syntax is

fuser -k -n protocol portno

Example:

$ fuser -k -n udp 7777

7777/udp:            11774

The number 11774 is the pid.

netstat example:

$ sudo netstat -ap | grep :9050

tcp        0      0 localhost:9050          *:*       LISTEN      1613/tor

The number 1613 is the pid and "tor" is the process name.

Once you have the pid just end it using the kill or killall command

kill pid

or

killall -9 command_name

Share:
45

Related videos on Youtube

Anmol Singh Jaggi
Author by

Anmol Singh Jaggi

Updated on September 18, 2022

Comments

  • Anmol Singh Jaggi
    Anmol Singh Jaggi over 1 year

    Drools has functionality for declaring new data types in the rule file itself, instead of doing it in the java file.

    But how can we define methods for that data type?

    For example, if I declare the following type in the rule file:

    declare Address
       number : int
       streetName : String
       city : String
    end
    

    How can I add a method to it:

    public int doubleNumber()
    {
      number = number * 2;
    }
    
    • Darael
      Darael about 12 years
      Do you want to close extant connections, prevent further ones on the same port, or both?
  • Eroen
    Eroen over 11 years
    Your instructions are for showing the perceived symptoms described in the question, open ports belonging to processes. You left out how to accomplish the desired task, to close the port.
  • Michael W
    Michael W over 7 years
    Still doesn't answer how to close a port. But rather kill the process that created/opened it. What if a process holds >1 open port. How could I kill one but not the other
  • Anmol Singh Jaggi
    Anmol Singh Jaggi over 7 years
    Thanks! Do you, by any chance, know the solution to this problem ?