You are on page 1of 2

command line - How to close an open port in U... about:reader?url=https://askubuntu.com/questi...

askubuntu.com

command line - How to close an


open port in Ubuntu?
4 minutes

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.

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 commandline. Other way you may look for a
firewall solution (as isolating that port from network)

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.

1 of 2 2/3/18, 12:37 PM
command line - How to close an open port in U... about:reader?url=https://askubuntu.com/questi...

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.

answered Jan 25 '14 at 17:56

for closing listen 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.

answered Feb 9 '17 at 9:28

Not the answer you're looking for? Browse other


questions tagged command-line or ask your own
question.

2 of 2 2/3/18, 12:37 PM

You might also like