linux常用命令之网络命令

Posted 定静沉行

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux常用命令之网络命令相关的知识,希望对你有一定的参考价值。

发现网络细节的命令:netstat ifconfig ping -b

原创 2016年07月19日 09:33:44
 

(1)netstat -i:
提供网络接口的信息.
我们还可以指定-n标志以输出数值地址,而不是试图把它们反向解析成名字.
–numeric , -n
Show numerical addresses instead of trying to determine symbolic host, port or user names.
–interfaces, -i
Display a table of all network interfaces.

[email protected]-Inspiron-3437:~/unix/unpv13e/intro$ netstat -i
Kernel Interface table
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0         0      0      0 0             0      0      0      0 BMU
lo        65536 0     81477      0      0 0         81477      0      0      0 LRU
wlan0      1500 0     68375      0      0 0         76751      0      0      0 BMRU
[email protected]-Inspiron-3437:~/unix/unpv13e/intro$ netstat -ni
Kernel Interface table
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0         0      0      0 0             0      0      0      0 BMU
lo        65536 0     81479      0      0 0         81479      0      0      0 LRU
wlan0      1500 0     68382      0      0 0         76765      0      0      0 BMRU
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

(2)netstat -r:
展示路由表

xiahuixia@xiahuixia-Inspiron-3437:~/unix/unpv13e/intro$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         192.168.1.1     0.0.0.0         UG        0 0          0 wlan0
192.168.1.0     *               255.255.255.0   U         0 0          0 wlan0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

(3)netstat -npl:
监听所有进程服务和所用端口
-p, –program
Show the PID and name of the program to which each socket belongs.
-l, –listening
Show only listening sockets. (These are omitted by default.)

[email protected]3437:~/unix/unpv13e/intro$ netstat -npl
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.1.1:53            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:7070          0.0.0.0:*               LISTEN      5950/ssh        
tcp        0      0 0.0.0.0:37              0.0.0.0:*               LISTEN      -               
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      -               
tcp6       0      0 ::1:631                 :::*                    LISTEN      -               
tcp6       0      0 ::1:7070                :::*                    LISTEN      5950/ssh        
udp        0      0 0.0.0.0:631             0.0.0.0:*                           -               
udp        0      0 0.0.0.0:13036           0.0.0.0:*                           -               
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           6401/chrome     
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           -               
udp        0      0 0.0.0.0:40937           0.0.0.0:*                           -               
udp        0      0 0.0.0.0:37              0.0.0.0:*                           -               
udp        0      0 127.0.1.1:53            0.0.0.0:*                           -               
udp        0      0 0.0.0.0:68              0.0.0.0:*                           -               
udp6       0      0 :::37530                :::*                                -               
udp6       0      0 :::5353                 :::*                                -               
udp6       0      0 :::36391                :::*                                -               
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     18841    2428/unity-scope-lo @xiahuixia-com.canonical.Unity.Scope.applications.T6322455988553
unix  2      [ ACC ]     STREAM     LISTENING     46317    2394/unity-scope-ho @xiahuixia-com.canonical.Unity.Master.Scope.searchin.T27851042799850
unix  2      [ ACC ]     STREAM     LISTENING     15915    1958/gnome-session  @/tmp/.ICE-unix/1958
unix  2      [ ACC ]     STREAM     LISTENING     11216    -                   /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     15916    1958/gnome-session  /tmp/.ICE-unix/1958
unix  2      [ ACC ]     STREAM     LISTENING     15885    1991/pulseaudio     /run/user/1000/pulse/native
unix  2      [ ACC ]     STREAM     LISTENING     17243    1930/fcitx          /tmp/fcitx-socket-:0
unix  2      [ ACC ]     STREAM     LISTENING     18081    2260/sogou-qimpanel /tmp/sogou-qimpanel-cell
unix  2      [ ACC ]     STREAM     LISTENING     18703    2260/sogou-qimpanel /tmp/sogou-qimpanelxiahuixia
unix  2      [ ACC ]     STREAM     LISTENING     15851    1970/dbus-daemon    @/tmp/dbus-ubllZvxCji
unix  2      [ ACC ]     STREAM     LISTENING     11215    -                   @/tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     14631    -                   /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     16297    2250/gvfsd-trash    @/dbus-vfs-daemon/socket-MjVX0Tha
unix  2      [ ACC ]     STREAM     LISTENING     9032     -                   @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     18151    2394/unity-scope-ho @xiahuixia-com.canonical.Unity.Master.Scope.applications.T6281581711419
unix  2      [ ACC ]     STREAM     LISTENING     18842    2428/unity-scope-lo @xiahuixia-com.canonical.Unity.Scope.scopes.T6322468445678
unix  2      [ ACC ]     STREAM     LISTENING     17572    1899/dbus-daemon    @/tmp/dbus-vkou6mhKao
unix  2      [ ACC ]     STREAM     LISTENING     18195    2432/unity-files-da @xiahuixia-com.canonical.Unity.Scope.files.T6298090592527
unix  2      [ ACC ]     STREAM     LISTENING     18152    2394/unity-scope-ho @xiahuixia-com.canonical.Unity.Master.Scope.files.T6281589456934
unix  2      [ ACC ]     STREAM     LISTENING     144013   -                   /var/run/cups/cups.sock
unix  2      [ ACC ]     STREAM     LISTENING     14478    -                   /var/run/avahi-daemon/socket
unix  2      [ ACC ]     STREAM     LISTENING     15761    -                   /run/user/1000/keyring-BgLLUc/control
unix  2      [ ACC ]     STREAM     LISTENING     18895    2523/unity-music-da @xiahuixia-com.canonical.Unity.Scope.rhythmbox.T6337947908079
unix  2      [ ACC ]     STREAM     LISTENING     20743    2394/unity-scope-ho @xiahuixia-com.canonical.Unity.Master.Scope.music.T6337152322839
unix  2      [ ACC ]     SEQPACKET  LISTENING     9114     -                   /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     143333   6401/chrome         /tmp/.com.google.Chrome.uaHrVq/SingletonSocket
unix  2      [ ACC ]     STREAM     LISTENING     13991    -                   /var/run/mysqld/mysqld.sock
unix  2      [ ACC ]     STREAM     LISTENING     16782    1830/init           @/com/ubuntu/upstart-session/1000/1830
unix  2      [ ACC ]     STREAM     LISTENING     22037    2394/unity-scope-ho @xiahuixia-com.canonical.Unity.Master.Scope.reference.T7028131647813
unix  2      [ ACC ]     STREAM     LISTENING     13742    -                   /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     17591    -                   /run/user/1000/keyring-BgLLUc/pkcs11
unix  2      [ ACC ]     STREAM     LISTENING     17596    -                   /run/user/1000/keyring-BgLLUc/gpg
unix  2      [ ACC ]     STREAM     LISTENING     17598    -                   /run/user/1000/keyring-BgLLUc/ssh
unix  2      [ ACC ]     STREAM     LISTENING     17001    2026/dbus-daemon    @/tmp/dbus-z5PVVTGiBY
unix  2      [ ACC ]     STREAM     LISTENING     10966    -                   /var/run/sdp
unix  2      [ ACC ]     STREAM     LISTENING     22121    2394/unity-scope-ho @xiahuixia-com.canonical.Unity.Master.Scope.moresuggestions.T7032862236599
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

结合grep可以用来查看某个具体端口及服务情况
如下命令查看所有7070端口使用情况

xiahuixia@xiahuixia-Inspiron-3437:~/unix/unpv13e/intro$ netstat -npl|grep 7070
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 127.0.0.1:7070          0.0.0.0:*               LISTEN      5950/ssh        
tcp6       0      0 ::1:7070                :::*                    LISTEN      5950/ssh        
xiahuixia@xiahuixia-Inspiron-3437:~/unix/unpv13e/intro$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

也可以使用如下命令:
-a, –all
Show both listening and non-listening sockets. With the –interfaces option, show interfaces that are not up

-o, –timers
Include information related to networking timers.

xiahuixia@xiahuixia-Inspiron-3437:~/unix/unpv13e/intro$ netstat -anlp |grep 7070
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 127.0.0.1:7070          0.0.0.0:*               LISTEN      5950/ssh        
tcp6       0      0 ::1:7070                :::*                    LISTEN      5950/ssh        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

找到对应的进程号后,如果想解除这个端口的占用,就可以使用kill将这个进程杀死.
(4)ifconfig eth0
由(1)就可知道各个网络接口的名字,执行ifconfig 网络接口名 就可以获得每个接口的详细信息.

xiahuixia@xiahuixia-Inspiron-3437:~/unix/unpv13e/intro$ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr 9c:d2:1e:02:ed:77  
          inet addr:192.168.1.104  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::9ed2:1eff:fe02:ed77/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:68954 errors:0 dropped:0 overruns:0 frame:0
          TX packets:78019 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:58028864 (58.0 MB)  TX bytes:13984328 (13.9 MB)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

由上面这个接口信息MULTICAST 可以得知该接口所在的主机支持多播.
(5)ping -b 广播ip地址
找出本地网络中众多主机的ip地址的方法之一就是,针对从上一步找到的本地接口的广播地址执行ping命令
-b:broadcast

[email protected]3437:~/unix/unpv13e/intro$ ping -b 192.168.1.255 
WARNING: pinging broadcast address
PING 192.168.1.255 (192.168.1.255) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.66 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.77 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=2.85 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=3.30 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=5.49 ms





















以上是关于linux常用命令之网络命令的主要内容,如果未能解决你的问题,请参考以下文章

Linux之常用网络命令

Linux系列教程——Linux常用命令之网络和关机重启命令

Linux系列教程——Linux常用命令之网络和关机重启命令

linux之常用操作基本命令

linux系列之常用运维命令整理笔录

测试开发之网络篇-常用命令