linux中获取未使用的端口

本文详细介绍了端口取值范围,从系统端口到用户自定义端口的区别,展示了如何在Ubuntu和Windows上使用netstat和PowerShell检查端口状态,以及获取未使用端口的方法。此外,还涵盖了常见命令和端口选择策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

端口取值范围

一般用到的是1到65535,其中0不使用,1-1023为系统端口,也叫BSD保留端口;1024-65535为用户端口,又分为: BSD临时端口(1024-5000)和BSD服务器(非特权)端口(5001-65535).
0-1023: BSD保留端口,也叫系统端口,这些端口只有系统特许的进程才能使用;
1024-5000: BSD临时端口,一般的应用程序使用1024到4999来进行通讯;
5001-65535: BSD服务器(非特权)端口,用来给用户自定义端口.

常见命令

Ubuntu查看端口使用情况,使用netstat命令:

查看已经连接的服务端口(ESTABLISHED)

netstat -a

查看所有的服务端口(LISTEN,ESTABLISHED)

netstat -ap

查看指定端口,可以结合grep命令:

netstat -ap | grep 8080

也可以使用lsof命令:

lsof -i:8888

若要关闭使用这个端口的程序,使用kill + 对应的pid

kill -9 PID号

ps:kill就是给某个进程id发送了一个信号。默认发送的信号是SIGTERM,而kill -9发送的信号是SIGKILL,即exit。exit信号不会被系统阻塞,所以kill -9能顺利杀掉进程。

获取未使用端口号

端口状态:
LISTEN: The socket is listening for incoming connections. Foreign address is not relevant for this line
ESTABLISHED: The socket has an established connection. Foreign address in the address of the remote end point of the socket.
CLOSE_WAIT: The remote end has shut down, waiting for the socket to close.

windows上ping端口

  1. Search for “PowerShell” in the start menu. Click the Windows PowerShell app.

  2. In the PowerShell prompt window enter

Test-NetConnection <address> -p <port_number>

在这里插入图片描述

相关链接1
相关链接2
相关链接3
相关链接4
相关链接5
相关链接6
相关链接7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值