Freeing port in linux

Manually closing a port from commandline

I want to close an open port which is in listening mode between my client and server application. Is there any manual command line option in Linux to close a port? NOTE: I came to know that «only the application which owns the connected socket should close it, which will happen when the application terminates.» I don’t understand why it is only possible by the application which opens it . But I’m still eager to know if there is any other way to do it.

No, opened ports belong to the process which opened them, there is no control possible from outside. Which is a good thing, or all applications would have to anticipate their open ports (and files) being messed with. However, you can block traffic to a port by firewalling (iptables), but that will not close and give up the port for other use.

A lot of responders missed the point of this question. It is nonsense to declare that only the application that owns the port can disconnect it. I can disconnect it by walking up to the box and pulling the ethernet cable out of the socket, or by killing the application at the other end of the connection! The application must be written to handle this. So — how do you test to be sure the application is written properly without requiring physical intervention and/or control of the other computer?

«. there is no control possible from outside.» That’s an important remark, that’s guided me to the next question, how can i be the part of the process from the outside? GDB.

@JürgenStrobel There is indeed control possible from outside — both tcpkill and ss can do exactly what is asked for. Because opened ports do not truly belong to a process; they are kernel resources, with some rights assigned to a process, but still existing only at the kernel’s pleasure.

@tom-anderson DaleW: tcpkill is a firewalling tool, and I did mention this option. You can prevent traffic to a port, which is different than closing a port (socket).

15 Answers 15

I had same problem, the process must keep alive but the socket must close. Closing a socket in a running process is not impossible but difficult:

call close($fileDescriptor) //does not need ; at end. 

sudo lsof -np $pid gives me about 200 lines and I’m confused about how to find desired FD. In my case process is a Chrome tab and i’m trying to close opened websockets.

A row typically looks like: firefox 14812 szupervigyor 97u IPv4 32814564 0t0 TCP 192.168.2.4:40385->173.194.39.65:https (ESTABLISHED) as: process_name pid user fd[opened_for] protocol device inode protocol_data_toString

* A row typically looks like: firefox 14812 szupervigyor 97u IPv4 32814564 0t0 TCP 192.168.2.4:40385->173.194.39.65:https (ESTABLISHED) as: process_name pid user fd[opened_for] protocol device inode protocol_data_toString You need to know the remote ip address and find on the last col. In my example the 97 is the FileDescriptor. Searching is difficult if you opened multiple connection to the target host.

If you want to simulate the socket being closed by the remote end (e.g. the peer exiting) it’s better to use shutdown : call shutdown($fileDescriptor, 0) .

Читайте также:  Windows and linux загрузчик

You’re kind of asking the wrong question here. It isn’t really possible to simply «close a port» from outside the application that opened the socket listening on it. The only way to do this is to completely kill the process that owns the port. Then, in about a minute or two, the port will become available again for use. Here’s what’s going on (if you don’t care, skip to the end where I show you how to kill the process owning a particular port):

Ports are resources allocated by the OS to different processes. This is similar to asking the OS for a file pointer. However, unlike file pointers, only ONE process at a time may own a port. Through the BSD socket interface, processes can make a request to listen on a port, which the OS will then grant. The OS will also make sure no other process gets the same port. At any point, the process can release the port by closing the socket. The OS will then reclaim the port. Alternatively, if the process ends without releasing the port, the OS will eventually reclaim the port (though it won’t happen immediately: it’ll take a few minutes).

Now, what you want to do (simply close the port from the command-line), isn’t possible for two reasons. First, if it were possible, it would mean one process could simply steal away another process’s resource (the port). This would be bad policy, unless restricted to privileged processes. The second reason is it is unclear what would happen to the process that owned the port if we let it continue running. The process’s code is written assuming that it owns this resource. If we simply took it away, it would end up crashing on it’s own, so OS’s don’t let you do this, even if you’re a privileged process. Instead, you must simply kill them.

Anyway, here’s how to kill a process that owns a particular port:

That will output the line corresponding to the process holding port , for example:

tcp 0 0 *:8000 *:* LISTEN 4683/procHoldingPort 

In this case, procHoldingPort is the name of the process that opened the port, 4683 is its pid, and 8000 (note that it is TCP) is the port number it holds.

Then, look in the last column, you’ll see /. Then execute this:

If that doesn’t work (you can check by re-running the netstat command). Do this:

In general, it’s better to avoid sending SIGKILL if you can. This is why I tell you to try kill before kill -9 . Just using kill sends the gentler SIGTERM.

Like I said, it will still take a few minutes for the port to re-open if you do this. I don’t know a way to speed this up. If someone else does, I’d love to hear it.

Источник

How to Close open Ports manually in Ubuntu / Linux

When running any kind of server application like http or ftp server, or doing socket programming, it might so happen that a server program when recompiled/rerun fails to bind to a particular port number because that port number is already in use.

In such a case, you can either restart the system or close the port manually.

To close the port number manually first the process name/id has to be found out that is holding the port open and then use the kill command on that process.

Find pid with lsof — The lsof command can be used to find the pid and command name of the program or application that is currently using the port. Here is a quick example:

$ lsof -i :8888 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 8461 enlightened 11u IPv6 138527 0t0 UDP *:8888

In the above example it is seen that port 8888 is being held in use by the command java with pid 8461. Now kill the process by doing any of the following

$ kill 8461 or $ killall -9 8461 or $ killall -9 java

Find process/pid with netstat — The netstat command can also be used to find out which process is holding a certain port number

$ netstat -u -ap (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 *:18347 *:* - udp 0 0 localhost:11211 *:* - udp 0 0 localhost:36254 localhost:36254 ESTABLISHED - udp 0 0 localhost:domain *:* - udp 0 0 *:ipp *:* - udp 0 0 *:42038 *:* - udp 0 0 *:17500 *:* 4090/dropbox udp 0 0 *:mdns *:* - udp 0 0 localhost:58797 localhost:7777 ESTABLISHED 9831/ncat udp 0 0 localhost:42724 localhost:domain ESTABLISHED - udp6 0 0 [::]:46282 [::]:* - udp6 0 0 [::]:mdns [::]:* - udp6 0 0 [::]:9999 [::]:* 11598/java

The port we want to close here is 9999. And netstat shows that the pid is «11598» and command name is «java». Over here we used the -u for udp port. If its a tcp port then the «-u» switch is not needed.

Читайте также:  Аналог компас на linux

To make the search process easier, simply pipe the output of netstat to grep and look for the exact port number

$ sudo netstat -ap | grep :9050 tcp 0 0 localhost:9050 *:* LISTEN 1613/tor

Once the process id/name is found end it with the kill command.

Find pid with fuser — This is yet another command to find the pid/process holding a certain port number. The sytanx is as follows:

fuser -k -n protocol portno
$ fuser -k -n udp 7777 7777/udp: 11774

Conclusion

The above examples show how to find specific process and its pid that are using a given port number. Once you know the port number you can just kill that process and free the port.

Note that if the process was initially launched with root privileges then you would need root privileges to kill it as well.

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

Источник

Freeing up a TCP/IP Port on Linux

TCP/IP ports are used to connect and communicate with different devices and applications on a network. In Linux, sometimes a specific port may be in use by a particular application or process, preventing other applications from using it. In such cases, it is necessary to free up the port by stopping the process or application using it.

In this article, we will discuss how to free up a TCP/IP port on a Linux system. We will also discuss some common tools and commands that can be used to identify the process or application using a particular port and terminate it.

Identifying the Process or Application Using a Port

Before we can free up a port, we need to identify the process or application using it. There are several tools and commands that can be used to do this.

netstat tool

A netstat command is a useful tool for displaying network statistics and the status of network connections. It can be used to list all active TCP/IP connections, including the ports they are using.

Читайте также:  Аналог zona для linux

To list all active TCP/IP connections, use the following command —

This will display a list of all active TCP connections, along with the local and remote addresses, the state of the connection, and the PID (process ID) of the process using the connection.

To list only the ports being used by a specific process, use the following command —

For example, to list the ports being used by the Apache web server, use the following command

lsof tool

The lsof command (short for «list open files») is another useful tool for identifying the process or application using a particular port. It can be used to list all open files on a system, including network sockets and ports.

To list all open network sockets and their associated PIDs, use the following command —

To list only the ports being used by a specific process, use the following command —

For example, to list the ports being used by the Apache web server, use the following command

Freeing up the port

Once we have identified the process or application using a particular port, we can stop it to free up the port. There are several ways to do this, depending on the process or application.

Terminating the Process

If the process or application using the port is a standalone process, we can simply terminate it to free up the port. To do this, we need to find the PID of the process using the port, and then use the kill command to terminate it.

For example, if the Apache web server is using port 80, we can use the netstat command to find its PID

$ netstat -atn | grep apache tcp 0 0 . 80 . * LISTEN 1785/httpd

In this case, the PID of the Apache process is 1485. To terminate the process, use the following command

Stopping the Service

If the process or application using the port is a service, we can stop the service to free up the port. To do this, we can use the systemctl command.

For example, to stop the Apache web server service, use the following command

Closing the Connection

In some cases, the process or application using the port may be a network connection, rather than a standalone process or service. In such cases, we can use the netstat command to close the connection and free up the port. For example, to close a connection using port 80, use the following command

For example, to close a connection using port 80, use the following command —

$ netstat -atn | grep :80 tcp 0 0 192.168.0.43:80 192.168.0.44:56632 ESTABLISHED 1485/httpd

In this case, the connection is being used by the Apache web server (PID 1485). To close the connection, use the following command

$ netstat -atn -f inet -w 1 | grep :80 | grep ESTABLISHED | awk '' | awk -F '/' '' | xargs kill -9

This command will close all connections using port 80 that are in the ESTABLISHED state.

Conclusion

In this article, we have discussed how to free up a TCP/IP port on a Linux system. We have also discussed some common tools and commands that can be used to identify the process or application using a particular port, and terminate it. By following these steps, you can easily free up a port and allow other applications or processes to use it.

Источник

Оцените статью
Adblock
detector