50

I need a command to list all open ports in my PC, and another command to close a port.

Any suggestions?

I need to close some applications' port.

7 Answers 7

54

netstat can be used to see the ports stat.

sudo netstat -lnp

To list all Listening ports Numbers with the Process responsible on each one. Terminate or kill the process to close port. (kill, pkill ...)

Without process termination, It is not possible! . See Manually closing a port from command line. Other way you may look for a firewall solution (as isolating that port from network)

2
  • i need to close the port but without killing the application
    – nux
    Jan 24, 2014 at 19:15
  • It's possible to close a port as an external port.
    – sergzach
    Sep 25, 2017 at 12:41
44

for closing open port in ubuntu you can use below command

sudo kill $(sudo lsof -t -i:3000)

in place of 3000 you can specify your port number

lsof command will give information about file opened by process

-t : This flag specifies that lsof should produce terse output with process identifiers only and no header - e.g., so that the output may be piped to kill(1). This option selects the -w option.

-i : This flag selects the listing of files any of whose Internet address matches the address specified in i. If no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files.

2
  • 5
    sudo kill -9 $(sudo lsof -t -i:3000) worked for me
    – jim smith
    Sep 8, 2021 at 19:24
  • something is bound to the port but not listed (even I'm using sudo). I can tell by opening the port 433 in the browser. It will say "404 page not found", but nothing's bound to that port.
    – france1
    Aug 19, 2022 at 15:35
24
sudo ufw allow 22

sudo ufw deny 22
9

You can use iptables to block the port on the network level without having to close the application. The port would still appear open, but will be unreachable.

alternatively, this is dependent on the application, some permit to disable some port ( think dovecot and the pop3 or imap port ), and some cannot. Some application can also be configured to listen only on localhost or a specific address.

6

You can use netstat -nalp and lsof -i:port tools to identify process/binaries behind open port.

If you want to close port you have to kill process or stop relative service.If you want run services only for your local box you can configure respective service to listen on localhost/127.0.0.1 not on all available (0.0.0.0) ips.

5

To show lists of all open ports:

netstat -lnp

To close an open port:

fuser -k port_no/tcp

example:

fuser -k 8080/tcp

In both you can use sudo if needed.

1
  • For some cases the second command returns the following error: bash: fuser: command not found
    – Alexey Sh.
    Feb 12 at 4:01
2

If your port opened due to running a service, like vsftpd for ftp service, you can stop and then disable the service so that executable file related to the service will be killed too. in debian base systems you can run bellow commands to destruct a service:

service SERVICENAME stop
systemctl disable SERVICENAME

GoodLuck

1
  • Thank you. I checked the service name by sudo netstat -lnp and ran your suggested commands to stop the port 22 :) Mar 6, 2019 at 7:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .