How to Block Ports on a Computer

By Kristen Waters

A TCP or UDP port is the way that specific information gets passed into and out of a computer. For example, port 80 is used for HTTP traffic. Port numbers range from 0 to 65535 with ports 0 to 1024 reserved for specific types of traffic. When ports are not in use, they are a security risk. Traffic can get in and out without your knowledge. Blocking ports keeps unwanted traffic from passing through.

Things You'll Need

  • Windows XP with Service Pack 2, Windows Server 2003 or a Linux operating system

Windows XP or Windows Server 2003

Step 1

Download the \"Netdiag.exe\" utility from the Microsoft website.

Step 2

Install the utility.

Step 3

Open a Command Prompt window which will be found under the \"Accessories\" submenu of the \"Start\" menu.

Step 4

Type the command \"cd C:\\Program Files\\System Tools\" folder.

Step 5

Type the command \"netdiag /test:ipsec\" to check that there are no policies already in place.

Step 6

Type the command \"IPSeccmd.exe -w REG -p \"Block Filter\" -r \"Block Rule\" -f *=0: -n BLOCK –x\" to create a filter that blocks a particular port. Replace \"\" with the protocol and port number that is to be blocked (e.g. \"TCP 80\"). Replace \"\" with either \"Inbound\" or \"Outbound\" depending on whether you want to block inbound or outbound traffice. Replace \"\" with the port number and protocol (e.g. \"80:TCP\").

Step 7

Type the command \"netdiag /test:ipsec /debug\" to verify that the filter has been created.


Step 1

Open a terminal window. This is where you will type the following commands.

Step 2

Type the command \"su\" to switch to the root user.

Step 3

Type the command \"iptables -A INPUT -p tcp --destination-port -j DROP\" to block a particular port. Replace \"\" with the port number you want to block. For example, \"iptables -A INPUT -p tcp --destination-port 80 -j DROP\" will block port 80 for all traffic.

Step 4

Type the command \"exit\" to close the root session.