对于 Linux 系统管理员来说,清楚某个服务是否正确地绑定或监听某个端口,是至关重要的。如果你需要处理端口相关的问题,这篇文章可能会对你有用。
端口是 Linux 系统上特定进程之间逻辑连接的标识,包括物理端口和软件端口。由于 Linux 操作系统是一个软件,因此本文只讨论软件端口。软件端口始终与主机的 IP 地址和相关的通信协议相关联,因此端口常用于区分应用程序。大部分涉及到网络的服务都必须打开一个套接字来监听传入的网络请求,而每个服务都使用一个独立的套接字。
# less /etc/services # /etc/services: # $Id: services,v 1.55 2013/04/14 ovasik Exp $ # # Network services, Internet style # IANA services version: last updated 2013-04-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]
tcpmux1/tcp # TCP port service multiplexer tcpmux1/udp # TCP port service multiplexer rje5/tcp # Remote Job Entry rje5/udp # Remote Job Entry echo7/tcp echo7/udp discard9/tcp sink null discard9/udp sink null systat11/tcp users systat11/udp users daytime13/tcp daytime13/udp qotd17/tcp quote qotd17/udp quote msp18/tcp # message send protocol (historic) msp18/udp # message send protocol (historic) chargen19/tcp ttytst source chargen19/udp ttytst source ftp-data 20/tcp ftp-data 20/udp # 21 is registered to ftp, but also used by fsp ftp21/tcp ftp21/udp fsp fspd ssh22/tcp # The Secure Shell (SSH) Protocol ssh22/udp # The Secure Shell (SSH) Protocol telnet23/tcp telnet23/udp # 24 - private mail system lmtp24/tcp # LMTP Mail Delivery lmtp24/udp # LMTP Mail Delivery
可以使用以下六种方法查看端口信息。
ss:可以用于转储套接字统计信息。
netstat:可以显示打开的套接字列表。
lsof:可以列出打开的文件。
fuser:可以列出那些打开了文件的进程的进程 ID。
nmap:是网络检测工具和端口扫描程序。
systemctl:是 systemd 系统的控制管理器和服务管理器。
以下我们将找出 sshd 守护进程所使用的端口号。
方法 1:使用 ss 命令
ss 一般用于转储套接字统计信息。它能够输出类似于 netstat 输出的信息,但它可以比其它工具显示更多的 TCP 信息和状态信息。
nmap 使用原始 IP 数据包来确定网络上可用的主机,这些主机的服务(包括应用程序名称和版本)、主机运行的操作系统(包括操作系统版本等信息)、正在使用的数据包过滤器或防火墙的类型,以及很多其它信息。
1 2 3 4 5 6 7 8 9 10 11
# nmap -sV -p 22 localhost
Starting Nmap 6.40 ( http://nmap.org ) at 2018-09-2312:36 IST Nmap scan report for localhost (127.0.0.1) Host is up (0.000089s latency). Other addresses for localhost (not scanned): 127.0.0.1 PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 7.4 (protocol 2.0)
Service detection performed. Please report any incorrect results at http://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 0.44 seconds
方法 6:使用 systemctl 命令
systemctl 是 systemd 系统的控制管理器和服务管理器。它取代了旧的 SysV 初始化系统管理,目前大多数现代 Linux 操作系统都采用了 systemd。
# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2018-09-23 02:08:56 EDT; 6h 11min ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 11584 (sshd) CGroup: /system.slice/sshd.service └─11584 /usr/sbin/sshd -D
Sep 23 02:08:56 vps.2daygeek.com systemd[1]: Starting OpenSSH server daemon... Sep 23 02:08:56 vps.2daygeek.com sshd[11584]: Server listening on 0.0.0.0 port 22. Sep 23 02:08:56 vps.2daygeek.com sshd[11584]: Server listening on :: port 22. Sep 23 02:08:56 vps.2daygeek.com systemd[1]: Started OpenSSH server daemon. Sep 23 02:09:15 vps.2daygeek.com sshd[11589]: Connection closed by 103.5.134.167 port 49899 [preauth] Sep 23 02:09:41 vps.2daygeek.com sshd[11592]: Accepted password for root from 103.5.134.167 port 49902 ssh2
# journalctl | grep -i "openssh\|sshd" Sep2302:08:56 vps138235.vps.ovh.ca sshd[997]: Received signal 15; terminating. Sep2302:08:56 vps138235.vps.ovh.ca systemd[1]: Stopping OpenSSH server daemon... Sep2302:08:56 vps138235.vps.ovh.ca systemd[1]: Starting OpenSSH server daemon... Sep2302:08:56 vps138235.vps.ovh.ca sshd[11584]: Server listening on0.0.0.0 port 22. Sep2302:08:56 vps138235.vps.ovh.ca sshd[11584]: Server listening on :: port 22. Sep2302:08:56 vps138235.vps.ovh.ca systemd[1]: Started OpenSSH server daemon.