加入收藏 | 设为首页 | 会员中心 | 我要投稿 财气旺网 - 财气网 (https://www.caiqiwang.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

如何在Linux中查看进程占用的端口号

发布时间:2018-10-16 04:02:13 所属栏目:Windows 来源:Prakash Subramanian
导读:【新品产上线啦】51CTO播客,随时随地,碎片化学习 对于 Linux 系统管理员来说,清楚某个服务是否正确地绑定或监听某个端口,是至关重要的。如果你需要处理端口相关的问题,这篇文章可能会对你有用。 端口是 Linux 系统上特定进程之间逻辑连接的标识,包括
副标题[/!--empirenews.page--] 【新品产上线啦】51CTO播客,随时随地,碎片化学习

如何在Linux中查看进程占用的端口号

对于 Linux 系统管理员来说,清楚某个服务是否正确地绑定或监听某个端口,是至关重要的。如果你需要处理端口相关的问题,这篇文章可能会对你有用。

端口是 Linux 系统上特定进程之间逻辑连接的标识,包括物理端口和软件端口。由于 Linux 操作系统是一个软件,因此本文只讨论软件端口。软件端口始终与主机的 IP 地址和相关的通信协议相关联,因此端口常用于区分应用程序。大部分涉及到网络的服务都必须打开一个套接字来监听传入的网络请求,而每个服务都使用一个独立的套接字。

套接字是和 IP 地址、软件端口和协议结合起来使用的,而端口号对传输控制协议(TCP)和用户数据报协议(UDP)协议都适用,TCP 和 UDP 都可以使用 0 到 65535 之间的端口号进行通信。

以下是端口分配类别:

  • 0 - 1023: 常用端口和系统端口
  • 1024 - 49151: 软件的注册端口
  • 49152 - 65535: 动态端口或私有端口

在 Linux 上的 /etc/services 文件可以查看到更多关于保留端口的信息。

  1. # less /etc/services
  2. # /etc/services:
  3. # $Id: services,v 1.55 2013/04/14 ovasik Exp $
  4. #
  5. # Network services, Internet style
  6. # IANA services version: last updated 2013-04-10
  7. #
  8. # Note that it is presently the policy of IANA to assign a single well-known
  9. # port number for both TCP and UDP; hence, most entries here have two entries
  10. # even if the protocol doesn't support UDP operations.
  11. # Updated from RFC 1700, ``Assigned Numbers'' (October 1994). Not all ports
  12. # are included, only the more common ones.
  13. #
  14. # The latest IANA port assignments can be gotten from
  15. # http://www.iana.org/assignments/port-numbers
  16. # The Well Known Ports are those from 0 through 1023.
  17. # The Registered Ports are those from 1024 through 49151
  18. # The Dynamic and/or Private Ports are those from 49152 through 65535
  19. #
  20. # Each line describes one service, and is of the form:
  21. #
  22. # service-name port/protocol [aliases ...] [# comment]
  23.  
  24. tcpmux 1/tcp # TCP port service multiplexer
  25. tcpmux 1/udp # TCP port service multiplexer
  26. rje 5/tcp # Remote Job Entry
  27. rje 5/udp # Remote Job Entry
  28. echo 7/tcp
  29. echo 7/udp
  30. discard 9/tcp sink null
  31. discard 9/udp sink null
  32. systat 11/tcp users
  33. systat 11/udp users
  34. daytime 13/tcp
  35. daytime 13/udp
  36. qotd 17/tcp quote
  37. qotd 17/udp quote
  38. msp 18/tcp # message send protocol (historic)
  39. msp 18/udp # message send protocol (historic)
  40. chargen 19/tcp ttytst source
  41. chargen 19/udp ttytst source
  42. ftp-data 20/tcp
  43. ftp-data 20/udp
  44. # 21 is registered to ftp, but also used by fsp
  45. ftp 21/tcp
  46. ftp 21/udp fsp fspd
  47. ssh 22/tcp # The Secure Shell (SSH) Protocol
  48. ssh 22/udp # The Secure Shell (SSH) Protocol
  49. telnet 23/tcp
  50. telnet 23/udp
  51. # 24 - private mail system
  52. lmtp 24/tcp # LMTP Mail Delivery
  53. lmtp 24/udp # LMTP Mail Delivery

可以使用以下六种方法查看端口信息。

  • ss:可以用于转储套接字统计信息。
  • netstat:可以显示打开的套接字列表。
  • lsof:可以列出打开的文件。
  • fuser:可以列出那些打开了文件的进程的进程 ID。
  • nmap:是网络检测工具和端口扫描程序。
  • systemctl:是 systemd 系统的控制管理器和服务管理器。

以下我们将找出 sshd 守护进程所使用的端口号。

方法 1:使用 ss 命令

ss 一般用于转储套接字统计信息。它能够输出类似于 netstat 输出的信息,但它可以比其它工具显示更多的 TCP 信息和状态信息。

(编辑:财气旺网 - 财气网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!