linux常用基本命令

  wc命令

  统计指定的文件中的字节数、字数、行数,并将统计结果显示输出。如果没有给出文件名,则从标准输入读取。wc同时也给出所指文件的总统计数

  语法:wc [选项] [文件名]

  -c:统计字节数;

  -l:统计行数;

  -m:统计字符数。这个标志不能与-c一起使用;

  -w:统计字数。一个字被定义为空白、跳格或者换行字符分割的字符串;

  -L:打印最长行的长度

[root@localhost wc]# cat test.txt 
hnlinux 
peida.cnblogs.com 
ubuntu 
ubuntu linux 
redhat 
Redhat 
linuxmint
[root@localhost wc]# wc test.txt 
 7  8 76 test.txt
[root@localhost wc]# wc -l test.txt 
7 test.txt
[root@localhost wc]# wc -c test.txt 
76 test.txt
[root@localhost wc]# wc -w test.txt 
8 test.txt
[root@localhost wc]# wc -m test.txt 
76 test.txt
[root@localhost wc]# wc -L test.txt 
18 test.txt
[root@localhost wc]#

  uptime命令

  uptime命令能够打印系统运行了多长时间和系统平均负载。uptime命令可以显示的信息依次是:现在时间、系统已经运行了多长时间、目前有多少登录用户、系统过去的1分钟、5分钟和15分钟内的平均负载。

  语法:uptime [选项]

  -V:显示指令的版本信息

[root@localhost wc]# uptime -V
uptime from procps-ng 3.3.10
[root@localhost wc]# 

  使用uptime命令查看系统负载:

[root@localhost wc]# uptime
 22:16:12 up  4:26,  1 user,  load average: 0.08, 0.04, 0.05
[root@localhost wc]#

  显示内容说明:

  22:16:12//系统当前时间

  up 4:26//主机已经运行时间,时间越大,说明你的机器越稳定

  1 user//用户连接数,是总连接数而不是用户数

  load average: 0.08, 0.04, 0.05//系统平均负载,统计最近1,5,15分钟的系统平均负载

  cut命令

  cut命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写入标准输出。

  语法:cut [-bn] [file] 或 cut [-c] [file] 或 cut [-df] [file]

  如果不指定file参数,cut命令将读取标准输入。必须制定-b、-c或-f标志之一。

  -b:已字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也制定了-n标志。

  -c:已字符为单位进行分割。

  -d:自定义分隔符,默认为制表符。

  -f:与-d一起使用,制定显示哪个区域。

  -n:取消分割多字节字符。仅和-b标志一起使用。如果字符的最后一个字节落在由-b标志的List参数指定的<br/>范围之内,该字符将被写出;否则,该字符被排除。

  cut命令可将一串字符作为列来显示,字符字段的记法:

    N-:从第N个字节、字符、字段到结尾。

    N-M:从第N个字节、字符、字段到第M个(包括M在内)字节、字符、字段。

    -M:从第1个字节、字符、字段到第M个(包括M在内)字节、字符、字段。

  以/etc/passwd的前5行内容为例:

[root@localhost wc]# head -n 5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost wc]#

  截取用户名:-d来设置分隔符为“:”,然后用-f来设置截取第一个域:

[root@localhost wc]# head -n 5 /etc/passwd | cut -d ':' -f 1
root
bin
daemon
adm
lp
[root@localhost wc]# 

  截取第1,3到5字段:

[root@localhost wc]# head -n 5 /etc/passwd | cut -d ':' -f 1,3-5
root:0:0:root
bin:1:1:bin
daemon:2:2:daemon
adm:3:4:adm
lp:4:7:lp
[root@localhost wc]# 

  截取1到3个字符:

[root@localhost wc]# head -n 5 /etc/passwd | cut -c 1-3
roo
bin
dae
adm
lp:
[root@localhost wc]#

  截取前2个字符:

[root@localhost wc]# head -n 5 /etc/passwd | cut -c -2
ro
bi
da
ad
lp
[root@localhost wc]# 

  截取第5个字符开始到结尾:

[root@localhost wc]# head -n 5 /etc/passwd | cut -c 5-
:x:0:0:root:/root:/bin/bash
x:1:1:bin:/bin:/sbin/nologin
on:x:2:2:daemon:/sbin:/sbin/nologin
x:3:4:adm:/var/adm:/sbin/nologin
:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost wc]# 

  netstat命令

  netstat是一个告诉我们系统中所有tcp/udp/unix socket连接状态的命令行工具。它会列出所有已连接或等待连接状态的连接。该工具在识别某个应用监听哪个端口时特别有用,我们也能用它来判断某个应用是否正常的在监听某个端口。

  netstat命令还能显示其他各种各样的网络相关信息,例如路由表,网卡统计信息,虚假连接以及多播成员等。

  1)、检查所有的连接

  使用a选项可以列出系统中的所有连接。

[root@localhost wc]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN     
tcp        0     52 localhost.localdoma:ssh 10.0.30.1:lnvmaps       ESTABLISHED
tcp6       0      0 [::]:mysql              [::]:*                  LISTEN     
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN     
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7          
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     13568    /run/lvm/lvmetad.socket
unix  2      [ ]         DGRAM                    13571    /run/systemd/shutdownd
unix  3      [ ]         DGRAM                    8710     /run/systemd/notify
unix  2      [ ]         DGRAM                    8712     /run/systemd/cgroups-agent
unix  2      [ ACC ]     STREAM     LISTENING     21355    public/showq
unix  2      [ ACC ]     STREAM     LISTENING     8730     /run/systemd/journal/stdout
unix  5      [ ]         DGRAM                    8733     /run/systemd/journal/socket

  这会显示系统所有的tcp、udp以及unix连接。

  2)、检查所有tcp/udp/unix socket连接

  使用t选项只列出tcp连接。

[root@localhost wc]# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN     
tcp        0     52 localhost.localdoma:ssh 10.0.30.1:lnvmaps       ESTABLISHED
tcp6       0      0 [::]:mysql              [::]:*                  LISTEN     
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN     
[root@localhost wc]# 

  类似的,使用u选项只列出udp连接。

[root@localhost wc]# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
[root@localhost wc]# 

  使用x选项只列出unix socket连接。

[root@localhost wc]# netstat -ax
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     13568    /run/lvm/lvmetad.socket
unix  2      [ ]         DGRAM                    13571    /run/systemd/shutdownd
unix  3      [ ]         DGRAM                    8710     /run/systemd/notify
unix  2      [ ]         DGRAM                    8712     /run/systemd/cgroups-agent
unix  2      [ ACC ]     STREAM     LISTENING     21355    public/showq
unix  2      [ ACC ]     STREAM     LISTENING     8730     /run/systemd/journal/stdout
unix  5      [ ]         DGRAM                    8733     /run/systemd/journal/socket
unix  12     [ ]         DGRAM                    8735     /dev/log
unix  2      [ ACC ]     SEQPACKET  LISTENING     13622    /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     21620    /tmp/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     21330    private/trace
unix  2      [ ACC ]     STREAM     LISTENING     21337    private/verify
unix  2      [ ACC ]     STREAM     LISTENING     21343    private/proxymap

  3)、同时列出ID/进程名称

  使用p选项可以列出连接的同时也显示PID或者进程名称,而且它还能与其他选项连用。

[root@localhost wc]# netstat -ap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      992/sshd            
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN      1563/master         
tcp        0     52 localhost.localdoma:ssh 10.0.30.1:lnvmaps       ESTABLISHED 1813/sshd: root@pts 
tcp6       0      0 [::]:mysql              [::]:*                  LISTEN      1631/mysqld         
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      992/sshd            
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN      1563/master         
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7           686/NetworkManager  
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     13568    1/systemd            /run/lvm/lvmetad.socket
unix  2      [ ]         DGRAM                    13571    1/systemd            /run/systemd/shutdownd
unix  3      [ ]         DGRAM                    8710     1/systemd            /run/systemd/notify
unix  2      [ ]         DGRAM                    8712     1/systemd            /run/systemd/cgroups-agent
unix  2      [ ACC ]     STREAM     LISTENING     21355    1563/master          public/showq
unix  2      [ ACC ]     STREAM     LISTENING     8730     1/systemd            /run/systemd/journal/stdout
unix  5      [ ]         DGRAM                    8733     1/systemd            /run/systemd/journal/socket
unix  12     [ ]         DGRAM                    8735     1/systemd            /dev/log
unix  2      [ ACC ]     SEQPACKET  LISTENING     13622    1/systemd            /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     21620    1631/mysqld          /tmp/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     21330    1563/master          private/trace
unix  2      [ ACC ]     STREAM     LISTENING     21337    1563/master          private/verify
unix  2      [ ACC ]     STREAM     LISTENING     21343    1563/master          private/proxymap
unix  2      [ ACC ]     STREAM     LISTENING     21346    1563/master          private/proxywrite
unix  2      [ ACC ]     STREAM     LISTENING     21349    1563/master          private/smtp
unix  2      [ ACC ]     STREAM     LISTENING     21352    1563/master          private/relay
unix  2      [ ACC ]     STREAM     LISTENING     21358    1563/master          private/error
unix  2      [ ACC ]     STREAM     LISTENING     21361    1563/master          private/retry

  4)、列出端口号而不是服务名

  使用n选项可以加快输出,它不会执行任何反向查询,而是直接输出数字。由于无需查询,因此结果输出会很快。

[root@localhost wc]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0     52 10.0.30.102:22          10.0.30.1:2284          ESTABLISHED
tcp6       0      0 :::3306                 :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
raw6       0      0 :::58                   :::*                    7          
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     13568    /run/lvm/lvmetad.socket
unix  2      [ ]         DGRAM                    13571    /run/systemd/shutdownd
unix  3      [ ]         DGRAM                    8710     /run/systemd/notify
unix  2      [ ]         DGRAM                    8712     /run/systemd/cgroups-agent
unix  2      [ ACC ]     STREAM     LISTENING     21355    public/showq
unix  2      [ ACC ]     STREAM     LISTENING     8730     /run/systemd/journal/stdout
unix  5      [ ]         DGRAM                    8733     /run/systemd/journal/socket
unix  12     [ ]         DGRAM                    8735     /dev/log
unix  2      [ ACC ]     SEQPACKET  LISTENING     13622    /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     21620    /tmp/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     21330    private/trace
unix  2      [ ACC ]     STREAM     LISTENING     21337    private/verify
unix  2      [ ACC ]     STREAM     LISTENING     21343    private/proxymap
unix  2      [ ACC ]     STREAM     LISTENING     21346    private/proxywrite
unix  2      [ ACC ]     STREAM     LISTENING     21349    private/smtp
unix  2      [ ACC ]     STREAM     LISTENING     21352    private/relay

  5)、只输出监听端口

  使用l选项只输出监听端口。它不能与a选项连用,因为a会输出所有端口。

[root@localhost wc]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN     
tcp6       0      0 [::]:mysql              [::]:*                  LISTEN     
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN     
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7          
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     13568    /run/lvm/lvmetad.socket
unix  2      [ ACC ]     STREAM     LISTENING     21355    public/showq
unix  2      [ ACC ]     STREAM     LISTENING     8730     /run/systemd/journal/stdout
unix  2      [ ACC ]     SEQPACKET  LISTENING     13622    /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     21620    /tmp/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     21330    private/trace
unix  2      [ ACC ]     STREAM     LISTENING     21337    private/verify
unix  2      [ ACC ]     STREAM     LISTENING     21343    private/proxymap
unix  2      [ ACC ]     STREAM     LISTENING     21346    private/proxywrite
unix  2      [ ACC ]     STREAM     LISTENING     21349    private/smtp
unix  2      [ ACC ]     STREAM     LISTENING     21352    private/relay
unix  2      [ ACC ]     STREAM     LISTENING     21358    private/error
unix  2      [ ACC ]     STREAM     LISTENING     21361    private/retry
unix  2      [ ACC ]     STREAM     LISTENING     21364    private/discard
unix  2      [ ACC ]     STREAM     LISTENING     21367    private/local
unix  2      [ ACC ]     STREAM     LISTENING     21370    private/virtual
unix  2      [ ACC ]     STREAM     LISTENING     21373    private/lmtp
unix  2      [ ACC ]     STREAM     LISTENING     21377    private/anvil
unix  2      [ ACC ]     STREAM     LISTENING     21380    private/scache
unix  2      [ ACC ]     STREAM     LISTENING     13436    /run/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     16805    /run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     21297    public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     21302    public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     21305    public/qmgr
unix  2      [ ACC ]     STREAM     LISTENING     21340    public/flush
unix  2      [ ACC ]     STREAM     LISTENING     21309    private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     21321    private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     21324    private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     21327    private/defer
unix  2      [ ACC ]     STREAM     LISTENING     13761    /run/lvm/lvmpolld.socket
unix  2      [ ACC ]     STREAM     LISTENING     17657    /var/run/vmware/guestServicePipe
[root@localhost wc]# 

  6)、输出网络状态

  使用s选项输出每个协议的统计信息,包括接收/发送的包数量。

[root@localhost wc]# netstat -s
Ip:
    4688 total packets received
    0 forwarded
    0 incoming packets discarded
    4408 incoming packets delivered
    4110 requests sent out
Icmp:
    818 ICMP messages received
    409 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 818
    818 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 818
IcmpMsg:
        InType3: 818
        OutType3: 818
Tcp:
    409 active connections openings
    1 passive connection openings
    409 failed connection attempts
    0 connection resets received
    1 connections established
    3574 segments received
    2888 segments send out
    409 segments retransmited
    0 bad segments received.
    0 resets sent
Udp:
    16 packets received
    0 packets to unknown port received.
    0 packet receive errors
    16 packets sent
    0 receive buffer errors
    0 send buffer errors
UdpLite:
TcpExt:
    14 delayed acks sent
    1 delayed acks further delayed because of locked socket
    1041 packet headers predicted
    556 acknowledgments not containing data payload received
    1386 predicted acknowledgments
    409 other TCP timeouts
    TCPRcvCoalesce: 1
    TCPSpuriousRtxHostQueues: 760
    TCPSynRetrans: 409
    TCPOrigDataSent: 2440
IpExt:
    InBcastPkts: 280
    InOctets: 374032
    OutOctets: 465867
    InBcastOctets: 27427
    InNoECTPkts: 4688
[root@localhost wc]# 

  7)、输出网卡状态

  使用i选项只显示网卡的统计信息。

[root@localhost wc]# netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33     1500     4152      0      0 0          5224      0      0      0 BMRU
lo       65536      822      0      0 0           822      0      0      0 LRU
[root@localhost wc]# 

  8)、显示多播组信息

  使用g选项输出IPV4以及IPV6的多播信息。

[root@localhost wc]# netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      all-systems.mcast.net
ens33           1      all-systems.mcast.net
lo              1      ff02::1
lo              1      ff01::1

  9)、显示网络路由信息

  使用r选项输出网络路由信息。

[root@localhost wc]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 ens33
10.0.30.0       0.0.0.0         255.255.254.0   U         0 0          0 ens33
[root@localhost wc]# 

  10)、持续输出

  使用c选项持续输出信息。

[root@localhost wc]# netstat -c

  

  

posted @ 2016-03-02 14:02  周国伟  阅读(391)  评论(0编辑  收藏  举报