自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Linux中查看進程占用的端口號

系統(tǒng) Linux 系統(tǒng)運維
對于 Linux 系統(tǒng)管理員來說,清楚某個服務是否正確地綁定或監(jiān)聽某個端口,是至關重要的。如果你需要處理端口相關的問題,這篇文章可能會對你有用。

[[245396]]

對于 Linux 系統(tǒng)管理員來說,清楚某個服務是否正確地綁定或監(jiān)聽某個端口,是至關重要的。如果你需要處理端口相關的問題,這篇文章可能會對你有用。

端口是 Linux 系統(tǒng)上特定進程之間邏輯連接的標識,包括物理端口和軟件端口。由于 Linux 操作系統(tǒng)是一個軟件,因此本文只討論軟件端口。軟件端口始終與主機的 IP 地址和相關的通信協(xié)議相關聯(lián),因此端口常用于區(qū)分應用程序。大部分涉及到網(wǎng)絡的服務都必須打開一個套接字來監(jiān)聽傳入的網(wǎng)絡請求,而每個服務都使用一個獨立的套接字。

套接字是和 IP 地址、軟件端口和協(xié)議結合起來使用的,而端口號對傳輸控制協(xié)議(TCP)和用戶數(shù)據(jù)報協(xié)議(UDP)協(xié)議都適用,TCP 和 UDP 都可以使用 0 到 65535 之間的端口號進行通信。

以下是端口分配類別:

  • 0 - 1023: 常用端口和系統(tǒng)端口
  • 1024 - 49151: 軟件的注冊端口
  • 49152 - 65535: 動態(tài)端口或私有端口

在 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:可以用于轉儲套接字統(tǒng)計信息。
  • netstat:可以顯示打開的套接字列表。
  • lsof:可以列出打開的文件。
  • fuser:可以列出那些打開了文件的進程的進程 ID。
  • nmap:是網(wǎng)絡檢測工具和端口掃描程序。
  • systemctl:是 systemd 系統(tǒng)的控制管理器和服務管理器。

以下我們將找出 sshd 守護進程所使用的端口號。

方法 1:使用 ss 命令

ss 一般用于轉儲套接字統(tǒng)計信息。它能夠輸出類似于 netstat 輸出的信息,但它可以比其它工具顯示更多的 TCP 信息和狀態(tài)信息。

它還可以顯示所有類型的套接字統(tǒng)計信息,包括 PACKET、TCP、UDP、DCCP、RAW、Unix 域等。

  1. # ss -tnlp | grep ssh
  2. LISTEN 0 128 *:22 *:* users:(("sshd",pid=997,fd=3))
  3. LISTEN 0 128 :::22 :::* users:(("sshd",pid=997,fd=4))

也可以使用端口號來檢查。

  1. # ss -tnlp | grep ":22"
  2. LISTEN 0 128 *:22 *:* users:(("sshd",pid=997,fd=3))
  3. LISTEN 0 128 :::22 :::* users:(("sshd",pid=997,fd=4))

方法 2:使用 netstat 命令

netstat 能夠顯示網(wǎng)絡連接、路由表、接口統(tǒng)計信息、偽裝連接以及多播成員。

默認情況下,netstat 會列出打開的套接字。如果不指定任何地址族,則會顯示所有已配置地址族的活動套接字。但 netstat 已經(jīng)過時了,一般會使用 ss 來替代。

  1. # netstat -tnlp | grep ssh
  2. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 997/sshd
  3. tcp6 0 0 :::22 :::* LISTEN 997/sshd

也可以使用端口號來檢查。

  1. # netstat -tnlp | grep ":22"
  2. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1208/sshd
  3. tcp6 0 0 :::22 :::* LISTEN 1208/sshd

方法 3:使用 lsof 命令

lsof 能夠列出打開的文件,并列出系統(tǒng)上被進程打開的文件的相關信息。

  1. # lsof -i -P | grep ssh
  2. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  3. sshd 11584 root 3u IPv4 27625 0t0 TCP *:22 (LISTEN)
  4. sshd 11584 root 4u IPv6 27627 0t0 TCP *:22 (LISTEN)
  5. sshd 11592 root 3u IPv4 27744 0t0 TCP vps.2daygeek.com:ssh->103.5.134.167:49902 (ESTABLISHED)

也可以使用端口號來檢查。

  1. # lsof -i tcp:22
  2. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  3. sshd 1208 root 3u IPv4 20919 0t0 TCP *:ssh (LISTEN)
  4. sshd 1208 root 4u IPv6 20921 0t0 TCP *:ssh (LISTEN)
  5. sshd 11592 root 3u IPv4 27744 0t0 TCP vps.2daygeek.com:ssh->103.5.134.167:49902 (ESTABLISHED)

方法 4:使用 fuser 命令

fuser 工具會將本地系統(tǒng)上打開了文件的進程的進程 ID 顯示在標準輸出中。

  1. # fuser -v 22/tcp
  2. USER PID ACCESS COMMAND
  3. 22/tcp: root 1208 F.... sshd
  4. root 12388 F.... sshd
  5. root 49339 F.... sshd

方法 5:使用 nmap 命令

nmap(“Network Mapper”)是一款用于網(wǎng)絡檢測和安全審計的開源工具。它最初用于對大型網(wǎng)絡進行快速掃描,但它對于單個主機的掃描也有很好的表現(xiàn)。

nmap 使用原始 IP 數(shù)據(jù)包來確定網(wǎng)絡上可用的主機,這些主機的服務(包括應用程序名稱和版本)、主機運行的操作系統(tǒng)(包括操作系統(tǒng)版本等信息)、正在使用的數(shù)據(jù)包過濾器或防火墻的類型,以及很多其它信息。

  1. # nmap -sV -p 22 localhost
  2.  
  3. Starting Nmap 6.40 ( http://nmap.org ) at 2018-09-23 12:36 IST
  4. Nmap scan report for localhost (127.0.0.1)
  5. Host is up (0.000089s latency).
  6. Other addresses for localhost (not scanned): 127.0.0.1
  7. PORT STATE SERVICE VERSION
  8. 22/tcp open ssh OpenSSH 7.4 (protocol 2.0)
  9.  
  10. Service detection performed. Please report any incorrect results at http://nmap.org/submit/ .
  11. Nmap done: 1 IP address (1 host up) scanned in 0.44 seconds

方法 6:使用 systemctl 命令

systemctl 是 systemd 系統(tǒng)的控制管理器和服務管理器。它取代了舊的 SysV 初始化系統(tǒng)管理,目前大多數(shù)現(xiàn)代 Linux 操作系統(tǒng)都采用了 systemd。

推薦閱讀:

  1. # systemctl status sshd
  2. sshd.service - OpenSSH server daemon
  3. Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
  4. Active: active (running) since Sun 2018-09-23 02:08:56 EDT; 6h 11min ago
  5. Docs: man:sshd(8)
  6. man:sshd_config(5)
  7. Main PID: 11584 (sshd)
  8. CGroup: /system.slice/sshd.service
  9. └─11584 /usr/sbin/sshd -D
  10.  
  11. Sep 23 02:08:56 vps.2daygeek.com systemd[1]: Starting OpenSSH server daemon...
  12. Sep 23 02:08:56 vps.2daygeek.com sshd[11584]: Server listening on 0.0.0.0 port 22.
  13. Sep 23 02:08:56 vps.2daygeek.com sshd[11584]: Server listening on :: port 22.
  14. Sep 23 02:08:56 vps.2daygeek.com systemd[1]: Started OpenSSH server daemon.
  15. Sep 23 02:09:15 vps.2daygeek.com sshd[11589]: Connection closed by 103.5.134.167 port 49899 [preauth]
  16. Sep 23 02:09:41 vps.2daygeek.com sshd[11592]: Accepted password for root from 103.5.134.167 port 49902 ssh2

以上輸出的內(nèi)容顯示了最近一次啟動 sshd 服務時 ssh 服務的監(jiān)聽端口。但它不會將最新日志更新到輸出中。

  1. # systemctl status sshd
  2. sshd.service - OpenSSH server daemon
  3. Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
  4. Active: active (running) since Thu 2018-09-06 07:40:59 IST; 2 weeks 3 days ago
  5. Docs: man:sshd(8)
  6. man:sshd_config(5)
  7. Main PID: 1208 (sshd)
  8. CGroup: /system.slice/sshd.service
  9. ├─ 1208 /usr/sbin/sshd -D
  10. ├─23951 sshd: [accepted]
  11. └─23952 sshd: [net]
  12.  
  13. Sep 23 12:50:36 vps.2daygeek.com sshd[23909]: Invalid user pi from 95.210.113.142 port 51666
  14. Sep 23 12:50:36 vps.2daygeek.com sshd[23909]: input_userauth_request: invalid user pi [preauth]
  15. Sep 23 12:50:37 vps.2daygeek.com sshd[23911]: pam_unix(sshd:auth): check pass; user unknown
  16. Sep 23 12:50:37 vps.2daygeek.com sshd[23911]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=95.210.113.142
  17. Sep 23 12:50:37 vps.2daygeek.com sshd[23909]: pam_unix(sshd:auth): check pass; user unknown
  18. Sep 23 12:50:37 vps.2daygeek.com sshd[23909]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=95.210.113.142
  19. Sep 23 12:50:39 vps.2daygeek.com sshd[23911]: Failed password for invalid user pi from 95.210.113.142 port 51670 ssh2
  20. Sep 23 12:50:39 vps.2daygeek.com sshd[23909]: Failed password for invalid user pi from 95.210.113.142 port 51666 ssh2
  21. Sep 23 12:50:40 vps.2daygeek.com sshd[23911]: Connection closed by 95.210.113.142 port 51670 [preauth]
  22. Sep 23 12:50:40 vps.2daygeek.com sshd[23909]: Connection closed by 95.210.113.142 port 51666 [preauth]

大部分情況下,以上的輸出不會顯示進程的實際端口號。這時更建議使用以下這個 journalctl 命令檢查日志文件中的詳細信息。

  1. # journalctl | grep -i "openssh\|sshd"
  2. Sep 23 02:08:56 vps138235.vps.ovh.ca sshd[997]: Received signal 15; terminating.
  3. Sep 23 02:08:56 vps138235.vps.ovh.ca systemd[1]: Stopping OpenSSH server daemon...
  4. Sep 23 02:08:56 vps138235.vps.ovh.ca systemd[1]: Starting OpenSSH server daemon...
  5. Sep 23 02:08:56 vps138235.vps.ovh.ca sshd[11584]: Server listening on 0.0.0.0 port 22.
  6. Sep 23 02:08:56 vps138235.vps.ovh.ca sshd[11584]: Server listening on :: port 22.
  7. Sep 23 02:08:56 vps138235.vps.ovh.ca systemd[1]: Started OpenSSH server daemon. 
責任編輯:龐桂玉 來源: Linux中國
相關推薦

2010-08-18 15:56:28

DB2占用端口號

2019-07-07 08:36:31

Linux命令端口號

2023-10-11 12:59:55

Ping端口

2022-11-15 21:21:06

Linux中國

2017-01-12 09:31:07

Windows端口進程

2019-12-16 09:10:38

Linux中央處理器進程

2019-12-16 11:00:04

LinuxCPU進程

2010-07-19 16:21:58

Telnet端口號

2024-06-12 11:26:04

2022-04-05 14:13:56

Linux端口系統(tǒng)

2011-01-26 13:26:32

Linux進程

2010-04-12 17:58:38

Oracle更改

2023-03-05 16:40:07

linux進程內(nèi)存

2010-09-08 14:22:19

sql server端口配置

2017-05-26 12:59:48

Linux端口

2010-06-18 08:55:46

SQL Server數(shù)

2016-11-24 09:22:20

Linux時區(qū)NTP服務器

2022-02-16 10:59:54

Spring端口郵件

2018-01-29 11:10:47

LinuxUnix網(wǎng)絡取證工具

2020-11-10 07:13:44

端口號進程
點贊
收藏

51CTO技術棧公眾號