【Linux 高級命令】

1、13個超實用的Linux性能監測命令行工具

http://os.51cto.com/art/201304/388673_all.htmnode

1. Top:監測Linux進程python

Linux Top命令是一個性能監測程序,它常常被許多系統管理員用來監測Linux性能,在許多相似Linux/Unix的操做系統環境下都能找到。Top命令可用於顯示全部運行中和活動的實時進程(按順序排列),而且按期更新。它可顯示處理器使用狀況、內存使用狀況、交換內存、緩存大小、緩衝器大小、進程標識符(PID)、用戶、命令及更多的信息。它還顯示了某個運行中進程大量使用內存和處理器的狀況。Top命令對系統管理員大有用處,可用來監測,須要時還可採起正確的行動。不妨看看Top命令的實際使用狀況。mysql

# top

Top命令示例linux

2. VmStat:顯示虛擬內存方面的統計信息ios

Linux VmStat命令用來顯示虛擬內存、內核線程、磁盤、系統進程、輸入/輸出塊、中斷、處理器活動及更多方面的統計信息。默認狀況下,vmstat命令並不出如今Linux系統環境下,你須要安裝一個名爲sysstat的程序包,該程序包裏面含有一個vmstat程序。命令格式一般這樣使用:es6

# vmstat

 

 procs -----------memory----------swap-- -----io---- --system-- -----cpu----

r b swpd free inact active si   so  bi  bo  in  cs us sy id wa stsql

1 0  0 810420  97380  70628   0  0  115  4   89  79  1  6 90  3  0shell

 

3. Lsof:列出打開的文件數據庫

Lsof命令用於許多相似Linux/Unix的系統中,用來顯示全部打開的文件和進程。打開的文件包括:磁盤文件、網絡套接字、管道、設備和進程。使用這個命令的主要場合之一是,當磁盤沒法卸載,顯示文件被使用或被打開這個錯誤信息時。有了這個命令,你很容易查明哪些文件在使用中。這個命令最多見的格式以下:json

# lsof

 

 COMMAND PID     USER FD       TYPE     DEVICE     SIZE     NODE   NAME

init  1  root  cwd    DIR   104,2      4096         2   /

init  1  root  rtd     DIR   104,2      4096         2   /

init  1  root  txt      REG   104,2   38652  17710339  /sbin/init

init  1  root  mem    REG   104,2   129900    196453  /lib/ld-2.5.so

init  1  root  mem    REG   104,2   1693812    196454  /lib/libc-2.5.so

init  1  root  mem    REG   104,2   20668    196479  /lib/libdl-2.5.so

init  1  root  mem    REG   104,2   245376    196419  /lib/libsepol.so.1

init  1  root  mem   REG   104,2   93508    196431  /lib/libselinux.so.1

init  1  root   10u    FIFO   0,17     953   /dev/initctl

 

 

4. Tcpdump:網絡數據包分析器

Tcpdump是使用最普遍的命令行網絡數據包分析器或數據包嗅探程序之一,用於捕捉或過濾在網絡上經過某個接口接收或傳輸的TCP/IP數據包。它還提供了這個選項:把捕捉到的數據包保存到一個文件中,供之後分析。Tcpdump幾乎出如今全部主要的Linux發行版環境下。

# tcpdump -i eth0

 

 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

22:08:59.617628 IP tecmint.com.ssh > 115.113.134.3.static-mumbai.vsnl.net.in.28472: P 2532133365:2532133481(116) ack 3561562349 win 9648

22:09:07.653466 IP tecmint.com.ssh > 115.113.134.3.static-mumbai.vsnl.net.in.28472: P 116:232(116) ack 1 win 9648

22:08:59.617916 IP 115.113.134.3.static-mumbai.vsnl.net.in.28472 > tecmint.com.ssh: . ack 116 win 64347

 

5. Netstat:顯示網絡方面的統計信息

除了用於監測接口方面的統計信息外,Netstat這個命令行工具還用於監測進出的網絡數據包方面的統計信息。它對每一個系統管理員來講是個頗有用的工具,可用於監測網絡性能,並排查網絡相關問題。

# netstat -a | more

 

 Active Internet connections (servers and established)

Proto  Recv-Q Send-Q Local Address              Foreign Address             State

tcp        0      0 *:mysql                   *:*                         LISTEN

tcp        0      0 *:sunrpc                   *:*                         LISTEN

tcp        0      0 *:realm-rusd                *:*                         LISTEN

tcp        0      0 *:ftp                      *:*                         LISTEN

tcp        0      0 localhost.localdomain:ipp     *:*                         LISTEN

tcp        0      0 localhost.localdomain:smtp    *:*                         LISTEN

tcp        0      0 localhost.localdomain:smtp    localhost.localdomain:42709    TIME_WAIT

tcp        0      0 localhost.localdomain:smtp    localhost.localdomain:42710    TIME_WAIT

tcp        0      0 *:http                     *:*                         LISTEN

tcp        0      0 *:ssh                      *:*                         LISTEN

tcp        0      0 *:https                     *:*                         LISTEN

 

6. Htop:監測Linux進程

Htop是一款很是先進的交互式實時Linux進程監測工具。它很是相似Linux top命令,可是有一些豐富的功能特性,好比易於使用的界面,可用於管理進程、快捷鍵、進程的垂直和水平視圖以及其餘對象。Htop是一個第三方工具,並不包含在Linux系統中,你須要使用YUM程序包管理器工具來安裝它。想了解安裝方面的更多信息,請參閱本文

# htop

Htop命令示例的屏幕截圖

 

7. Iotop:監測Linux磁盤的輸入/輸出

Iotop也很是相似top命令和Htop程序,可是它有記帳功能,可用於監測和顯示實時磁盤輸入/輸出及進程。這個工具很是有用,可用於查找具體的進程以及進程的頻繁使用的磁盤讀取/寫入操做。

# iotop

Iotop命令示例的屏幕截圖

8. Iostat:顯示輸入/輸出方面的統計信息

IoStat是款簡單的工具,能夠收集和顯示系統輸入/輸出存儲設備方面的統計信息。這個工具常常用於追查存儲設備性能方面的問題,包括設備、本地磁盤和NFS等遠程磁盤。

# iostat

 

 Linux 2.6.18-238.9.1.el5 (tecmint.com)         09/13/2012

avg-cpu:  %user   %nice %system %iowait  %steal   %idle

2.60    3.65    1.04    4.29    0.00   88.42

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn

cciss/c0d0       17.79       545.80       256.52  855159769  401914750

cciss/c0d0p1      0.00         0.00         0.00       5459       3518

cciss/c0d0p2     16.45       533.97       245.18  836631746  384153384

cciss/c0d0p3      0.63         5.58         3.97    8737650    6215544

cciss/c0d0p4      0.00         0.00         0.00          8          0

cciss/c0d0p5      0.63         3.79         5.03    5936778    7882528

cciss/c0d0p6      0.08         2.46         2.34    3847771    3659776

 

9. IPTraf:實時監測IP局域網

IPTraf是一個基於控制檯的開源實時網絡(IP LAN)監測實用工具,面向Linux。它能夠收集經過網絡傳輸的衆多信息(好比IP流量監測器),包括TCP標記信息、ICMP詳細信息、TCP/UDP流量故障、TCP鏈接數據包以及字節計數。它還能夠收集接口方面普通和詳細的統計信息,好比TCP、UDP、IP、ICMP、非IP、IP校驗和錯誤以及接口活動等。

IP流量監測器

 

10. psacct或acct:監測用戶活動

psacct或acct這兩個工具大有用處,可用於監測系統上每一個用戶的活動。這兩個守護程序都在後臺運行,監測系統上每一個用戶的整體活動,還監測它們在使用什麼資源。

這些工具對系統管理員們來講大有用處,可用於跟蹤每一個用戶的活動,好比用戶在從事什麼操做,他們發出了什麼命令,他們使用了多少資源,以及他們在系統上處於活動狀態已有多久,等等。

11. Monit:監測Linux進程和服務

Monit是一款免費的開源、基於Web的進程監測實用工具,能夠自動監測和管理系統進程、程序、文件、目錄、許可權限、校驗和以及文件系統。

它能夠監測Apache、MySQL、Mail、FTP、ProFTP、Nginx和SSH等服務。能夠從命令行或使用其本身的Web界面來查看系統狀態。

Monit Linux進程監測

12. NetHogs:監測每一個進程的網絡帶寬

NetHogs是一款優秀、小巧的開源程序(相似Linux top命令),可密切監測系統上每一個進程的網絡活動。它還密切跟蹤每一個程序或應用軟件所使用的實時網絡流量帶寬。

NetHogs Linux帶寬監測

13. iftop:監測網絡帶寬

iftop是另外一款基於終端的免費開源系統監測實用工具,可顯示一份常常更新的列表,該列表顯示了經過系統上網絡接口的網絡帶寬使用狀況。iftop一般用於監測網絡使用狀況,就像top一般用於監測處理器使用狀況。iftop是屬於top家族的工具,可監測某個所選擇的接口,並顯示兩個主機之間目前的帶寬使用狀況。

Iftop:網絡帶寬監測

咱們想知道各位使用哪一種監測程序來監測Linux服務器的性能?要是咱們遺漏了任何你但願咱們補充進來的重要工具,請留言告知咱們,歡迎分享。

20個對Linux專家很是有用命令

 

http://os.51cto.com/art/201308/406979_all.htm

 

1. 命令: ifconfig

ifconfig用來配置常駐內核的網絡接口信息。在系統啓動必要時用來設置網絡適配器的信息。以後,它一般是隻須要在調試時或當系統須要調整時使用。

檢查活動網絡適配器

  1. [avishek@tecmint ~]$ ifconfig 
  2. eth0      Link encap:Ethernet  HWaddr 40:2C:F4:EA:CF:0E 
  3. inet addr:192.168.1.3  Bcast:192.168.1.255  Mask:255.255.255.0 
  4. inet6 addr: fe80::422c:f4ff:feea:cf0e/64 Scope:Link 
  5. UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
  6. RX packets:163843 errors:0 dropped:0 overruns:0 frame:0 
  7. TX packets:124990 errors:0 dropped:0 overruns:0 carrier:0 
  8. collisions:0 txqueuelen:1000 
  9. RX bytes:154389832 (147.2 MiB)  TX bytes:65085817 (62.0 MiB) 
  10. Interrupt:20 Memory:f7100000-f7120000 
  11. lo        Link encap:Local Loopback 
  12. inet addr:127.0.0.1  Mask:255.0.0.0 
  13. inet6 addr: ::1/128 Scope:Host 
  14. UP LOOPBACK RUNNING  MTU:16436  Metric:1 
  15. RX packets:78 errors:0 dropped:0 overruns:0 frame:0 
  16. TX packets:78 errors:0 dropped:0 overruns:0 carrier:0 
  17. collisions:0 txqueuelen:0 
  18. RX bytes:4186 (4.0 KiB)  TX bytes:4186 (4.0 KiB) 

檢查全部的網絡適配器

「-a」參數用來顯示全部網絡適配器(網卡)的詳細信息,包括那些停用的適配器。

  1. [avishek@tecmint ~]$ ifconfig -a 
  2. eth0      Link encap:Ethernet  HWaddr 40:2C:F4:EA:CF:0E 
  3. inet addr:192.168.1.3  Bcast:192.168.1.255  Mask:255.255.255.0 
  4. inet6 addr: fe80::422c:f4ff:feea:cf0e/64 Scope:Link 
  5. UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
  6. RX packets:163843 errors:0 dropped:0 overruns:0 frame:0 
  7. TX packets:124990 errors:0 dropped:0 overruns:0 carrier:0 
  8. collisions:0 txqueuelen:1000 
  9. RX bytes:154389832 (147.2 MiB)  TX bytes:65085817 (62.0 MiB) 
  10. Interrupt:20 Memory:f7100000-f7120000 
  11. lo        Link encap:Local Loopback 
  12. inet addr:127.0.0.1  Mask:255.0.0.0 
  13. inet6 addr: ::1/128 Scope:Host 
  14. UP LOOPBACK RUNNING  MTU:16436  Metric:1 
  15. RX packets:78 errors:0 dropped:0 overruns:0 frame:0 
  16. TX packets:78 errors:0 dropped:0 overruns:0 carrier:0 
  17. collisions:0 txqueuelen:0 
  18. RX bytes:4186 (4.0 KiB)  TX bytes:4186 (4.0 KiB) 
  19. virbr0    Link encap:Ethernet  HWaddr 0e:30:a3:3a:bf:03 
  20. inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0 
  21. UP BROADCAST MULTICAST  MTU:1500  Metric:1 
  22. RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
  23. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
  24. collisions:0 txqueuelen:0 
  25. RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B) 

停用網絡適配器

  1. [avishek@tecmint ~]$ ifconfig eth0 down 

啓用網絡適配器

  1. [avishek@tecmint ~]$ ifconfig eth0 up 

指定IP地址到網絡適配器

爲網絡適配器eth0設定IP地址「192.168.1.12」.

  1. [avishek@tecmint ~]$ ifconfig eth0 192.168.1.12 

更改網絡適配器eth0的子網掩碼:

  1. [avishek@tecmint ~]$ ifconfig eth0 netmask 255.255.255. 

更改網絡適配器eth0的廣播地址:

  1. [avishek@tecmint ~]$ ifconfig eth0 broadcast 192.168.1.255 

爲網絡適配器eth0指定IP地址,子網掩碼,廣播地址:

  1. [avishek@tecmint ~]$ ifconfig eth0 192.168.1.12 netmask 255.255.255.0 broadcast 192.168.1.255 

注Note: 若是你設置一塊無線網卡的信息,你可使用的命令是「iwconfig」.欲知更多ifconfig命令的例子和使用方法,讀「15個有用的ifconfig 命令」。

2. 命令: netstat

netstat命令顯示各類網絡相關的信息,如網絡鏈接,路由表,接口統計,假裝鏈接,組播成員身份等....

列出全部的網絡端口

  1. [avishek@tecmint ~]$ netstat -a 
  2. Active UNIX domain sockets (servers and established) 
  3. Proto RefCnt Flags       Type       State         I-Node   Path 
  4. unix  2      [ ACC ]     STREAM     LISTENING     741379   /run/user/user1/keyring-I5cn1c/gpg 
  5. unix  2      [ ACC ]     STREAM     LISTENING     8965     /var/run/acpid.socket 
  6. unix  2      [ ACC ]     STREAM     LISTENING     18584    /tmp/.X11-unix/X0 
  7. unix  2      [ ACC ]     STREAM     LISTENING     741385   /run/user/user1/keyring-I5cn1c/ssh 
  8. unix  2      [ ACC ]     STREAM     LISTENING     741387   /run/user/user1/keyring-I5cn1c/pkcs11 
  9. unix  2      [ ACC ]     STREAM     LISTENING     20242    @/tmp/dbus-ghtTjuPN46 
  10. unix  2      [ ACC ]     STREAM     LISTENING     13332    /var/run/samba/winbindd_privileged/pipe 
  11. unix  2      [ ACC ]     STREAM     LISTENING     13331    /tmp/.winbindd/pipe 
  12. unix  2      [ ACC ]     STREAM     LISTENING     11030    /var/run/mysqld/mysqld.sock 
  13. unix  2      [ ACC ]     STREAM     LISTENING     19308    /tmp/ssh-qnZadSgJAbqd/agent.3221 
  14. unix  2      [ ACC ]     STREAM     LISTENING     436781   /tmp/HotShots 
  15. unix  2      [ ACC ]     STREAM     LISTENING     46110    /run/user/ravisaive/pulse/native 
  16. unix  2      [ ACC ]     STREAM     LISTENING     19310    /tmp/gpg-zfE9YT/S.gpg-agent 
  17. .... 

顯示全部tcp相關端口

  1. [avishek@tecmint ~]$ netstat -at 
  2. Active Internet connections (servers and established) 
  3. Proto Recv-Q Send-Q Local Address           Foreign Address         State 
  4. tcp        0      0 localhost:mysql         *:*                     LISTEN 
  5. tcp        0      0 *:5901                  *:*                     LISTEN 
  6. tcp        0      0 *:5902                  *:*                     LISTEN 
  7. tcp        0      0 *:x11-1                 *:*                     LISTEN 
  8. tcp        0      0 *:x11-2                 *:*                     LISTEN 
  9. tcp        0      0 *:5938                  *:*                     LISTEN 
  10. tcp        0      0 localhost:5940          *:*                     LISTEN 
  11. tcp        0      0 ravisaive-OptiPl:domain *:*                     LISTEN 
  12. tcp        0      0 ravisaive-OptiPl:domain *:*                     LISTEN 
  13. tcp        0      0 localhost:ipp           *:*                     LISTEN 
  14. tcp        0      0 ravisaive-OptiPle:48270 ec2-23-21-236-70.c:http ESTABLISHED 
  15. tcp        0      0 ravisaive-OptiPle:48272 ec2-23-21-236-70.c:http TIME_WAIT 
  16. tcp        0      0 ravisaive-OptiPle:48421 bom03s01-in-f22.1:https ESTABLISHED 
  17. tcp        0      0 ravisaive-OptiPle:48269 ec2-23-21-236-70.c:http ESTABLISHED 
  18. tcp        0      0 ravisaive-OptiPle:39084 channel-ecmp-06-f:https ESTABLISHED 
  19. ... 

顯示全部鏈接的統計信息

  1. [avishek@tecmint ~]$ netstat -s 
  2. Ip: 
  3. 4994239 total packets received 
  4. 0 forwarded 
  5. 0 incoming packets discarded 
  6. 4165741 incoming packets delivered 
  7. 3248924 requests sent out 
  8. 8 outgoing packets dropped 
  9. Icmp: 
  10. 29460 ICMP messages received 
  11. 566 input ICMP message failed. 
  12. ICMP input histogram: 
  13. destination unreachable: 98 
  14. redirects: 29362 
  15. 2918 ICMP messages sent 
  16. 0 ICMP messages failed 
  17. ICMP output histogram: 
  18. destination unreachable: 2918 
  19. IcmpMsg: 
  20. InType3: 98 
  21. InType5: 29362 
  22. OutType3: 2918 
  23. Tcp: 
  24. 94533 active connections openings 
  25. 23 passive connection openings 
  26. 5870 failed connection attempts 
  27. 7194 connection resets received 
  28. .... 

好的!因爲某些緣由若是你不想解析netstat 輸出的主機、端口和用戶名稱的話 。

  1. [avishek@tecmint ~]$ netstat -an 

好,你可能須要獲取的 netstat 持續輸出的動態信息,經過傳遞中斷輸出指令 (ctrl + c)來中止。

  1. [avishek@tecmint ~]$ netstat -c 

更多關於「netstat」的例子和使用方法,瀏覽文章「20個netstat 的使用案例」。

3. 命令: nslookup

網絡實用程序,用於得到互聯網服務器的信息。顧名思義,該實用程序將發現經過查詢 DNS 域的名稱服務器信息。

  1. [avishek@tecmint ~]$ nslookup tecmint.com 
  2. Server:     192.168.1.1 
  3. Address:    192.168.1.1#53 
  4. Non-authoritative answer: 
  5. Name:   tecmint.com 
  6. Address: 50.16.67.239 

查詢郵件 交換器 記錄

  1. [avishek@tecmint ~]$ nslookup -query=mx tecmint.com 
  2. Server:     192.168.1.1 
  3. Address:    192.168.1.1#53 
  4. Non-authoritative answer: 
  5. tecmint.com mail exchanger = 0 smtp.secureserver.net. 
  6. tecmint.com mail exchanger = 10 mailstore1.secureserver.net. 
  7. Authoritative answers can be found from: 

查詢域名服務器

  1. [avishek@tecmint ~]$ nslookup -type=ns tecmint.com 
  2. Server:     192.168.1.1 
  3. Address:    192.168.1.1#53 
  4. Non-authoritative answer: 
  5. tecmint.com nameserver = ns3404.com. 
  6. tecmint.com nameserver = ns3403.com. 
  7. Authoritative answers can be found from: 

查詢DNS記錄

  1. [avishek@tecmint ~]$ nslookup -type=any tecmint.com 
  2. Server:     192.168.1.1 
  3. Address:    192.168.1.1#53 
  4. Non-authoritative answer: 
  5. tecmint.com mail exchanger = 10 mailstore1.secureserver.net. 
  6. tecmint.com mail exchanger = 0 smtp.secureserver.net. 
  7. tecmint.com nameserver = ns06.domaincontrol.com. 
  8. tecmint.com nameserver = ns3404.com. 
  9. tecmint.com nameserver = ns3403.com. 
  10. tecmint.com nameserver = ns05.domaincontrol.com. 
  11. Authoritative answers can be found from: 

查詢 起始 受權機構

  1. [avishek@tecmint ~]$ nslookup -type=soa tecmint.com 
  2. Server:     192.168.1.1 
  3. Address:    192.168.1.1#53 
  4. Non-authoritative answer: 
  5. tecmint.com 
  6. origin = ns3403.hostgator.com 
  7. mail addr = dnsadmin.gator1702.hostgator.com 
  8. serial = 2012081102 
  9. refresh = 86400 
  10. retry = 7200 
  11. expire = 3600000 
  12. minimum = 86400 
  13. Authoritative answers can be found from: 

查詢端口號

更改使用你想要鏈接的端口號:

  1. [avishek@tecmint ~]$ nslookup -port 56 tecmint.com 
  2. Server:     tecmint.com 
  3. Address:    50.16.76.239#53 
  4. Name:   56 
  5. Address: 14.13.253.12 

更多閱讀 8個Nslookup 命令

4. 命令: dig

dig是查詢DNS 域名服務器的工具,能夠查詢的主機地址、 郵件交流、 域名服務器相關的信息。在任何 Linux (Unix) 或 Macintosh OS X 操做系統上,均可以使用該工具。dig的最典型的用法是單個主機的查詢。

  1. [avishek@tecmint ~]$ dig tecmint.com 
  2. <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com 
  3. ;; global options: +cmd 
  4. ;; Got answer: 
  5. ;; ->>HEADER

關閉註釋行

  1. [avishek@tecmint ~]$ dig tecmint.com +nocomments 
  2. <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com +nocomments 
  3. ;; global options: +cmd 
  4. ;tecmint.com.           IN  A 
  5. tecmint.com.        14400   IN  A   40.216.66.239 
  6. ;; Query time: 418 msec 
  7. ;; SERVER: 192.168.1.1#53(192.168.1.1) 
  8. ;; WHEN: Sat Jun 29 13:53:22 2013 
  9. ;; MSG SIZE  rcvd: 45 

關閉認證塊

  1. [avishek@tecmint ~]$ dig tecmint.com +noauthority 
  2. <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com +noauthority 
  3. ;; global options: +cmd 
  4. ;; Got answer: 
  5. ;; ->>HEADER

關閉其餘塊

  1. [avishek@tecmint ~]$ dig  tecmint.com +noadditional 
  2. <<>> DiG 9.9.2-P1 <<>> tecmint.com +noadditional 
  3. ;; global options: +cmd 
  4. ;; Got answer: 
  5. ;; ->>HEADER

關閉統計塊

  1. [avishek@tecmint ~]$ dig tecmint.com +nostats 
  2. <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com +nostats 
  3. ;; global options: +cmd 
  4. ;; Got answer: 
  5. ;; ->>HEADER

關閉回覆塊

  1. [avishek@tecmint ~]$ dig tecmint.com +noanswer 
  2. <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com +noanswer 
  3. ;; global options: +cmd 
  4. ;; Got answer: 
  5. ;; ->>HEADER

關閉全部塊

  1. [avishek@tecmint ~]$ dig tecmint.com +noall 
  2. <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com +noall 
  3. ;; global options: +cmd 

閱讀更多10 個Linux Dig 命令實例

5.命令: uptime

你鏈接到你的 Linux 服務器時發現一些不尋常或惡意的東西,你會作什麼?猜想......不,毫不!你能夠運行uptime來驗證當服務器無人值守式到底發生了什麼事情。

  1. [avishek@tecmint ~]$ uptime 
  2. 14:37:10 up  4:21,  2 users,  load average: 0.00, 0.00, 0.04 

6. 命令: wall

對系統管理員來講一個最重要的命令.wall發送一條消息到你們登陸端將其 mesg 權限設置爲"yes"。這條信息能夠被wall做爲參數,或者能夠將它做爲wall的標準輸入。

  1. [avishek@tecmint ~]$ wall "we will be going down for maintenance for one hour sharply at 03:30 pm" 
  2. Broadcast message from root@localhost.localdomain (pts/0) (Sat Jun 29 14:44:02 2013): 
  3. we will be going down for maintenance for one hour sharply at 03:30 pm 

7. 命令: mesg

其餘人們可使用"wtrite"命令,將在在向您發送文本到屏幕上。你能夠控制是否顯示。

  1. mesg [<strong>n</strong>|<strong>y</strong>] <strong>n</strong> - prevents the message from others popping up on the screen. <strong>y</strong> – Allows messages to appear on your screen. 

 

8. 命令: write

若是 'mesg' 是 'y',讓你的文本直接發送到另外一臺 Linux 機器的屏幕。

  1. [avishek@tecmint ~]$ write ravisaive 

9. 命令: talk

加強的write命令,talk命令可以讓你與其餘登陸的用戶交談。

  1. [avishek@tecmint ~]$ talk ravisaive 

註釋: 若是 talk 命令沒安裝的話,能夠經過apt 或yum 安裝所需的包.

  1. view sourceprint? 
  2. [avishek@tecmint ~]$ yum install talk 
  3. OR 
  4. [avishek@tecmint ~]$ apt-get install talk 

10. 命令:w

是否以爲命令'w'很滑稽?可是事實上不是的。它是一個命令,儘管只有一個字符長!命令"w"是uptime和who命令,之前後的順序組合在一塊兒。

  1. [avishek@tecmint ~]$ w 
  2. 15:05:42 up  4:49,  3 users,  load average: 0.02, 0.01, 0.00 
  3. USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT 
  4. server   tty7     :0               14:06    4:43m  1:42   0.08s pam: gdm-passwo 
  5. server   pts/0    :0.0             14:18    0.00s  0.23s  1.65s gnome-terminal 
  6. server   pts/1    :0.0             14:47    4:43   0.01s  0.01s bash 

11. 命令: rename

見名知意,這個命令重命名文件。rename將會經過從文件名的首字符開始替換,重命名爲指定的文件名。

  1. Give the file names a1, a2, a3, a4.....1213 

僅僅寫這些命令:[@Lesus 注: 在Ubuntu上不支持這種格式, rename與mv不一樣的是,rename能夠批量修改,如同帶了while的mv操做。]

  1. view sourceprint? 
  2. rename a1 a0 a? 
  3. rename a1 a0 a?? 

12. 命令: top

顯示CPU進程信息。這個命令自動刷新,默認是持續顯示CPU進程信息,除非使用了中斷指令。

  1. [avishek@tecmint ~]$ top 
  2. top - 14:06:45 up 10 days, 20:57,  2 users,  load average: 0.10, 0.16, 0.21 
  3. Tasks: 240 total,   1 running, 235 sleeping,   0 stopped,   4 zombie 
  4. %Cpu(s):  2.0 us,  0.5 sy,  0.0 ni, 97.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st 
  5. KiB Mem:   2028240 total,  1777848 used,   250392 free,    81804 buffers 
  6. KiB Swap:  3905532 total,   156748 used,  3748784 free,   381456 cached 
  7. PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+ COMMAND 
  8. 23768 ravisaiv  20   0 1428m 571m  41m S   2.3 28.9  14:27.52 firefox 
  9. 24182 ravisaiv  20   0  511m 132m  25m S   1.7  6.7   2:45.94 plugin-containe 
  10. 26929 ravisaiv  20   0  5344 1432  972 R   0.7  0.1   0:00.07 top 
  11. 24875 ravisaiv  20   0  263m  14m  10m S   0.3  0.7   0:02.76 lxterminal 
  12. 1 root      20   0  3896 1928 1228 S   0.0  0.1   0:01.62 init 
  13. 2 root      20   0     0    0    0 S   0.0  0.0   0:00.06 kthreadd 
  14. 3 root      20   0     0    0    0 S   0.0  0.0   0:17.28 ksoftirqd/0 
  15. 5 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kworker/0:0H 
  16. 7 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kworker/u:0H 
  17. 8 root      rt   0     0    0    0 S   0.0  0.0   0:00.12 migration/0 
  18. 9 root      20   0     0    0    0 S   0.0  0.0   0:00.00 rcu_bh 
  19. 10 root      20   0     0    0    0 S   0.0  0.0   0:26.94 rcu_sched 
  20. 11 root      rt   0     0    0    0 S   0.0  0.0   0:01.95 watchdog/0 
  21. 12 root      rt   0     0    0    0 S   0.0  0.0   0:02.00 watchdog/1 
  22. 13 root      20   0     0    0    0 S   0.0  0.0   0:17.80 ksoftirqd/1 
  23. 14 root      rt   0     0    0    0 S   0.0  0.0   0:00.12 migration/1 
  24. 16 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kworker/1:0H 
  25. 17 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 cpuset 
  26. 18 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 khelper 
  27. 19 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kdevtmpfs 
  28. 20 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 netns 
  29. 21 root      20   0     0    0    0 S   0.0  0.0   0:00.04 bdi-default 
  30. 22 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kintegrityd 
  31. 23 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kblockd 
  32. 24 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 ata_sff 

另查看 12 TOP命令例子 ·[@Lesus 注:htop比top命令更好用,不過須要本身安裝]

13. 命令: mkfs.ext4

這個命令在指定的設備上建立一個新的ext4文件系統,若是這個命令後面跟的是個錯誤的設備,那麼整個設備就會被擦除和格式化,因此建議不要運行這個命令,除非你清楚本身正在幹什麼。

  1. Mkfs.ext4 /dev/sda1 (sda1 block will be formatted) 
  2. mkfs.ext4 /dev/sdb1 (sdb1 block will be formatted) 

更多查看: Ext4是什麼及怎麼建立和轉換

14. vi/emac/nano 命令

vi (visual), emac, nano 是 linux 中最經常使用的一些編輯器。它們常常用於編輯文本,配置,… 等文件. A quick guide to work around vi and nano is, emac is a.

vi 編輯器:

  1. [avishek@tecmint ~]$ touch a.txt (建立一個名爲a.txt的文本文件) 
  2. [avishek@tecmint ~]$ vi a.txt (用vi打開a.txt) 

[按下‘i’鍵進入插入模式, 不然你不能輸入任何內容]

  1. echo "Hello"  (這裏的文本會存到文件中) 
  • alt+x (退出插入模式, 記得在最後的字符間留有一些空格.
  • ctrl+x 命令或你上一個單詞將被刪除).
  • :wq! (以當前的文本保存文件, 記住‘!’ 是覆蓋的意思).

nano 編輯器:

  1. [avishek@tecmint ~]$ nano a.txt (用nano打開 a.txt) 
  2. edit, with the content, required 

ctrl +x (關閉編輯器).它會顯示以下的提示輸出信息:

  1. Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ? 
  2. Y Yes 
  3. N No           ^C Cancel 

點擊‘y’ 選擇 yes 並輸入文件名,就完成編輯了。

15. 命令: rsync

Rsync複製文件,參數-P開啓進度條。若是你已經安裝了rsync,你可使用一個簡單的別名。

  1. alias cp='rsync -aP' 

如今嘗試在終端複製一個大文件,這樣將會看到顯示剩餘部分的輸出,與進度條相似。

並且,保持和維護備份是系統管理員不得不作的最重要、最無聊的工做之一。Rsync是一個用於新建和維護備份的很是好用的終端工具(也存在許多其它工具)。

  1. [avishek@tecmint ~]$ rsync -zvr IMG_5267\ copy\=33\ copy\=ok.jpg ~/Desktop/ 
  2. sending incremental file list 
  3. IMG_5267 copy=33 copy=ok.jpg 
  4. sent 2883830 bytes  received 31 bytes  5767722.00 bytes/sec 
  5. total size is 2882771  speedup is 1.00 

注意: -z表示壓縮, -v表示詳細信息,-r表示遞歸。

16. 命令: free

跟蹤內存的使用和資源同樣重要,就像管理員執行的任何其它任務,可使用 'free' 命令來在這裏救援。

當前內存使用狀態Current Usage Status of Memory

  1. [avishek@tecmint ~]$ free 
  2. total       used       free     shared    buffers     cached 
  3. Mem:       2028240    1788272     239968          0      69468     363716 
  4. -/+ buffers/cache:    1355088     673152 
  5. Swap:      3905532     157076    3748456 

設置輸出單位爲KB,MB或GB

  1. [avishek@tecmint ~]$ free -b 
  2. total       used       free     shared    buffers     cached 
  3. Mem:    2076917760 1838272512  238645248          0   71348224  372670464 
  4. -/+ buffers/cache: 1394253824  682663936 
  5. Swap:   3999264768  160845824 3838418944 
  6. [avishek@tecmint ~]$ free -k 
  7. total       used       free     shared    buffers     cached 
  8. Mem:       2028240    1801484     226756          0      69948     363704 
  9. -/+ buffers/cache:    1367832     660408 
  10. Swap:      3905532     157076    3748456 
  11. [avishek@tecmint ~]$ free -m 
  12. total       used       free     shared    buffers     cached 
  13. Mem:          1980       1762        218          0         68        355 
  14. -/+ buffers/cache:       1338        641 
  15. Swap:         3813        153       3660 
  16. [avishek@tecmint ~]$ free -g 
  17. total       used       free     shared    buffers     cached 
  18. Mem:             1          1          0          0          0          0 
  19. -/+ buffers/cache:          1          0 
  20. Swap:            3          0          3 

以可讀的格式顯示,檢查當前內存使用:

  1. [avishek@tecmint ~]$ free -h 
  2. total       used       free     shared    buffers     cached 
  3. Mem:          1.9G       1.7G       208M         0B        68M       355M 
  4. -/+ buffers/cache:       1.3G       632M 
  5. Swap:         3.7G       153M       3.6G 

設定 時間間隔 後 ,持續檢查使用狀態:

  1. [avishek@tecmint ~]$ free -s 3 
  2. total       used       free     shared    buffers     cached 
  3. Mem:       2028240    1824096     204144          0      70708     364180 
  4. -/+ buffers/cache:    1389208     639032 
  5. Swap:      3905532     157076    3748456 
  6. total       used       free     shared    buffers     cached 
  7. Mem:       2028240    1824192     204048          0      70716     364212 
  8. -/+ buffers/cache:    1389264     638976 
  9. Swap:      3905532     157076    3748456 

閱讀更多10個Free命令使用實例

17. mysqldump 命令

好了,如今你從名字上就能明白這個命令所表明的做用。mysqldump 命令會轉儲(備份)數據庫的所有或特定一部分數據到一個給定的文件中。例如:

  1. [avishek@tecmint ~]$ mysqldump -u root -p --all-databases > /home/server/Desktop/backupfile.sql 

注意: mysqldump 須要 mysql 在運行中而且有正確的受權密碼。咱們在 用mysqldump命令備份數據庫中討論了一些有用的 「mysqldump」 命令用法。

18. mkpasswd 命令

根據指定的長度,產生一個難猜的隨機密碼。

  1. [avishek@tecmint ~]$ mkpasswd -l 10 
  2. zI4+Ybqfx9 
  3. [avishek@tecmint ~]$ mkpasswd -l 20 
  4. w0Pr7aqKk&hmbmqdrlmk 

注意: -l 10 產生一個10個字符的隨機密碼,而-l 20 產生 20個字符的密碼,它能夠設置爲任意長度來取得所但願的結果。這個命令頗有用,常常在腳本語言裏使用來產生隨機的密碼。你可能須要 yum 或 apt ‘expect’ 包來使用這個命令。

  1. [avishek@tecmint ~]$ yum install expect 
  2. 或 
  3. [avishek@tecmint ~]$ apt-get install expect 

19. Command: paste

合併兩個或多個文本文件,按行來進行合併。示例。若是 file1 的內容是:

  1. file2 是這樣的: 
  2. <pre class="brush:shell">[avishek@tecmint ~]$ paste file1 file2 > file3</pre
  3. <br
  4. 結果file3將是: 
  5. 1    a 
  6. 2    b 
  7. 3    c 

20.Command: lsof

lsof 是"list open files("列表中打開的文件") 的縮寫,顯示您的系統當前已打開的全部文件。這是很是有用的對於想找出哪些進程使用某一特定文件,或顯示爲單個進程打開全部文件。一些有用的 10 個lsof 命令示例,你可能會感興趣閱讀。

  1. [avishek@tecmint ~]$ lsof 
  2. COMMAND     PID   TID            USER   FD      TYPE     DEVICE SIZE/OFF       NODE NAME 
  3. init          1                  root  cwd       DIR        8,1     4096          2 / 
  4. init          1                  root  rtd       DIR        8,1     4096          2 / 
  5. init          1                  root  txt       REG        8,1   227432     395571 /sbin/init 
  6. init          1                  root  mem       REG        8,1    47080     263023 /lib/i386-linux-gnu/libnss_files-2.17.so 
  7. init          1                  root  mem       REG        8,1    42672     270178 /lib/i386-linux-gnu/libnss_nis-2.17.so 
  8. init          1                  root  mem       REG        8,1    87940     270187 /lib/i386-linux-gnu/libnsl-2.17.so 
  9. init          1                  root  mem       REG        8,1    30560     263021 /lib/i386-linux-gnu/libnss_compat-2.17.so 
  10. init          1                  root  mem       REG        8,1   124637     270176 /lib/i386-linux-gnu/libpthread-2.17.so 
  11. init          1                  root  mem       REG        8,1  1770984     266166 /lib/i386-linux-gnu/libc-2.17.so 
  12. init          1                  root  mem       REG        8,1    30696     262824 /lib/i386-linux-gnu/librt-2.17.so 
  13. init          1                  root  mem       REG        8,1    34392     262867 /lib/i386-linux-gnu/libjson.so.0.1.0 
  14. init          1                  root  mem       REG        8,1   296792     262889 /lib/i386-linux-gnu/libdbus-1.so.3.7.2 
  15. init          1                  root  mem       REG        8,1    34168     262840 /lib/i386-linux-gnu/libnih-dbus.so.1.0.0 
  16. init          1                  root  mem       REG        8,1    95616     262848 /lib/i386-linux-gnu/libnih.so.1.0.0 
  17. init          1                  root  mem       REG        8,1   134376     270186 /lib/i386-linux-gnu/ld-2.17.so 
  18. init          1                  root    0u      CHR        1,3      0t0       1035 /dev/null 
  19. init          1                  root    1u      CHR        1,3      0t0       1035 /dev/null 
  20. init          1                  root    2u      CHR        1,3      0t0       1035 /dev/null 
  21. init          1                  root    3r     FIFO        0,8      0t0       1714 pipe 
  22. init          1                  root    4w     FIFO        0,8      0t0       1714 pipe 
  23. init          1                  root    5r     0000        0,9        0       6245 anon_inode 
  24. init          1                  root    6r     0000        0,9        0       6245 anon_inode 
  25. init          1                  root    7u     unix 0xf5e91f80      0t0       8192 @/com/ubuntu/upstart 
  26. init          1                  root    8w      REG        8,1     3916        394 /var/log/upstart/teamviewerd.log.1 (deleted) 

這裏並無結束,系統管理員會不少東西,爲你提供漂亮的界面,服務於你的工做。系統管理其實是學習和實現的一門優雅的藝術。咱們會盡力給你介紹 linux 專業人員必須瞭解的知識以及一些其餘必要東西,linux自己是基礎的,簡單的。深刻理解是不斷學習的過程。你的美言好詞老是在不斷鼓勵咱們寫出更多的優秀,豐富的文章。「喜之以分享,助我來傳播」。

3、11 個頗有用但鮮有人知的 Linux 命令

http://os.51cto.com/art/201310/414075.htm

這篇文章的目的是介紹一些少有人知的Linux命令,它們必定會高效地幫你管理你的桌面/服務器。

1. sudo !!命令

沒有特定輸入sudo命令而運行,將給出沒有權限的錯誤。那麼,你不須要重寫整個命令,僅僅輸入'!!'就能夠抓取最後的命令。

  1. $ apt-get update 
  2. E: Could not open lock file /var/lib/apt/lists/lock - open(13: Permission denied) 
  3. E: Unable to lock directory /var/lib/apt/lists/ 
  4. E: Could not open lock file /var/lib/dpkg/lock - open(13: Permission denied) 
  5. E: Unable to lock the administration directory (/var/lib/dpkg/), are you root? 
  6. $ sudo !! 
  7. sudoapt-get update 
  8. [sudo] password forserver: 
  9. … 
  10. .. 
  11. Fetched 474 kB in16s (28.0 kB/s) 
  12. Reading package lists... Done 
  13. server@localhost:~$ 

2. python命令

下面的命令生產一個經過HTTP顯示文件夾結構樹的簡單網頁,能夠經過瀏覽器在端口8000訪問,直到發出中斷信號。

  1. # python -m SimpleHTTPServer 

 

3. mtr命令

咱們大多數都熟悉ping和traceroute。那對於把兩個命令的功能合二爲一的mtr命令呢。若是mtr沒在你的機子上安裝,apt或者yum須要的包。

  1. $ sudo apt-get install mtr (On Debian based Systems) 
  2. # yum install mtr (On Red Hat based Systems) 

如今運行mtr命令,開始查看mtr運行的主機和google.com直接的網絡鏈接。

  1. # mtr google.com 

mtr命令

4. Ctrl+x+e命令

這個命令對於管理員和開發者很是有用。爲了使天天的任務自動化,管理員須要經過輸入vi、vim、nano等打開編輯器。

僅僅從命令行快速的敲擊「Ctrl-x-e」,就能夠在編輯器中開始工做了。

5. nl命令

「nl命令」添加文件的行數。一個叫作'one.txt'的文件,其每行的內容是(Fedora、Debian、Arch、Slack和Suse),給每行添加行號。首先使用cat命令顯示「one.txt」的文件內容。

  1. # cat one.txt 
  2. fedora 
  3. debian 
  4. arch 
  5. slack 
  6. suse 

如今運行「nl命令」,以添加行號的方式來顯示。

  1. # nl one.txt  
  2. 1 fedora 
  3. 2 debian 
  4. 3 arch 
  5. 4 slack 
  6. 5 suse 

6. shuf命令

「Shut」命令隨機從一個文件或文件夾中選擇行/文件/文件夾。首先使用ls命令來顯示文件夾的內容。

  1. # ls 
  2. Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos 
  3. #  ls | shuf (shuffle Input) 
  4. Music 
  5. Documents 
  6. Templates 
  7. Pictures 
  8. Public 
  9. Desktop 
  10. Downloads 
  11. Videos 
  12. #  ls | shuf -n1 (pick on random selection) 
  13. Public 
  14. # ls | shuf -n1 
  15. Videos 
  16. # ls | shuf -n1 
  17. Templates 
  18. # ls | shuf -n1 
  19. Downloads 

注意:你能夠把‘ n1’替換成‘ n2’來輸出兩個隨機選擇或者使用 n三、 n4等數字輸出其餘任意的隨機選擇。

7. ss命令

「ss」表示socket統計。這個命令調查socket,顯示相似netstat命令的信息。它能夠比其餘工具顯示更多的TCP和狀態信息。

  1. # ss 
  2. State      Recv-Q Send-Q    Local Address:Port         Peer Address:Port   
  3. ESTAB      0      0      192.168.1.198:41250        *.*.*.*:http    
  4. CLOSE-WAIT 1      0    127.0.0.1:8000             127.0.0.1:41393   
  5. ESTAB      0      0     192.168.1.198:36239        *.*.*.*:http    
  6. ESTAB      310    0     127.0.0.1:8000             127.0.0.1:41384   
  7. ESTAB      0      0     192.168.1.198:41002       *.*.*.*:http    
  8. ESTAB      0      0     127.0.0.1:41384            127.0.0.1:8000 

8. last命令

「last」命令顯示的是上次登陸用戶的歷史信息。這個命令經過搜索文件「/var/log/wtmp」,顯示logged-in和logged-out及其tty‘s的用戶列表。

  1. #  last 
  2. server   pts/0        :0               Tue Oct 22 12:03   still logged in   
  3. server   tty8         :0               Tue Oct 22 12:02   still logged in   
  4. … 
  5. ... 
  6. (unknown tty8         :0               Tue Oct 22 12:02 - 12:02  (00:00)    
  7. server   pts/0        :0               Tue Oct 22 10:33 - 12:02  (01:29)    
  8. server   tty7         :0               Tue Oct 22 10:05 - 12:02  (01:56)    
  9. (unknown tty7         :0               Tue Oct 22 10:04 - 10:05  (00:00)    
  10. reboot   system boot  3.2.0-4-686-pae  Tue Oct 22 10:04 - 12:44  (02:39)    
  11. wtmp begins Fri Oct  4 14:43:17 2007 

9. curl ifconfig.me

那麼如何獲得你的外部IP地址呢?使用google?那麼這個命令就在你的終端輸出你的外部IP地址。

  1. # curl ifconfig.me 

注意:你可能沒有按照curl包,你須要 apt/yum來按照包。

10. tree命令

以樹式的格式獲得當前文件夾的結構。

11. pstree

這個命令顯示當前運行的全部進程及其相關的子進程,輸出的是相似‘tree’命令的樹狀格式

目前爲止就這麼多。在下篇文章中,我將涉及一些其餘不多有人知道的有趣的Linux命令。到那時鏈接 Tecmint保持收看。喜歡和分享將有助於咱們傳播。

相關文章
相關標籤/搜索