Skip to main content

How to close open ports in linux

· 2 min read

Introduction

Ports are virtual places within an operating system where network connections start and end. They help computers sort the network traffic they receive. This means they are entities in a computer networking which acts as a communication endpoint for identifying a given process or application on the a given operating system.

How to close open ports in linux

  • There are several ways to close open ports in linux.
  1. Using fuser

fuser -k 3001/tcp
info

-k -> for kill

  1. Using lsof

sudo kill $(sudo lsof -t -i:5000)
  • In place of 5000 you can specify your port number
tip

-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.

tip

-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.

See also

Conclusion

To summarize, closing open ports is one of the most fundamental tasks of a Linux system administrator for security reasons. Close all unwanted ports and configure firewalls such as UFW and FirewallD to open or block ports based on your needs. For more information.

Happy coding 🎉!