


View commands include: 1. ss command, which can check the occupancy of process ports, the syntax is "ss -tnlp | grep "port number""; 2. netstat command, which can display related information such as ports and processes. Syntax "netstat -tnlp | grep "port number""; 3. lsof command, you can check the port occupancy, the syntax "lsof -i tcp:port number"; 4. fuser command, you can check the port occupancy, the syntax "fuser - v port number/tcp".
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
Port is the identifier of the logical connection between specific processes on the Linux system, including physical ports and software ports. Since the Linux operating system is a software, this article only discusses software ports. Software ports are always associated with a host's IP address and associated communications protocol, so ports are often used to differentiate applications. Most services that involve networking must open a socket to listen for incoming network requests, and each service uses a separate socket.
The socket is used in combination with the IP address, software port and protocol. The port number is applicable to both the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP) protocols. Both TCP and UDP can be used. Use port numbers between 0 and 65535 for communication.
The following are the port allocation categories:
0 – 1023: Common ports and system ports
1024 – 49151: Software Registered port
49152 – 65535: Dynamic port or private port
You can use the following six methods to view the port information of the process
#ss: Can be used to dump socket statistics.
netstat: Can display a list of open sockets.
lsof: Can list open files.
fuser: You can list the process IDs of processes that have files open.
nmap: is a network detection tool and port scanner.
systemctl: is the control manager and service manager of the systemd system.
Below we will find out the port number used by the sshd daemon.
Method 1: Use the ss command
ss is generally used to dump socket statistics. It can output information similar to the output of netstat, but it can display more TCP information and status information than other tools.
It can also display all types of socket statistics, including PACKET, TCP, UDP, DCCP, RAW, Unix domains, etc.
# ss -tnlp | grep ssh
#You can also use the port number to check.
# ss -tnlp | grep ":22"
Method 2: Use the netstat command
netstat can display network connections, routing tables, interface statistics, masquerading connections, and Multicast members.
By default, netstat lists open sockets. If no address family is specified, active sockets for all configured address families are displayed. But netstat is outdated, and ss is generally used instead.
# netstat -tnlp | grep ssh
#You can also use the port number to check.
# netstat -tnlp | grep ":22"
Method 3: Use the lsof command
lsof can list open files and list files opened by processes on the system information about the file.
# lsof -i -P | grep ssh
#You can also use the port number to check.
# 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)
Method 4: Use the fuser command
The fuser tool will display the process ID of the process that has the file open on the local system in the standard output.
# fuser -v 22/tcp
Method 5: Use the nmap command
nmap ("Network Mapper") is a software for network detection and security Open source tools for auditing. It was originally designed for fast scanning of large networks, but it also performs well for scanning of individual hosts.
nmap uses raw IP packets to determine the hosts available on the network, their services (including application names and versions), the operating system the host is running (including information such as operating system version), the Type of packet filter or firewall, and much other information.
# nmap -sV -p 22 localhost
Method 6: Use systemctl command
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
以上输出的内容显示了最近一次启动 sshd 服务时 ssh 服务的监听端口。但它不会将最新日志更新到输出中。
# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2018-09-06 07:40:59 IST; 2 weeks 3 days ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 1208 (sshd) CGroup: /system.slice/sshd.service ├─ 1208 /usr/sbin/sshd -D ├─23951 sshd: [accepted] └─23952 sshd: [net] Sep 23 12:50:36 vps.2daygeek.com sshd[23909]: Invalid user pi from 95.210.113.142 port 51666 Sep 23 12:50:36 vps.2daygeek.com sshd[23909]: input_userauth_request: invalid user pi [preauth] Sep 23 12:50:37 vps.2daygeek.com sshd[23911]: pam_unix(sshd:auth): check pass; user unknown 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 Sep 23 12:50:37 vps.2daygeek.com sshd[23909]: pam_unix(sshd:auth): check pass; user unknown 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 Sep 23 12:50:39 vps.2daygeek.com sshd[23911]: Failed password for invalid user pi from 95.210.113.142 port 51670 ssh2 Sep 23 12:50:39 vps.2daygeek.com sshd[23909]: Failed password for invalid user pi from 95.210.113.142 port 51666 ssh2 Sep 23 12:50:40 vps.2daygeek.com sshd[23911]: Connection closed by 95.210.113.142 port 51670 [preauth] Sep 23 12:50:40 vps.2daygeek.com sshd[23909]: Connection closed by 95.210.113.142 port 51666 [preauth]
大部分情况下,以上的输出不会显示进程的实际端口号。这时更建议使用以下这个 journalctl 命令检查日志文件中的详细信息。
# journalctl | grep -i "openssh\|sshd"
相关推荐:《Linux视频教程》
The above is the detailed content of What is the command to view the process port in Linux?. For more information, please follow other related articles on the PHP Chinese website!

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

手机远程linux工具有:1、JuiceSSH,是一款功能强大的安卓SSH客户端应用,可直接对linux服务进行管理;2、Termius,可以利用手机来连接Linux服务器;3、Termux,一个强大的远程终端工具;4、向日葵远程控制等等。

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft