15、Linux:IP及端口检查命令 您所在的位置:网站首页 linux怎么看端口占用情况查询 15、Linux:IP及端口检查命令

15、Linux:IP及端口检查命令

2024-05-25 21:00| 来源: 网络整理| 查看: 265

本机IP:10.129.49.102,连接测试主机:10.129.49.104

一、查询IP 1、查询当前服务器的 IP 信息 ifconfig ---或使用: ip addr

请注意,ifconfig 命令用于查询时,主要查询 IP 地址,子网掩码和MAC地址等三类信息。当然,ifconfig 命令还可用于临时修改 IP 信息,只不过服务器一旦重启,配置则会失效。

临时配置 IP 地址,格式如下:

ifconfig 网卡名称 IP地址

临时配置IP地址,广播地址,子网掩码等信息,格式如下:

ifconfig 网卡名称 192.168.xx.xx broadcast 192.168.1.255 netmask 255.255.255.0 2、查询出口 IP 地址 curl cip.cc

出口 IP 也称外网 IP(公网 IP),它是路由器连接其他网络端口的 IP,如果自己在内网要发送信息到外部并被识别,需要配置公网 IP 地址。

二、查看端口号 1、lsof 命令

lsof(list open files)是一个列出当前系统打开文件的工具,该命令可进行系统监控、网络信息查看等,但需要 root 用户权限求执行。

用于端口号情况查看的常用命令,有:

# 【按端口查】查询指定端口号情况 lsof -i :端口号 # 【按用户查】查询该用户下的端口情况 lsof -a -u 用户名 -i # 【按协议类型查】查询IPv4协议有关的端口情况 lsof -i 4 # 【按tcp查】查看所有tcp端口情况 lsof -i tcp # 【按tcp查】查看tcp端口9001情况 lsof -i tcp:9001 # 【按udp查】查看所有udp端口情况 lsof -i udp # 【按udp查】查看udp端口121情况 lsof -i udp:121 2、netstat 命令

netstat 命令用于显示网络状态,Linux 服务器默认都装有这个命令。用于端口号情况查看的常用命令,有:

# 查看所有端口情况 netstat -tunplp # 查看22端口情况 netstat -anp | grep ":22" netstat -tunplp | grep 22 # 或 netstat -tunplp | grep ":22" netstat -tunplp | grep 22 三、连通性检测命令 1、ping 命令 ping [选项] IP地址或主机名

检测主机 IP 连通性,示例如下:

[wxx@localhost ~]$ ping 10.129.49.102 PING 10.129.49.102 (10.129.49.102) 56(84) bytes of data. 64 bytes from 10.129.49.102: icmp_seq=1 ttl=64 time=0.042 ms 64 bytes from 10.129.49.102: icmp_seq=2 ttl=64 time=0.053 ms 64 bytes from 10.129.49.102: icmp_seq=3 ttl=64 time=0.063 ms

请注意,ping 命令只能检测主机 IP 的连通性,不适用检测端口。

ping 命令可以加些选项参数,以满足不同的检测需求,比如 [-c 数据包接收次数] ;[-s 设置发送包大小];[-i 设置发送周期] 等。

2、telnet 命令

telnet 协议是 TCP/IP 协议家族中的一员,是互联网远程登陆服务的主要方式和标准协议。它为用户提供了在本地计算机上完成远程主机工作的能力,在终端使用者的电脑上使用 telnet 程序,用它连接到服务器。

通过 telnet 命令,可测试IP及端口的连通性,常用到的命令格式,如下:

telnet 域名 port telnet ip port

如果刚开始使用 telnet 命令,会出现 "-bash: telnet: command not found" 错误提示,原因是没有安装 telnet 软件包。

接着,就去安装它。需要注意的是 telnet 是挂在xinetd下的,需要确认是否安装了xinetd服务,检查和安装步骤如下:

# 检查 telnet 和 xinetd rpm -qa | grep telnet rpm -qa | grep xinetd # 二者都没安装的话,通过yum安装 yum install xinetd telnet telnet-server -y

这里,遇到了以下错误:

Error downloading packages: 1:telnet-server-0.17-66.el7.x86_64: [Errno 256] No more mirrors to try. 1:telnet-0.17-66.el7.x86_64: [Errno 256] No more mirrors to try. 2:xinetd-2.3.15-14.el7.x86_64: [Errno 256] No more mirrors to try.

更换下镜像源即可。后续关于如何配置、激活以及避免防火墙阻止 telnet 服务的,这里不再描述。

测试示例:

telnet 10.129.49.104 9200 Trying 10.129.49.104... Connected to 10.129.49.104. Escape character is '^]'.

最后一行是提示语,使用 ctrl + ] 可进入 telnet 交互命令行。

请注意,telnet 命令用于登录和管理远程主机,适用于局域网网络设备的远程连接,但它采用的是明文传送报文,缺乏安全性。因此,实际中很多 Linux 服务器都不开放 telnet 服务,而改用更安全的 ssh 方式。

3、ssh 命令

相对于 telnet 服务,ssh 是加密连接的,更适用于服务器远程连接和管理。

用来测试 IP 及端口的连通性,命令格式如下:

ssh [选项] 端口号 用户名@ip地址 # 或者 ssh [选项] 端口号 ip地址

可选项中,常用的参数有-v,-p,其中,-v表示调试打印日志,-p表示指定端口号。

使用 ssh 测试下目标主机的连通性,如下:

ssh -v -p 9200 10.129.49.104 OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 58: Applying options for * debug1: Connecting to 10.129.49.104 [10.129.49.104] port 9200. debug1: Connection established. debug1: key_load_public: No such file or directory debug1: identity file /home/wxx/.ssh/id_rsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/wxx/.ssh/id_rsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/wxx/.ssh/id_dsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/wxx/.ssh/id_dsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/wxx/.ssh/id_ecdsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/wxx/.ssh/id_ecdsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/wxx/.ssh/id_ed25519 type -1 debug1: key_load_public: No such file or directory debug1: identity file /home/wxx/.ssh/id_ed25519-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_7.4 debug1: ssh_exchange_identification: HTTP/1.0 400 Bad Request debug1: ssh_exchange_identification: content-type: application/json; charset=UTF-8 debug1: ssh_exchange_identification: content-length: 203 debug1: ssh_exchange_identification: ssh_exchange_identification: Connection closed by remote host

可看到,目标主机没有设置和开发 ssh 服务,连接被关闭,暂不可连接。

4、curl 命令

前面查询出口 IP 使用的就是 curl 命令。它是一个利用 URL 规则在命令行下工作的文件传输工具,支持包括 HTTP、HTTPS、FTP 等众多协议,还支持 POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。

用来测试 IP 及端口的连通性,命令格式如下:

curl ip:port

如果测试的端口号不存在,显示信息如下:

curl 10.129.49.104:9002 curl: (56) Recv failure: Connection reset by peer

如果测试的 IP 和端口号都正常,显示信息如下:

curl 10.129.49.104:9200 { "name" : "node-1", "cluster_name" : "elasticsearch", "cluster_uuid" : "iHXx-eIsTTinhm5sY-S9_A", "version" : { "number" : "7.8.0", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "757314695644ea9a1dc3tecd26d1a43856725e65", "build_date" : "2020-06-14T15:35:50.234039Z", "build_snapshot" : false, "lucene_version" : "8.5.1", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" } 5、wget 命令

wget 是一个 GPL 许可证下的自由软件,支持 HTTP、FTP 等下载方式,支持代理服务器,支持断点续传等功能。wget 功能强大,非常好用,常用来下载文件,但也可以用来测试主机的连通。

测试主机连通性,命令格式如下:

wget ip:port

如果测试的IP或端口号不存在,会一直重试,显示如下:

wget 10.129.49.194:9002 --2023-02-27 16:05:38-- http://10.129.49.194:9002/ Connecting to 10.129.49.194:9002... connected. HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers. Retrying. --2023-02-27 16:05:39-- (try: 2) http://10.129.49.194:9002/ Connecting to 10.129.49.194:9002... connected. HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers. Retrying.

如果测试的 IP 和端口号都正常,显示信息如下:

wget 10.129.49.104:9200 --2023-02-27 16:09:27-- http://10.129.49.104:9200/ Connecting to 10.129.49.104:9200... connected. HTTP request sent, awaiting response... 200 OK Length: 531 [application/json] Saving to: ‘index.html’ 100%[=========================================================================>] 531 --.-K/s in 0s 2023-02-27 16:09:27 (74.7 MB/s) - ‘index.html’ saved [531/531]

以上检测IP及端口的连通性命令非常实用,特此总结和记录一下。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有