Linux上的套接字方法及端口分配的方法介绍-苏州安嘉

套接字与IP地址、软件端口和协议结合使用,端口号可用于传输控制协议(TCP)和用户数据报协议(UDP)协议,TCP和UDP都可以使用0到65535进行通信.

以下是端口分配类别:

有关保留端口的更多信息linux查看端口被哪个进程占用,请参阅 Linux 上的 /etc/services 文件。

# 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]
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 (historic)
msp 18/udp # message send protocol (historic)
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
# 24 - private mail system
lmtp 24/tcp # LMTP Mail Delivery
lmtp 24/udp # LMTP Mail Delivery

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

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

方法一:使用ss命令

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

它还可以显示所有类型的套接字统计信息linux查看端口被哪个进程占用,包括 PACKET、TCP、UDP、DCCP、RAW、Unix 域等。

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

您也可以使用端口号进行检查。

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

方法二:使用netstat命令

netstat 可以显示网络连接、路由表、接口统计信息、伪装连接和多播成员资格。

默认情况下,netstat 列出打开的套接字。如果未指定地址族,则显示所有已配置地址族的活动套接字。但是netstat已经过时了,一般用ss代替。

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

您也可以使用端口号进行检查。

# netstat -tnlp | grep ":22"

图片[1]-Linux上的套接字方法及端口分配的方法介绍-苏州安嘉-唐朝资源网

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1208/sshd tcp6 0 0 :::22 :::* LISTEN 1208/sshd

方法三:使用 lsof 命令

lsof 列出打开的文件并列出系统上进程打开的文件的相关信息。

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

您也可以使用端口号进行检查。

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

方法四:使用 fuser 命令

fuser 工具将在本地系统上将文件打开到标准输出的进程的进程 ID 显示出来。

# fuser -v 22/tcp
 USER PID ACCESS COMMAND

22/tcp: root 1208 F.... sshd
 root 12388 F.... sshd
 root 49339 F.... sshd

方法五:使用nmap命令

nmap(“Network Mapper”)是一个用于网络检测和安全审计的开源工具。它最初用于快速扫描大型网络,但它也适用于单个主机的扫描。

nmap 使用原始 IP 数据包来确定网络上可用的主机、这些主机的服务(包括应用程序名称和版本)、主机运行的操作系统(包括操作系统版本等信息)、包过滤使用了服务器或防火墙的类型,以及许多其他信息。

# nmap -sV -p 22 localhost
Starting Nmap 6.40 ( http://nmap.org ) at 2018-09-23 12: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

方法六:使用systemctl命令

systemctl 是 systemd 系统的控制管理器和服务管理器。它取代了目前大多数现代 Linux 操作系统采用的旧的 SysV 初始化系统管理。

© 版权声明
THE END
喜欢就支持一下吧
点赞104赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容