百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 优雅编程 > 正文

15、linux命令-netstat(linux命令lt)

sinye56 2024-11-13 12:33 10 浏览 0 评论

15、linux命令-netstat

主要命令

netstat -a 显示所有网络连接

netstat -a | grep baidu.com

netstat -a | grep ESTABLISHED

netstat -a | grep tcp | grep LISTEN

netstat -at # 仅显示tcp

netstat -au # 仅显示udp

netstat -l # 要显示所有监听套接字

netstat -p # 获取程序及其进程ID

netstat -n # 以数字方式获取与IP地址和端口有关的所有数据

netstat -lnpt

netstat -lnpu

netstat -rn # 查看路由

netstat -i # 列出网口信息

The term “netstat” stands for Network Statistics. In layman’s terms, netstat command displays the current network connections, networking protocol statistics, and a variety of other interfaces.

术语“ netstat”代表网络统计信息。 用外行术语来说,netstat命令显示当前的网络连接,网络协议统计信息以及各种其他接口。

If we enter netstat in the terminal, without any background knowledge of computer networking, the system throws a wide range of networking jargon at us. It is the responsibility of the programmer to extract important information while flushing out the rest.

如果我们在终端中输入netstat ,而没有任何计算机联网的背景知识,则系统会向我们抛出各种各样的网络术语。 程序员有责任在清除其余信息的同时提取重要信息。

In this article, we will answer some of the queries related to computer networking using the netstat command.

在本文中,我们将使用netstat命令回答一些与计算机网络相关的查询。

使用Netstat命令识别活动的网络连接 (Identify Active Network Connections Using the Netstat Command)

To display all the active network connections in Linux, we use

要显示Linux中所有活动的网络连接,我们使用

netstat -a

Output:

输出:

Netstat All 1

Netstat全部1

The standard output contains six columns:

标准输出包含六列:

  • Proto (Protocol) 协议(Protocol)
  • Recv-Q (Receiving Queue) Recv-Q(接收队列)
  • Send-Q (Sending Queue) Send-Q(发送队列)
  • Addresses
    • Local Address

netstat

    • Foreign Address 地址
    • 本地地址

netstat

    • 外部地址
  • State 状态

To understand this better, suppose we open a website www.lookip.net. On running the command:

为了更好地理解这一点,假设我们打开一个网站www.lookip.net 。 运行命令时:

netstat -a | grep lookip.net

We will get the following output:

我们将得到以下输出:



Search lookip.net using the netstat command

使用netstat命令搜索lookip.net

As it quite clear that, we extracted all the network connections in progress with a particular foreign address. In the command, ‘|‘ is used to pass the output of one sub-command to another, whereas grep is a searching tool in Linux.

显然,我们使用特定的外部地址提取了所有正在进行的网络连接。 在命令中,' | '用于将一个子命令的输出传递给另一个子命令,而grep是Linux中的搜索工具。

Note: This technique cannot be applied for all kinds of websites since not every website has a foreign address matching the URL.

注意:由于并非每个网站都有与URL匹配的外部地址,因此该技术无法应用于所有类型的网站。

To further experiment with the data provided by the netstat command, we can write commands focusing on protocols, addresses, or states:

为了进一步试验netstat命令提供的数据,我们可以编写针对协议,地址或状态的命令:

Display all established connections

显示所有已建立的连接

netstat -a | grep ESTABLISHED

Display all TCP connections in listening state

显示所有处于侦听状态的TCP连接

netstat -a | grep tcp | grep LISTEN

Instead of creating custom commands, Linux provides some in-built options for fetching specific information.

Linux提供了一些内置选项来获取特定信息,而不是创建自定义命令。

基于协议过滤 (Filtering based on Protocols)

For TCP specific queries, -t option is used. To display only the TCP connections:

对于TCP特定查询,使用-t选项。 要仅显示TCP连接:

netstat -at

Note: To apply multiple filters in a single netstat command, the options are appended.注意:要在单个netstat命令中应用多个过滤器,将附加这些选项。

For UDP specific queries, -u option is used. To display all the sockets following UDP :

对于UDP特定查询,使用 -u 选项。 要显示所有遵循UDP的套接字:

netstat -au

基于状态的选项: (State-based option:)

To display all listening sockets:

要显示所有监听套接字:

netstat -l



使用Netstat使用网络连接识别程序 (Identify the programs using network connections using Netstat)

To fetch the programs and their process IDs, we use:

要获取程序及其进程ID,我们使用:

netstat -p

For TCP specific programs:

对于TCP特定程序:

netstat -pt

Output :

输出:



Programs following TCP

TCP之后的程序

As we can notice, Chrome is accessing the internet with the process id as 16648. This information can be used to kill or stop any program accessing some network without the knowledge of the user.

我们可以注意到,Chrome正在使用进程ID为16648的Internet进行访问。这些信息可用于杀死或阻止任何程序在不知情的情况下访问某个网络。

Note: It may happen that some program information might be hidden if the current user is not the root user. To become a root user in Linux, the command sudo su and entering the password can help. For further information, refer to this.

注意:如果当前用户不是root用户,则可能会隐藏某些程序信息。 要成为Linux的root用户,可以使用sudo su命令并输入密码。 欲了解更多信息,请参阅



使用Netstat命令列出每个网络连接的IP地址 (Using the Netstat Command to List IP Addresses of Each Network Connection)

For fetching all the data related to IP addresses and ports numerically, we use:

为了以数字方式获取与IP地址和端口有关的所有数据,我们使用:

netstat -n

We can display addresses numerically for programs following TCP by:

我们可以通过以下方式以数字方式显示遵循TCP的程序的地址:

netstat -ptn

Output:

输出:


Programs following TCP (numeric)

TCP之后的程序(数字)

The difference is very vivid as we can see the IP addresses as well as port numbers for each connection.

区别非常明显,因为我们可以看到每个连接的IP地址和端口号。



每个协议的统计数据是什么? (What are the statistics for each protocol?)

To access the summary statistics for each type of protocol using the netstat command, we run:

要使用netstat命令访问每种协议的摘要统计信息,我们运行:

netstat -s

Output:

输出:


Statistics for each protocol

每种协议的统计信息



使用Netstat命令显示路由表 (Using the Netstat Command to Display the Routing Table)

Any device on a network needs to decide where to route the data packets. The routing table contains information to make these decisions. To acquire the contents of the routing table in numerics, we use the following command option:

网络上的任何设备都需要决定将数据包路由到何处。 路由表包含做出这些决定的信息。 要获取数字形式的路由表的内容,我们使用以下命令选项:

netstat -rn

Output:

输出:


Contents of routing table

路由表内容

The kernel routing table consists of the following columns:

内核路由表由以下几列组成:

  • Destination 目标
  • Gateway 网关
  • Genmask Genmask
  • Flags 标志
  • MSS MSS
  • Window 窗口
  • irtt (Initial Round Trip Time) irtt (初始往返时间)
  • Iface (Interface) Iface (接口)

Note: The columns having zero value means that the default size is being used.注意:具有零值的列表示正在使用默认大小。



列出活动的网络接口 (List out the active network interfaces)

To access any information from the internet, there has to be some link between the system and the network. That point of interconnection is provided by a network interface. We run the command:

要从Internet访问任何信息,系统与网络之间必须存在某些链接。 互连点由网络接口?提供。 我们运行命令:

netstat -i

Output:

输出:

Network interfaces

网络接口

The kernel interface table comprises of:

内核接口表包括:

  • Iface (Interface) Iface(接口)
  • MTU MTU
  • RX RX
  • TX TX
  • OK OK
  • ERR ERR
  • DRP DRP
  • OVR OVR
  • Flg Flg


The command netstat features a wide range of knowledge which makes it impossible, to sum up in just one article. We can always refer man pages in Linux by:

netstat具有广泛的知识,仅凭一篇文章就无法总结。 我们总是可以通过以下方式引用Linux中的手册页:

man netstat

and to learn more about netstat options we can ask help in terminal by:

要了解有关netstat选项的更多信息,我们可以通过以下方式在终端中寻求帮助:

netstat -h



参考文献: (References:)

  • Linux – netstat man pageLinux – netstat手册页

翻译自: https://www.journaldev.com/41196/netstat-command-in-linux

资料来源如何在Linux中使用netstat命令

提交参考资料,PK现有内容

博客作者cunchi4221

拓展阅读

linux环境下使用netstat命令查看网络信息Linux网络相关命令:netstat,ssLinux网络监控命令——netstatLinux中netstat与ss命令之间的区别linux 端口监听 Netstat 常用命令Linux中的netstat命令详解

相关推荐

程序员:JDK的安装与配置(完整版)_jdk的安装方法

对于Java程序员来说,jdk是必不陌生的一个词。但怎么安装配置jdk,对新手来说确实头疼的一件事情。我这里以jdk10为例,详细的说明讲解了jdk的安装和配置,如果有不明白的小伙伴可以评论区留言哦下...

Linux中安装jdk并配置环境变量_linux jdk安装教程及环境变量配置

一、通过连接工具登录到Linux(我这里使用的Centos7.6版本)服务器连接工具有很多我就不一一介绍了今天使用比较常用的XShell工具登录成功如下:二、上传jdk安装包到Linux服务器jdk...

麒麟系统安装JAVA JDK教程_麒麟系统配置jdk

检查检查系统是否自带java在麒麟系统桌面空白处,右键“在终端打开”,打开shell对话框输入:java–version查看是否自带java及版本如图所示,系统自带OpenJDK,要先卸载自带JDK...

学习笔记-Linux JDK - 安装&配置

前提条件#检查是否存在JDKrpm-qa|grepjava#删除现存JDKyum-yremovejava*安装OracleJDK不分系统#进入安装文件目...

Linux新手入门系列:Linux下jdk安装配置

本系列文章是把作者刚接触和学习Linux时候的实操记录分享出来,内容主要包括Linux入门的一些理论概念知识、Web程序、mysql数据库的简单安装部署,希望能够帮到一些初学者,少走一些弯路。注意:L...

测试员必备:Linux下安装JDK 1.8你必须知道的那些事

1.简介在Oracle收购Sun后,Java的一系列产品就被整合到Oracle官网中,打开官网乍眼一看也不知道去哪里下载,还得一个一个的摸索尝试,而且网上大多数都是一些Oracle收购Sun前,或者就...

Linux 下安装JDK17_linux 安装jdk1.8 yum

一、安装环境操作系统:JDK版本:17二、安装步骤第一步:下载安装包下载Linux环境下的jdk1.8,请去官网(https://www.oracle.com/java/technologies/do...

在Ubuntu系统中安装JDK 17并配置环境变量教程

在Ubuntu系统上安装JDK17并配置环境变量是Java开发环境搭建的重要步骤。JDK17是Oracle提供的长期支持版本,广泛用于开发Java应用程序。以下是详细的步骤,帮助你在Ubuntu系...

如何在 Linux 上安装 Java_linux安装java的步骤

在桌面上拥抱Java应用程序,然后在所有桌面上运行它们。--SethKenlon(作者)无论你运行的是哪种操作系统,通常都有几种安装应用程序的方法。有时你可能会在应用程序商店中找到一个应用程序...

Windows和Linux环境下的JDK安装教程

JavaDevelopmentKit(简称JDK),是Java开发的核心工具包,提供了Java应用程序的编译、运行和开发所需的各类工具和类库。它包括了JRE(JavaRuntimeEnviro...

linux安装jdk_linux安装jdk软连接

JDK是啥就不用多介绍了哈,外行的人也不会进来看我的博文。依然记得读大学那会,第一次实验课就是在机房安装jdk,编写HelloWorld程序。时光飞逝啊,一下过了十多年了,挣了不少钱,买了跑车,娶了富...

linux安装jdk,全局配置,不同用户不同jdk

jdk1.8安装包链接:https://pan.baidu.com/s/14qBrh6ZpLK04QS8ogCepwg提取码:09zs上传文件解压tar-zxvfjdk-8u152-linux-...

运维大神教你在linux下安装jdk8_linux安装jdk1.7

1.到官网下载适合自己机器的版本。楼主下载的是jdk-8u66-linux-i586.tar.gzhttp://www.oracle.com/technetwork/java/javase/downl...

window和linux安装JDK1.8_linux 安装jdk1.8.tar

Windows安装JDK1.8的步骤:步骤1:下载JDK打开浏览器,找到JDK下载页面https://d.injdk.cn/download/oraclejdk/8在页面中找到并点击“下载...

最全的linux下安装JavaJDK的教程(图文详解)不会安装你来打我?

默认已经有了linux服务器,且有root账号首先检查一下是否已经安装过java的jdk任意位置输入命令:whichjava像我这个已经安装过了,就会提示在哪个位置,你的肯定是找不到。一般我们在...

取消回复欢迎 发表评论: