端口取值范围
一般用到的是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端口
-
Search for “PowerShell” in the start menu. Click the Windows PowerShell app.
-
In the PowerShell prompt window enter
Test-NetConnection <address> -p <port_number>