最常见的互联网传输协议,包括传输控制协议( TCP ) 和用户数据包协议( UDP ) ,以及其他很少听说的网络协议都使用端口号进行通信会话,本文将介绍如何查看 Linux 系统中所有已开放端口信息。
此外,IP 地址、端口和协议(如TCP/UDP)的组合称为套接字,每个服务都必须有一个唯一的套接字。
以下是不同类别的端口:
- 0-1023 – 最为人熟知的端口,也称为系统端口。
- 1024-49151 – 注册端口,也称为用户端口。
- 49152-65535 – 动态端口,也称为专用端口。
在 Linux 系统上,可以使用 cat 命令查看 /etc/services 文件,以了解不同应用程序和端口/协议组合的信息:
$ cat /etc/services
OR
$ cat /etc/services | less
网络服务和端口
# /etc/services:
# $Id: services,v 1.48 2009/11/11 14:32:31 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2009-11-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994). Not all ports
# are included, only the more common ones.
#
# The latest IANA port assignments can be gotten from
# http://www.iana.org/assignments/port-numbers
# The Well Known Ports are those from 0 through 1023.
# The Registered Ports are those from 1024 through 49151
# The Dynamic and/or Private Ports are those from 49152 through 65535
#
# Each line describes one service, and is of the form:
#
# service-name port/protocol [aliases ...] [# comment]
tcpmux 1/tcp # TCP port service multiplexer
tcpmux 1/udp # TCP port service multiplexer
rje 5/tcp # Remote Job Entry
rje 5/udp # Remote Job Entry
echo 7/tcp
echo 7/udp
discard 9/tcp sink null
discard 9/udp sink null
systat 11/tcp users
systat 11/udp users
daytime 13/tcp
daytime 13/udp
qotd 17/tcp quote
qotd 17/udp quote
msp 18/tcp # message send protocol
msp 18/udp # message send protocol
chargen 19/tcp ttytst source
chargen 19/udp ttytst source
ftp-data 20/tcp
ftp-data 20/udp
# 21 is registered to ftp, but also used by fsp
ftp 21/tcp
ftp 21/udp fsp fspd
ssh 22/tcp # The Secure Shell (SSH) Protocol
ssh 22/udp # The Secure Shell (SSH) Protocol
telnet 23/tcp
telnet 23/udp
如果要查看所有已开放端口或当前正在运行的端口,包括TCP和UDP,我们可以使用 netstat,它是一个监控网络连接和统计相关信息的强大工具。
使用 Netstat 命令列出所有网络端口
$ netstat -lntu
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::25 :::* LISTEN
udp 0 0 0.0.0.0:68 0.0.0.0:*
参数使用,
- -l – 只显示监听接口
- -n – 显示端口号
- -t – 启用的 tcp 端口列表
- -u – 启用的 udp 端口列表
还可以使用 ss 命令,用于检查 Linux 系统中的接口。运行以下命令以列出所有打开的 TCP 和 UCP 端口:
使用 ss 命令列出所有网络端口
$ ss -lntu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 *:68 *:*
tcp LISTEN 0 128 :::22 :::*
tcp LISTEN 0 128 *:22 *:*
tcp LISTEN 0 50 *:3306 *:*
tcp LISTEN 0 128 :::80 ::*
tcp LISTEN 0 100 :::25 :::*
tcp LISTEN 0 100 *:25
使用 lsof 命令查看所有已开放端口
前面,我们介绍了如何使用 netstat 命令和 ss 命令列出端口。两个命令都可以显示正在运行的服务端口。在这里,我们将介绍 lsof 命令。LSOF 代表列出打开的文件,用于列出系统中所有已打开的文件和进程。
让我们看看下面的命令:
sudo lsof -i -P -n | grep LISTEN
相关参数介绍:
- -i : 用于列出特定端口的所有正在运行的进程;
- -P:它将端口号转换为网络文件的端口名;
- -n:它将网络名称转换为网络文件的主机名。
Grep 命令用于搜索特定的关键词。在这里,它将显示处于 LISTEN 状态的端口。
使用 ps 命令查看所有开放的端口
执行下面的命令:
ps -aux
总之,理解计算机网络中端口的概念,对于系统和网络运营人员来说是非常重要的。通过本文介绍的内容,大家就可以掌握如何在 Linux 系统查看所有开放端口的列表了。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/find-open-ports-in-linux/