netstat

netstat命令

netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等

1 常用选项:

-a :(all)显示所有选项,默认不显示LISTEN相关

-t :(tcp)仅显示tcp相关选项

-u :(udp)仅显示udp相关选项

-n :以数字显示IP和端口,能显示数字的全部转化成数字。

-l :仅列出有在 Listen (监听) 的服務状态

-p: 显示建立相关链接的程序名

-r :显示路由信息,路由表

-e :显示扩展信息,例如uid等

-s :按各个协议进行统计

-c :每隔一个固定时间,执行该netstat命令。

-w:raw socket相关

2 -tan 显示所有tcp端口

[root@centos7 ~]# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 52 192.168.29.3:22 192.168.29.1:51029 ESTABLISHED
tcp 0 0 192.168.29.3:22 192.168.29.1:51180 ESTABLISHED
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::*

3 显示所有udo端口 -uan

[root@centos7 ~]# netstat -anu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp 0 0 0.0.0.0:47414 0.0.0.0:*
udp 0 0 192.168.122.1:53 0.0.0.0:*
udp 0 0 0.0.0.0:67 0.0.0.0:*

4 显示所有处于监听的tcp端口    -tnl

[root@centos7 ~]# netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN

5 显示所有处于监听状态的udp端口 -unl

[root@centos7 ~]# netstat -unl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp 0 0 0.0.0.0:47414 0.0.0.0:*
udp 0 0 0.0.0.0:20815 0.0.0.0:*
udp 0 0 192.168.122.1:53 0.0.0.0:*
udp 0 0 0.0.0.0:67 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:9804 0.0.0.0:*
udp6 0 0 :::12799 :::*
udp6 0 0 :::37953 :::*

6 显示路由表 –rn

[root@centos7 ~]# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.29.1 0.0.0.0 UG 0 0 0 ens33
192.168.29.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@centos7 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.29.1 0.0.0.0 UG 100 0 0 ens33
192.168.29.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

7 显示每个协议的统计信息

显示所有端口的统计信息 netstat -s

[root@centos7 ~]# netstat -s
Ip:
3808 total packets received
0 forwarded
0 incoming packets discarded
2444 incoming packets delivered
2254 requests sent out
32 outgoing packets dropped
Icmp:
676 ICMP messages received
0 input ICMP message failed.
ICMP input histogram:
destination unreachable: 148
echo replies: 528
717 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 148
echo request: 569
[……]

显示 TCP 或 UDP 端口的统计信息 netstat -st 或 -su

[root@centos7 ~]# netstat -st
IcmpMsg:
InType0: 528
InType3: 148
OutType3: 148
OutType8: 569
Tcp:
0 active connections openings
2 passive connection openings
0 failed connection attempts
0 connection resets received
2 connections established
1637 segments received
1418 segments send out
0 segments retransmited
0 bad segments received.
0 resets sent

[root@centos7 ~]# netstat -su
IcmpMsg:
InType0: 528
InType3: 148
OutType3: 148
OutType8: 569
Udp:
0 packets received
140 packets to unknown port received.
0 packet receive errors
169 packets sent
0 receive buffer errors
0 send buffer errors
UdpLite:
IpExt:
InMcastPkts: 27
OutMcastPkts: 33
InBcastPkts: 766
InOctets: 410294
OutOctets: 432666
InMcastOctets: 4963
OutMcastOctets: 5444
InBcastOctets: 179503
InNoECTPkts: 3848

8 显示 PID 和进程名称 netstat -p

[root@centos7 ~]# netstat -p
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 52 centos7.3.local:ssh gateway:51029 ESTABLISHED 1552/sshd: root@pts
tcp 0 0 centos7.3.local:ssh gateway:51180 ESTABLISHED 1854/sshd: root@pts
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ] DGRAM 8520 1/systemd /run/systemd/notify
unix 2 [ ] DGRAM 8522 1/systemd /run/systemd/cgroups-agent
unix 5 [ ] DGRAM 8542 1/systemd /run/systemd/journal/socket
unix 28 [ ] DGRAM 8544 1/systemd /dev/log
unix 2 [ ] DGRAM 11419 1/systemd /run/systemd/shutdownd
unix 3 [ ] STREAM CONNECTED 23993 1276/ibus-x11

原创文章,作者:linux is not unix,如若转载,请注明出处:http://www.178linux.com/74901

(0)
上一篇 2017-05-07 14:24
下一篇 2017-05-07 15:41

相关推荐

  • 系统自动化安装和SELinux

    一、知识整理 1、anaconda系统安装程序:默认图形启动; 使用光盘启动,在选择模式界面tab键在后面增加text或按下ESC键,输入lnux text进入字符界面安装。 2、创建kickstart文件: 直接手动编辑:依据模板修改,/root目录下的anaconda.cfg 使用创建工具创建:system-config-kickstart,图形化工具:…

    Linux干货 2016-09-26
  • 文本处理工具之sed

    文本处理工具之sed 1、sed是一种行编辑器,对文本逐行处理。处理时,它会将文本行载入”模式空间”(临时缓冲区),接着用sed命令对模式空间的内容进行处理,处理完成后,将模式空间内容输出到显示屏 与之类似的处理工具还有grep和awk,相比grep,sed不仅对文本有过滤功能,还可以对文本进行其他增删改的操作。而相比awk,sed显得更加简单高效。下面我们…

    2017-03-18
  • 文件的查找

    文件的查找 使用locate命令 默认从根开始搜索 非实时查找(数据库查找) locate 文件名 查询系统上预建的的文件索引数据库    /var/lib/mlocate/mlocate.db 依赖于事先构建的索引 索引的构建是在系统较为空闲时自动进行(周期性任务),管理员手动更新数据库(updatedb)。注意工作中不能轻易…

    Linux干货 2016-08-18
  • 20160808作业

    1、删除/etc/grub2.conf文件中所有以空白开头的行行首的空白字符      2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符      3、在/root/install.log每一行行首增加#号  &nbs…

    Linux干货 2016-08-11
  • HA Cluster-主备模型(项目实战)

    主机环境:主机A和主机B使用的是CentOS7.2的系统:    主机A IP地址为:192.168.1.103     主机B IP地址为:192.168.1.106     向外界提供的服务的地址为:192.168.1.100配置过程:…

    Linux干货 2016-11-01
  • Linux Cluster之Keepalived + Nginx的实现

      一、实验环境 http server1与http server2组成web集群,由nginx服务器实现负载均衡代理,使用keepalived保证nginx服务的高可用。通过虚拟IP192.168.154.177对外提供web服务。 实验目的: 1)当Nginx server1上的nginx服务异常时,由nginx server2提供反带服务。 …

    2016-11-02