大网数据广告活动
服务器运维问答

Any way to prevent a port from being used/listened to?

· 52CE 我爱测

Say for some reason, on a Windows system I'm managing, I do not want any application to listen on a certain port, say for example 8000. The effect would be when a user tries to run an application to listen on that port, for examp…

问题原文

原帖链接 · SparedWhisle · CC BY-SA 4.0

Say for some reason, on a Windows system I'm managing, I do not want any application to listen on a certain port, say for example 8000.
The effect would be when a user tries to run an application to listen on that port, for example, python -m http.server 8000, he gets the usual error saying OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions. And when he checks if anything is already using that port using a command like netstat -ano|findstr 8000 he wouldn't find anything.
It's kind of like reserving, like saying "no one is allowed to use this port, cause I might need to use it for important business".
How should I go about doing that? Does Windows support this natively? Do I need 3rd party software to achieve this?

已采纳解答

原帖链接 · petitradisgris · CC BY-SA 4.0

You should look at the 'netsh' command-line utility. This will make 'reservation' of ports... Making them unbindable by unprivileged processes.

NETSH list reserved ports on ipv4 > tcp stack:

netsh interface ipv4 show excludedportrange protocol=tcp

NETSH reserve ports (template):

netsh int <ipv4|ipv6> Add excludedportrange [protocol=]tcp|udp [startport=]<integer> [numberofports=]<integer> [[store=]active|persistent]

EXAMPLE: NETSH, reserve port 8000 for ipv4 > tcp, use the command:

netsh int ipv4 add excludedportrange protocol=tcp startport=8000 numberofports=1

By default port reservations are persistent across reboots.

In all case, i suggest you to read the NETSH documentation and related usage and examples links

If the above solution is not working, you may try it :

Hope it helps

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-06-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。