Use the kill command in combination with the lsof (list of open files) command to kill process listening to a particular port. As described in a previous post, the lsof command helps us identify which process is listening to a particular port - Just pass the result of the lsof command to the kill command.
The lsof (list open files) command returns the user processes that are actively using a file system. It is sometimes helpful in determining why a file system remains in use and cannot be unmounted.
sudo lsof -c ssh -c init. lsof provides a list of the files that have been opened by either of the processes provided on the command line.
There may be many child processes of a process and this process can also be termed as the parent process. To find out the list of files opened by parent process Id lsof command is used with the option -R.
To show all networking related to a given port
:
lsof -iTCP -i :port
lsof -i :22
To show connections to a specific host, use @host
lsof [email protected]
Show connections based on the host and the port using @host:port
lsof [email protected]:22
grep
ping for LISTEN
shows what ports your system is waiting for connections on:
lsof -i| grep LISTEN
Show what a given user has open using -u
:
lsof -u daniel
See what files and network connections a command is using with -c
lsof -c syslog-ng
The -p
switch lets you see what a given process ID has open, which is good for learning more about unknown processes:
lsof -p 10075
The -t
option returns just a PID
lsof -t -c Mail
Using the -t
and -c
options together you can HUP
processes
kill -HUP $(lsof -t -c sshd)
You can also use the -t
with -u
to kill everything a user has open
kill -9 $(lsof -t -u daniel)
lsof -i :port
will tell you what programs are listening on a specific port.
lsof -i
will provide a list of open network sockets. The -n
option will prevent DNS lookups, which is useful when your network connection is slow or unreliable.
lsof +D /some/directory
Will display recursively all the files opened in a directory. +d for just the top-level.
This is useful when you have high wait% for IO, correlated to use on a particular FS and want to see which processes are chewing up your io.
See what files a running application or daemon has open:
lsof -p pid
Where pid is the process ID of the application or daemon.
lsof +f -- /mountpoint
lists the processes using files on the mount mounted at /mountpoint. Particularly useful for finding which process(es) are using a mounted USB stick or CD/DVD.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With